从Flask发送URL参数到一个Bokeh服务器 [英] Sending URL parameter from Flask to a Bokeh server

查看:320
本文介绍了从Flask发送URL参数到一个Bokeh服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图将Bokehautoloaded服务器集成到Flask应用程序中,用户可以在另一个页面上选择要加载的数据集。 >这组数据的ID在URL(获取参数)中,我无法从Flask应用程序发送到Bokeh服务器。



一些示例代码:
$ b $ pre $ $ $ $
$进口子进程
进口atexit
进口子进程,来自flask进口的os

render_template,render_template_string,Flask
$ b 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 $ gt;
{{bokeh_script | safe}}
< / div>
< / body>
< / html>

$ b $ app = Flask(__ name__)

bokeh_process = subprocess.Popen(
['bokeh','serve','--allow-webs ():
$ _
$ b @ atexit.register
def kill_server():
bokeh_process.kill ocket-origin = localhost:5000','sample.py'],stdout = subprocess.PIPE ()

@ app.route('/ visualization /< int:batchid>')
def visualization(batchid):
session = pull_session(app_path ='/ sample ')

#我喜欢做这样的事情...虽然它不起作用:
session.document.batch_id = batchid
bokeh_script = autoload_server(无,app_path =/ sample,session_id = session.id)
return render_template_string(app_html,bokeh_script = bokeh_script)
$ b $ if if __name__ =='__main__':
print( STARTED)
app.run(debug = True)



 #sample.py 
导入熊猫作为pd
导入幻灯片

#...不起作用
data = pd.read_csv('batch-%i.csv'%(curdoc()。batch_id))

由于autoload_server创建一个JavaScript片段,没有办法使用Bokeh服务器的URL参数来传递这些数据(以及 curdoc()。session_context



那么...有什么办法将参数传递给散景应用程序吗?
TYA!

解决方案

您可以通过附加到< ;从 autoload_server 返回的脚本> 标记。看看我的函数 appendQuery 在这个问题

  def appendQuery(脚本,key,value):
#将参数作为查询字符串传递给脚本的src url:这将在下一个Bokeh版本中正式引入,以避免这种破解
script_list = script.split \\\

idxSrcAttr = 2
script_list [idxSrcAttr] = script_list [idxSrcAttr] [: - 1] +& {} = {} \。format(key,value )
script =\ n.join(script_list)
返回脚本



<这是方法论应该正式在即将到来的散景发布根据... https://github.com/bokeh/bokeh/issues/5992#issuecomment-287232101


I'm trying to integrate a Bokeh "autoloaded" server into a Flask app where the set of data to load would be chosen by the user on another page.

The ID of this set of data is in the URL (get parameter), and I'm not able to send it from the Flask app to the Bokeh server.

Some example code:

# 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)

and for the bokeh server:

# sample.py
import pandas as pd  
import bokeh  

# ... doesn't work
data = pd.read_csv('batch-%i.csv' % (curdoc().batch_id))

Since autoload_server creates a Javascript snippet, there is no way to use the URL parameters for the Bokeh server to pass this data (along with curdoc().session_context)

So... is there any way to pass arguments to a Bokeh app that way? TYA!

解决方案

You can do it by appending to the src attribute of the <script> tag returned from autoload_server. Check out my function appendQuery in the source for this question

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

This is methodology is supposed to be formalized in an upcoming Bokeh release according to... https://github.com/bokeh/bokeh/issues/5992#issuecomment-287232101

这篇关于从Flask发送URL参数到一个Bokeh服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆