bokeh / bokeh-django

Support for running Bokeh on Django
BSD 3-Clause "New" or "Revised" License
13 stars 6 forks source link

Using Panel>=1.3.0 in django_bokeh package #8

Open Jesse-vd-Linden opened 6 months ago

Jesse-vd-Linden commented 6 months ago

Hi There,

I am using the bokeh-django package to display a panel.chat.ChatInterface on my website.

The chat using the following panel_handler

def app(doc: Document) -> None:
    chat_interface.server_doc(doc)'

And is added to the bokeh_app with an autoload

bokeh_apps = [
    autoload("chat/", Agents.app)
]

After using the setup instructions for the README I was finally able to display the ChatInterface. For this, I did have to alter the following in the setting.py

from bokeh.settings import settings as bokeh_settings
# bokeh_settings.resources = 'server'
bokeh_settings.resources = 'inline'

If I use the the resources setting as server, it does not seem to find the static files from Panel.

HTTP GET /chat/autoload.js?bokeh-autoload-element=p1002&bokeh-app-path=/chat&bokeh-absolute-url=http://127.0.0.1:8000/chat 200 [0.22, 127.0.0.1:52925]
HTTP GET /static/js/bokeh.min.js?v=53f9ca7a5b60daf7940a6e91ff6bdf0913c7a7c780cae58750c958d90d0cf4a638713847f81637ff880a0d2dad5c57cf9988ac65af54c12df96cd7c3dffb4ac3 304 [0.01, 127.0.0.1:52925]
HTTP GET /static/js/bokeh-gl.min.js?v=32bcb6e6ccbd3c1a02be8407ea0e1e0b05a12a8c47a196f065fcf8639d99113cd4fcf3a6939fce0478f9c12f2cb7763011318f4fb8b4b8b579132ea29de9a099 304 [0.01, 127.0.0.1:52931]
HTTP GET /static/js/bokeh-widgets.min.js?v=f90def549d46f97ac5d3451c79f33a81b406a43529102059db32abc76aa700f18b8009e5e096f6f7b222a3b51226e53e77e62f5e48ac82cb3de0b300c5100959 304 [0.01, 127.0.0.1:52932]
HTTP GET /static/extensions/panel/panel.min.js?v=59296d23d623ce0adf7675d22156e83b1248c8508135a65dd8a9e38c497e94a8 404 [0.03, 127.0.0.1:52933]
HTTP GET /static/js/bokeh-tables.min.js?v=b3381341845a3f3b239aa9204f891355ff51670b2bba887db6b5f1de2ad890c65ed1d3f4e9d60c389f78f3109f299ade900cb1148e6d718a953fb372820c8573 304 [0.03, 127.0.0.1:52934]

However when using the inline as setting it does load the chat panel, but I am getting the error that protocol is not a key in the _RequestProxy object. Is this due to changing the bokeh_settings?

ERROR:bokeh.application.application:Error running application handler <bokeh.application.handlers.function.FunctionHandler object at 0x00000276821FD490>: 'protocol'
File 'consumers.py', line 355, in __getattr__:
return self[key] Traceback (most recent call last):
  File "C:\dev\PromoAgent\venv\Lib\site-packages\bokeh\application\handlers\function.py", line 140, in modify_document    
    self._func(doc)
  File "C:\dev\PromoAgent\app\PromoAgent\pn_app.py", line 9, in app
    chat_interface.server_doc(doc)
  File "C:\dev\PromoAgent\venv\Lib\site-packages\panel\viewable.py", line 1038, in server_doc
    self._add_location(doc, location, model)
  File "C:\dev\PromoAgent\venv\Lib\site-packages\panel\viewable.py", line 329, in _add_location
    loc = state.location
          ^^^^^^^^^^^^^^
  File "C:\dev\PromoAgent\venv\Lib\site-packages\panel\io\state.py", line 996, in location
    loc = Location.from_request(self.curdoc.session_context.request)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dev\PromoAgent\venv\Lib\site-packages\panel\io\location.py", line 76, in from_request
    if request.protocol:
       ^^^^^^^^^^^^^^^^
  File "C:\dev\PromoAgent\venv\Lib\site-packages\bokeh\server\contexts.py", line 378, in __getattr__
    val = getattr(self._request, name, None)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dev\PromoAgent\venv\Lib\site-packages\bokeh_django\consumers.py", line 355, in __getattr__
    return self[key]
           ~~~~^^^^^
KeyError: 'protocol'

Current dependencies are: bokeh==3.2.0 bokeh-django==0.1.0 channels==3.0.5 daphne==3.0.2 Django==4.2.9 Jinja2==3.1.3 panel==1.3.0 tornado==6.4

clementhelsens commented 5 months ago

hello, I see the same thing by simply running the example in django_embed python manage.py runserver and then browsing http://127.0.0.1:8000/shape_viewer or http://127.0.0.1:8000/shapes error:

February 14, 2024 - 07:26:50
Django version 4.2.7, using settings 'django_embed.settings'
Starting ASGI/Channels version 3.0.5 development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
HTTP GET /shapes 200 [0.02, 127.0.0.1:62960]
ERROR:bokeh.application.application:Error running application handler <bokeh.application.handlers.function.FunctionHandler object at 0x1153eb350>: 'protocol'
File 'consumers.py', line 352, in __getattr__:
return self[key] Traceback (most recent call last):
  File "/Users/helsens/miniconda3/envs/django_seg/lib/python3.11/site-packages/bokeh/application/handlers/function.py", line 140, in modify_document
    self._func(doc)
  File "/Users/helsens/Software/github/bokeh-django/examples/django_embed/django_embed/views.py", line 27, in shape_viewer_handler
    panel.server_doc(doc)
  File "/Users/helsens/miniconda3/envs/django_seg/lib/python3.11/site-packages/panel/viewable.py", line 1044, in server_doc
    self._add_location(doc, location, model)
  File "/Users/helsens/miniconda3/envs/django_seg/lib/python3.11/site-packages/panel/viewable.py", line 332, in _add_location
    loc = state.location
          ^^^^^^^^^^^^^^
  File "/Users/helsens/miniconda3/envs/django_seg/lib/python3.11/site-packages/panel/io/state.py", line 1015, in location
    loc = Location.from_request(self.curdoc.session_context.request)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/helsens/miniconda3/envs/django_seg/lib/python3.11/site-packages/panel/io/location.py", line 75, in from_request
    if request.protocol:
       ^^^^^^^^^^^^^^^^
  File "/Users/helsens/miniconda3/envs/django_seg/lib/python3.11/site-packages/bokeh/server/contexts.py", line 378, in __getattr__
    val = getattr(self._request, name, None)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/helsens/miniconda3/envs/django_seg/lib/python3.11/site-packages/bokeh_django/consumers.py", line 352, in __getattr__
    return self[key]
           ~~~~^^^^^
KeyError: 'protocol'

Could you please have a look?

philippjfr commented 5 months ago

Could you open a new issue in Panel since it probably has to be patched there.

sdc50 commented 1 month ago

Could you open a new issue in Panel since it probably has to be patched there.

@philippjfr

I was able to patch this in bokeh-django with:

class ConsumerHelper(AsyncConsumer):

    _prefix = "/"

    @property
    def request(self) -> "AttrDict":
        request = AttrDict(self.scope)
        request["arguments"] = self.arguments

        # patch for panel 1.4
        request['protocol'] = request.get('scheme')
        for k, v in request.headers:
            request[k.decode()] = v.decode()
        request['uri'] = request.get('path')

        return request

Do you think this is sufficient, or should changes be made in panel?