flask - 散景服务器如何从 server_document() 访问参数
问题描述
我有一个烧瓶应用程序,它与散景服务器对话。
我想将参数传递给散景服务器,以便散景服务器可以使用该信息以不同的方式显示事物。
这是我的烧瓶路线,包括我尝试将参数传递给散景的方式:
@app.route('/test')
def test():
return render_template(
'bokeh.html',
template='Flask',
script=server_document(
url='http://localhost:6001/test',
arguments={'foo': 'bar'}
))
我想我正确地传递了参数,但我不知道如何在散景服务器上访问这些参数。所以我实际上不知道他们是否到达那里,但我没有看到任何错误。
我知道server_document()
返回一个javascript字符串:
<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>
所以参数嵌入在 url 中,http://localhost:6001/test&foo=bar
但我仍然不知道散景服务器如何使它们可用于 python 代码。
如何访问参数:{'foo': 'bar'}
在散景服务器上?
编辑:
我以为我在从 Flask api 向 Bokeh autoload_server 传递参数中找到了答案,但我错了。
当我尝试将建议的内容添加到我的 main.ipynb 文件(我们使用面板来提供散景应用程序)时,它不起作用:
main.ipynb:
...
print(doc.session_context.request.arguments)
report.serve()
导致此错误:
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
解决方案
这在文档中有所描述:
# 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
推荐阅读
- tfs - 升级到 SonarQube 6.7.5 后,在 TFS 2017 构建定义中运行 SonarQube 的代码分析任务失败
- loops - SSIS - 使用顶部处理批次
- ios - 在 UIStackView 中增加 UIButton 的面积
- asp.net-mvc - 在 Visual Studio 上运行网站项目在本地因 http 404 未找到页面错误而失败
- java - 将多个 RecyclerView 与一个 Adapter 类一起使用
- typescript - 由于未定义的属性,地图失败
- python - 检查浮点值是否包含减号(连字符减号) - Python
- spi - SparkFun nRF52832 Breakout + SPI显示
- excel - ThisWorkbook.Sheets("Sheet1") 生成运行时错误“9”下标超出范围
- c++ - 如何初始化动态对象数组