python - 将 "Bokeh created html file"嵌入到 Flask "template.html"文件中

标签 python flask jinja2 bokeh

我有一个用 Python 编写的 Web 应用程序 - Flask。当用户在其中一个页面(POST 请求)中填写一些设置时,我的 Controller 计算一些函数并使用 Bokeh 和以下命令绘制输出,然后我重定向到 Bokeh 创建的 HTML 页面。

output_file("templates\\" + idx[j]['name'] + ".html", title = "line plots")
TOOLS="resize,crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select"
p = figure(tools=TOOLS, x_axis_label = 'time', y_axis_label = 'L', plot_width = 1400, plot_height = 900)

我的所有 HTML 页面都扩展了我的“Template.HTML”文件,除了 Bokeh 生成的那些。我的问题是如何自动修改 Bokeh 生成的 HTML 文件以扩展我的 template.html 文件?这样,我的所有导航栏和超大屏幕都位于 Bokeh html 文件之上。

  {% extends "template.html" %}
  {% block content %}

  <Bokeh.html file>

  {% endblock %}

最佳答案

在这种情况下,您不想使用 output_file。 Bokeh 有一个专门用于嵌入到 Web 应用程序中的 HTML 模板的功能,bokeh.embed.component,在 quickstart 中进行了演示。和 tutorial .

from bokeh.embed import components
script, div = components(plot)
return render_template('page.html', script=script, div=div)
<body>
{{ div|safe }}
{{ script|safe }}
</body>

Here is a complete, runnable example that that shows how to use this with Flask.

关于python - 将 "Bokeh created html file"嵌入到 Flask "template.html"文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33398950/

相关文章:

Python - 如何查找多维数组中是否存在某个项目?

ubuntu - Ubuntu 12.10 上 uWSGI 上的 Flask - 版本不匹配?

python - Angular、Flask、mysql.connector 编程错误 : Wrong number of arguments

python - 将字符串拆分为jinja中的列表?

python - 位置参数与关键字参数

python - 为什么 pip 说 "No module named commands.install"?

javascript - 将 Flask 变量传递到 Javascript

python - Flask 调用另一个函数

javascript - 将 jinja2 url_for 设置为 getJSON 回调中的 href

python - 如何使用 np.arange 按系列类型划分系列