Embeding: share doc across tornado and bokeh app

Dear all,

I am working on embeding a bokeh app inside of an already existing tornado app. I saw that it was possible to share the same ioloop, so I have a main that looks like this:

if __name__ == '__main__':
    # First instantiate main tornado infrastructure
    http_server = tornado.httpserver.HTTPServer(TornadoApplication())
    http_server.listen(tornado.options.options.port)
    io_loop = tornado.ioloop.IOLoop.current()

    # Now instantiate bokeh app
    bokeh_server = Server({'/bokeh_app': bokeh_app},
                          io_loop=io_loop,
                          allow_websocket_origin=['localhost:8888'])

    # Launch the whole thing
    io_loop.start()

And somwhere in my tornado handler, I get the generated bokeh script with:
script = server_document('http://localhost:5006/bokeh_app')

The problem is that, I would like to modify (like add points on user post, or add new data upon database update, or any event that might come from tornado).
The only way I found up to now, was to keep a dictionary of existing documents created per each user as a global variable shared both by the tornado handlers and bokeh_app.

As soon as certain event arise within tornado, I fetch the bokeh document with the right key within the dictionary and update it with:

document.add_next_tick_callback(update)

Now the problem that I have no way to share a unique identifier for each user, that would be shared in between tornado (like a cookie) and bokeh like a session id.
My main leads are:

  • Try to read a secure cookie from bokeh app ?
  • Try to force a session_id when including bokeh inside tornado handler with server_session instead of server_document for instance with a session_id that would be equal to the cookie value

What is in you opinion to do something like this ?

Thank you very much for your help

I would create a session ID outside of Bokeh, yes. That’s exactly what I’m doing in my app.
More than that, it’s just the current user session ID - throughout the whole app, not just for Bokeh. And I don’t use Bokeh auth as well, if that matters - Bokeh just manages plots and bidirectional communication, everything else is managed by the rest of the app.

1 Like

Tank you for your feedback.
As I was checking the documentation: bokeh.embed — Bokeh 3.3.2 Documentation
for
``server_session ( *model=None* , *session_id=None* , *url='default'* , *relative_urls=False* , *resources='default'* , *headers={}* )

I see:

Return a script tag that embeds content from a specific existing session on a Bokeh server.
This function is typically only useful for serving from a a specific session that was previously created using the bokeh.client API.

Do you know of any example about how to create a document explicitly, to get the session_id from there ?

Do you know of any example about how to create a document explicitly, to get the session_id from there ?

You would have to re-implement quite a bit a the code that’s already written in Bokeh. That’s why generating your own session ID is a much simpler approach.

1 Like

Great ! I ignored it was possible just to provide an arbitrary number to server_session, I’ll try that asap

It worked perfectly, thank you very much !!!

Don’t provide it just some small number though. Use UUID or something like that if you use nothing but session ID for authentication.