我一直在使用Bokeh绘制一个大型数据库,并使用Flask在本地主机上提供该应用程序。汇总的代码如下所示:
app = Flask(__name__)
def make_doc(doc):
def plot_time():
p = figure(plot_height=400, plot_width=1000, tools="xpan,box_zoom,xwheel_zoom,reset,save",
x_axis_type="datetime", background_fill_color="#efefef",outline_line_color="#000000")
for us,color in zip(lista_plots,colors):
p.line(x="Instant", y=us, source=source, name=us, line_color=color, line_width=1, legend=us.title())
return p
def plot_time_aux():
p = figure(plot_height=115, plot_width=1000, x_axis_type="datetime", y_axis_type=None, tools="", background_fill_color="#efefef")
for us in list_plots:
p.line(x="Instant", y=us, source=source, name=us, line_color="gray", alpha=0.55)
return p
p1 = plot_time()
p2 = plot_time_aux()
doc.add_root(p1)
doc.add_root(p2)
doc.title = "Time Plot"
@app.route('/', methods=['GET'])
def bkapp_page():
script = server_document('http://localhost:5006/bkapp')
return render_template("index.html", script=script)
def bk_worker():
server = Server({'/bkapp': make_doc}, io_loop=IOLoop(), allow_websocket_origin=["localhost:{}".format(port)])
server.start()
server.io_loop.start()
from threading import Thread
Thread(target=bk_worker).start()
if __name__ == '__main__':
print('Opening single process Flask app with embedded Bokeh application on http://localhost:{}/'.format(port))
webbrowser.open_new("http://localhost:{}/".format(port))
app.run(port=port, debug=False)
该代码运行得很好,但是当涉及到将p1和p2插入到Jinja2 html模板的自定义div中时,我不知道怎么做。 html模板如下所示:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Time Plots</title>
</head>
<body>
{% for root in script.roots %}
<div>{{ embed(root) }}</div>
{% endfor %}
{{ script | safe }}
</body>
</html>
照原样,该脚本一个接一个地绘制p1和p2,只是忽略Jinja2 for循环(可能是因为我在模板中引用的变量不存在...)。但是,我想将每个图(p1和p2)作为参数传递给render_template()函数,这样我可以自由地将它们放在html模板上的任何位置,但是我不知道如何。
任何想法都欢迎。
参考方案
我将概述如何实现多个绘图:
为每个图创建一个单独的Bokeh应用程序。在example处参考modify_doc
def change_doc1():
# code to define how plot 1 should behave
def change_doc2():
# code to define how plot 2 should behave
使用多个应用程序而不是单个应用程序初始化Bokeh Server
def bk_worker():
plots = { "/plot1": change_doc1, "/plot2": change_doc2 }
server = Server(plots, io_loop=IOLoop(), allow_websocket_origin=["localhost:{}".format(port)])
server.start()
server.io_loop.start()
在Flask路线中分别获取scrtip,并将其发送到模板。
@app.route('/', methods=['GET'])
def bkapp_page():
script1 = server_document('http://localhost:5006/plot1')
script2 = server_document('http://localhost:5006/plot2')
return render_template("embed.html", script1=script1, script2=script2)
分别使用模板中的脚本并将其放置在HTML中所需的位置
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Time Plots</title>
</head>
<body>
<div id="plot1">
{{ script1 | safe }}
</div>
<div id="plot2">
{{ script2 | safe }}
</div>
</body>
</html>
Python-crontab模块 - python我正在尝试在Linux OS(CentOS 7)上使用Python-crontab模块我的配置文件如下:{ "ossConfigurationData": { "work1": [ { "cronInterval": "0 0 0 1 1 ?", "attribute&…
Python Pandas导出数据 - python我正在使用python pandas处理一些数据。我已使用以下代码将数据导出到excel文件。writer = pd.ExcelWriter('Data.xlsx'); wrong_data.to_excel(writer,"Names which are wrong", index = False); writer.…
Python:在不更改段落顺序的情况下在文件的每个段落中反向单词? - python我想通过反转text_in.txt文件中的单词来生成text_out.txt文件,如下所示:text_in.txt具有两段,如下所示:Hello world, I am Here. I am eighteen years old. text_out.txt应该是这样的:Here. am I world, Hello old. years eighteen a…
每个文件合并后添加换行 - python我有很多类似以下内容的JSON文件:例如。1.json{"name": "one", "description": "testDescription...", "comment": ""} test.json{"name"…
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…