问题
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