Is it possible to save user session?

I was wondering if there’s a way to reload a user’s session (when using bokeh server) so that the document will be in the same state it was the last time it was used.
If not supported by bokeh, is there a way to achieve that in some other way?

There’s nothing defined specifically for that. However, Bokeh apps can access HTML arguments during session creation. So, if you define your own scheme to encode the current session state e.g. into some string token or similar, then you could pass that as an HTML arguments for session creation, and the app code could use the token contents to modify the session document appropriately.

@Bryan Thanks…interesting!
Do you know if there’s a simple example somewhere that I could start off with?

In the meanwhile, I tried some things and came up with the following:
main.py:

import os
import json
from bokeh.io import curdoc
from bokeh.models.layouts import Column
from bokeh.models.widgets.buttons import Button
from bokeh.models.widgets.inputs import Select

base_dir = os.path.dirname(__file__)
saved_sessions_path = os.path.join(base_dir, "saved_sessions.json")
user_key = curdoc().session_context.request.arguments.get("user_key", [bytes()])[0].decode()

with open(saved_sessions_path, "r") as f:
    saved_sessions = json.load(f)


def save_doc():
    """Saves the document state as a json object.
    """
    doc_json = curdoc().to_json_string()
    with open(saved_sessions_path, "w") as f:
        saved_sessions.update({user_key: doc_json})
        json.dump(saved_sessions, f, indent=2)


def create_app():
    """Creates the app from scratch
    """
    select = Select(options=[str(i) for i in range(10)])
    button = Button(label="disabled", disabled=True)

    def on_change(attr, old, new):
        button.disabled = not button.disabled
        button.label = "disabled" if button.disabled else "enabled"
        save_doc()

    select.on_change("value", on_change)
    curdoc().add_root(Column(select, button))


if user_key not in saved_sessions:
    create_app()

app_hooks.py:

import os
import json

base_dir = os.path.dirname(__file__)
saved_sessions_path = os.path.join(base_dir, "saved_sessions.json")


def on_session_created(session_context):
    user_key = session_context.request.arguments.get("user_key", [bytes()])[0].decode()
    with open(saved_sessions_path, "r") as f:
        saved_sessions = json.load(f)

    if not user_key or user_key not in saved_sessions:
        return

    if user_key in saved_sessions:
        doc_json = json.loads(saved_sessions[user_key])
        session_context._document.replace_with_json(doc_json)

So basically I’m fetching the user key and based on that I either create the app from scratch or load it using a json. The json gets updated each time the user makes any significant change in the app.
Is that a good direction or I’m missing something?

This seems reasonable. I forgot about on_session_created that is definitely another place you could hook in to customize session creation. I don’t have any examples I can point to, though.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.