将 URL 参数从 Flask 发送到 Bokeh 服务器
Sending URL parameter from Flask to a Bokeh server
我正在尝试将 Bokeh "autoloaded" 服务器集成到 Flask 应用程序中,用户可以在另一个页面上选择要加载的数据集。
这组数据的 ID 在 URL(get 参数)中,我无法将它从 Flask 应用程序发送到 Bokeh 服务器。
一些示例代码:
# flask_app.py
import subprocess
import atexit
import subprocess, os
from flask import render_template, render_template_string, Flask
from bokeh.embed import autoload_server
from bokeh.client import pull_session, push_session
app_html = """
<!DOCTYPE html>
<html lang="en">
<body>
<div class="bk-root">
{{ bokeh_script|safe }}
</div>
</body>
</html>
"""
app = Flask(__name__)
bokeh_process = subprocess.Popen(
['bokeh', 'serve', '--allow-websocket-origin=localhost:5000', 'sample.py'], stdout=subprocess.PIPE)
@atexit.register
def kill_server():
bokeh_process.kill()
@app.route('/visualization/<int:batchid>')
def visualization(batchid):
session = pull_session(app_path='/sample')
# I'd love to do something like that... though it doesn't work :
session.document.batch_id = batchid
bokeh_script = autoload_server(None, app_path="/sample", session_id=session.id)
return render_template_string(app_html, bokeh_script=bokeh_script)
if __name__ == '__main__':
print("STARTED")
app.run(debug=True)
对于散景服务器:
# sample.py
import pandas as pd
import bokeh
# ... doesn't work
data = pd.read_csv('batch-%i.csv' % (curdoc().batch_id))
由于 autoload_server 创建了一个 Javascript 片段,因此无法使用 Bokeh 服务器的 URL 参数来传递此数据(连同 curdoc().session_context
)
所以...有没有办法以这种方式将参数传递给 Bokeh 应用程序?
提亚!
您可以通过附加到从 autoload_server
返回的 <script>
标记的 src 属性来实现。在这个
的源代码中查看我的函数 appendQuery
def appendQuery( script , key , value) :
# Pass along the parameter as a query string to the script's src url: TODO this will formally be introduced in next release of Bokeh to avoid this hack
script_list = script.split("\n")
idxSrcAttr = 2
script_list[idxSrcAttr] = script_list[idxSrcAttr][:-1] + "&{}={}\"".format( key , value )
script = "\n".join(script_list)
return script
这种方法应该在即将发布的 Bokeh 版本中正式化,根据... https://github.com/bokeh/bokeh/issues/5992#issuecomment-287232101
我正在尝试将 Bokeh "autoloaded" 服务器集成到 Flask 应用程序中,用户可以在另一个页面上选择要加载的数据集。
这组数据的 ID 在 URL(get 参数)中,我无法将它从 Flask 应用程序发送到 Bokeh 服务器。
一些示例代码:
# flask_app.py
import subprocess
import atexit
import subprocess, os
from flask import render_template, render_template_string, Flask
from bokeh.embed import autoload_server
from bokeh.client import pull_session, push_session
app_html = """
<!DOCTYPE html>
<html lang="en">
<body>
<div class="bk-root">
{{ bokeh_script|safe }}
</div>
</body>
</html>
"""
app = Flask(__name__)
bokeh_process = subprocess.Popen(
['bokeh', 'serve', '--allow-websocket-origin=localhost:5000', 'sample.py'], stdout=subprocess.PIPE)
@atexit.register
def kill_server():
bokeh_process.kill()
@app.route('/visualization/<int:batchid>')
def visualization(batchid):
session = pull_session(app_path='/sample')
# I'd love to do something like that... though it doesn't work :
session.document.batch_id = batchid
bokeh_script = autoload_server(None, app_path="/sample", session_id=session.id)
return render_template_string(app_html, bokeh_script=bokeh_script)
if __name__ == '__main__':
print("STARTED")
app.run(debug=True)
对于散景服务器:
# sample.py
import pandas as pd
import bokeh
# ... doesn't work
data = pd.read_csv('batch-%i.csv' % (curdoc().batch_id))
由于 autoload_server 创建了一个 Javascript 片段,因此无法使用 Bokeh 服务器的 URL 参数来传递此数据(连同 curdoc().session_context
)
所以...有没有办法以这种方式将参数传递给 Bokeh 应用程序? 提亚!
您可以通过附加到从 autoload_server
返回的 <script>
标记的 src 属性来实现。在这个
appendQuery
def appendQuery( script , key , value) :
# Pass along the parameter as a query string to the script's src url: TODO this will formally be introduced in next release of Bokeh to avoid this hack
script_list = script.split("\n")
idxSrcAttr = 2
script_list[idxSrcAttr] = script_list[idxSrcAttr][:-1] + "&{}={}\"".format( key , value )
script = "\n".join(script_list)
return script
这种方法应该在即将发布的 Bokeh 版本中正式化,根据... https://github.com/bokeh/bokeh/issues/5992#issuecomment-287232101