【问题标题】:Bokeh Widget in FlaskFlask 中的散景小部件
【发布时间】:2019-11-22 07:36:08
【问题描述】:

我想在我的烧瓶应用程序中使用散景小部件,但我似乎无法触发一个简单的 on_click 行为。

我是 flask/bokeh/javascript/html 的新手。代码对我来说看起来不错,也许是编码步骤? (我从网上的几个例子中总结出来)。

我的工作示例如下。我可以随意点击boop 按钮,但没有任何变化(没有打印输出、标签更改、控制台日志等)

app.py

from flask import Flask, render_template
from bokeh.embed import components
from bokeh.resources import INLINE
from bokeh.util.string import encode_utf8
from bokeh.models.widgets import Button
app = Flask(__name__)

@app.route('/')
def index():
    # grab the static resources
    js_resources = INLINE.render_js()
    css_resources = INLINE.render_css()
    widget_status = "no clicked"

    # Submit button
    button = Button(label="boop", button_type="success")
    def my_button_handler():
        widget_status = "clicked"
        button.label = "mlem"

    button.on_click(my_button_handler)
    scriptb, divb = components(button)

    # render template
    html = render_template(
        'bokeh_bokeh.html',
        js_resources=js_resources,
        css_resources=css_resources,
        scriptb=scriptb,
        divb=divb,
        widget_status=widget_status
    )
    return encode_utf8(html)


if __name__ == '__main__':
    app.run(port=5000)

bokeh_bokeh.html

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>Bokeh Bokeh</title>
    {{ js_resources|indent(4)|safe }}
    {{ css_resources|indent(4)|safe }}
    {{ scriptb|indent(4)|safe }}
  </head>
  <body>
    {{ divb|indent(4)|safe }}
    {{ widget_status|indent(4)|safe }}
  </body>
</html>

【问题讨论】:

  • 我也会满足于 dash 等价物。我不想使用 wtf,因为我想最终使用颜色选择器小部件。

标签: python-3.x flask bokeh


【解决方案1】:

on_click 仅适用于在 Bokeh 服务器中运行的应用程序(Bokeh 服务器是执行回调代码的实际 Python 进程)。 components API 用于生成独立的(纯 HTML+JS)输出,因此这种组合不起作用。您的选择是:

【讨论】:

  • 感谢您的简洁说明!我将运行一些真正的 Python 代码,所以我会尝试你的前两个建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
  • 2016-12-18
  • 1970-01-01
  • 2021-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多