how can a bokeh server access arguments from server_document()

跟風遠走 提交于 2020-07-07 09:06:05

问题


I have a flask app which talks to a bokeh server.

I want to pass arguments to the bokeh server so that the bokeh server can use that information to display things differently.

Here's my flask route, complete with how I'm attempting to pass arguments to bokeh:

@app.route('/test')
def test():
    return render_template(
        'bokeh.html', 
        template='Flask',
        script=server_document(
            url='http://localhost:6001/test',
            arguments={'foo': 'bar'}
    ))

I think I'm passing the arguments correctly but I don't know how to access these arguments on the bokeh server. So I don't actually know if they're getting there, but I see no errors.

I understand that the server_document() returns a javascript string:

<script src="http://localhost:6001/test/autoload.js?bokeh-autoload-element=1001&bokeh-app-path=/test&bokeh-absolute-url=http://localhost:6001/test&foo=bar" id="1001"></script>

So the arguments are embedded in the url http://localhost:6001/test&foo=bar but I still don't know how the bokeh server makes them available to the python code.

How do I access the arguments: {'foo': 'bar'} on the bokeh server?

EDIT:

I thought I found the answer in Passing arguments to Bokeh autoload_server from Flask api but I was wrong.

When I attempted to add what was suggested to my main.ipynb file (we're using panel to serve the bokeh app) it didn't work:

main.ipynb:
...
print(doc.session_context.request.arguments)
report.serve()

resulted in this error:

Error running application handler <bokeh.application.handlers.directory.DirectoryHandler object at 0x7f981b953cf8>: name 'doc' is not defined
File "main.ipynb"...
File "/conda/lib/python3.7/site-packages/bokeh/application/handlers/code_runner.py", line 179...
NameError: name 'doc' is not defined

回答1:


This is described in the documentation:

# request.arguments is a dict that maps argument names to lists of strings,
# e.g, the query string ?N=10 will result in {'N': [b'10']}

args = curdoc().session_context.request.arguments

try:
  N = int(args.get('N')[0])
except:
  N = 200


来源:https://stackoverflow.com/questions/57420450/how-can-a-bokeh-server-access-arguments-from-server-document

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!