How to update DataTable on Embedded Bokeh Server on Tornado?

Hi!

I have Bokeh Server embedded into Tornado application, like:

Tornado Server:

io_loop = tornado.ioloop.IOLoop.current()
bokeh_server = get_bokeh_server(io_loop, {’/bokeh/app’: AppHandler.get_bokeh_app()})
bokeh_server.start(start_loop=False)

``


Bokeh Server:

def get_bokeh_server(io_loop, bokeh_applications: dict):
 server = Server(bokeh_applications,
 io_loop=io_loop, allow_websocket_origin=['xxx.xxx.xxx.xxx:8888']) # NOTE this is my real static IP
 return server

Application:

class AppHandler(BaseHandler):
@staticmethod
def modify_doc(doc):
source = ColumnDataSource(dict(xxx=, yyy=))

columns = [
TableColumn(field=“xxx”, title=“xxx”),
TableColumn(field=“yyy”, title=“YYY”)
]

data_table = DataTable(source=source, columns=columns)

def update_handler(*args, **kwargs):
print(“called update_handler”)

update_button = Button(label=“Update”, button_type=“success”)
update_button.on_click(update_handler)

doc.add_root(column(widgetbox(data_table), widgetbox(update_button)))

_bokeh_app = None

@classmethod
def get_bokeh_app(cls):
if cls._bokeh_app is None:
cls._bokeh_app = bokeh.application.Application(FunctionHandler(AppHandler.modify_doc))
return cls._bokeh_app

Tornado page handler:

class TornadoPageHandler(BaseHandler):

@gen.coroutine
def get(self):
user = self.current_user

bs= client.pull_session(session_id=user_id,
url=‘http://xxx.xxx.xxx.xxx:5006’,
app_path=’/bokeh/app’)

user_by_bokeh_session_id[bs.id] = user

doc_script = autoload_server(model=None, session_id=bs.id,

url=‘http://localhost:5006/bokeh/app

)

self.render(
‘mypage_template.html’,
doc_script=doc_script
)

``

autoload_server works, but client.pull_session cause OSError: Cannot push session document because we failed to connect to the server (to start the server, try the ‘bokeh serve’ command)

when Bokeh application is really available at localhost:5006/bokeh/app

what is going wrong?

UPDATE:

Tornado Server:

io_loop = tornado.ioloop.IOLoop.current()
bokeh_server = get_bokeh_server(io_loop, {’/bokeh/app’: AppHandler.get_bokeh_app()})
bokeh_server.start(start_loop=False)

``


Bokeh Server:

def get_bokeh_server(io_loop, bokeh_applications: dict):
 server = Server(bokeh_applications, io_loop=io_loop, allow_websocket_origin=['xxx.xxx.xxx.xxx:8888']) # NOTE this is my real static IP
 return server

Application:

class AppHandler(BaseHandler):
@staticmethod
def modify_doc(doc):
source = ColumnDataSource(dict(xxx=, yyy=))

columns = [
    TableColumn(field="xxx", title="xxx"),
    TableColumn(field="yyy", title="YYY")
]

data_table = DataTable(source=source, columns=columns)

def update_handler(*args, **kwargs):
  source.data = data_by_user[user_str]

update_button = Button(label="Update", button_type="success")
update_button.on_click(update_handler)

doc.add_root(column(widgetbox(data_table), widgetbox(update_button)))

_bokeh_app = None

@classmethod
def get_bokeh_app(cls):
if cls._bokeh_app is None:

cls._bokeh_app = bokeh.application.Application(FunctionHandler(AppHandler.modify_doc))
return cls._bokeh_app

Tornado page handler:

class TornadoPageHandler(BaseHandler):

@gen.coroutine
def get(self):
user_str = str(self.current_user)

doc_script = autoload_server(model=None, session_id=user_str,
                             app_path='/bokeh/inks_upload',
                             url='http://localhost:5006',
)

self.render('may_page-template.html', doc_script=doc_script)

``

Some external code:


user_id = self.current_user

class OtherHandler(RequestHandler):
  def post(self, *args, **kwargs):
    user_str = str(user_id = self.current_user)
    data_by_user[user_str] = data  # NOTE this works fine
    source = source_by_user_str[user_str]
    source.data = data  #  RuntimeError: _pending_writes should be non-None when we have a document lock, and we should have the lock when the document changes

Solution was found:

20:05

https://gist.github.com/Sklavit/c378a18661f0d91918931eba5a1d7553