Update server ColumnDataSource from python flask?


Is it possible to implement a push model for updating bokeh data sources (as opposed to a pull model in AjaxDatasource) where a datasource gets updated by an external caller? I’ve tried writing it using pull_session under the assumption that the session will synchronize states when it closes but that doesn’t seem to be the case. What I’m currently trying is this:

import os,sys,json,time
from datetime import datetime
from flask import jsonify,abort, Flask
from flask_classy import FlaskView, route,request
import logging

from bokeh.client import pull_session, push_session

app = Flask(__name__)

bokeh_url = ""
class ServerController(FlaskView):

    route_base = '/'
    def process(self):
        message =  json.loads(request.data)
        xs= message["x"]
        ys = message["y"]
        with pull_session(url=bokeh_url) as session:
            doc = session.document
            #data_source is a ColumnDataSource object defined on the bokeh server
            datasource = doc.get_model_by_name("data_source")
            datasource.data = {"x":xs, "y": ys}
        return ""

But it seems that the state update is not propagated to the server after the session closes. Any suggestions on whether this is possible?

You are not providing a session id in the call to pull_session, so this will just create a new session, with a new session_id. For any user to see the changes made, they would have to navigate to the Bokeh server with that bokeh-session-id query argument explicitly in the URL to see the new session, or else you could embed the new session in a page that this endpoint returns (using server_session).

Otherwise, if you are intending to update a session that the user already has open and is looking at, you would need to call pull_session with that actual, already existing session_id, for the single, already existing session they are currently viewing in their browser.