【问题标题】:Save simpleapp bokeh plot to standalone html file将 simpleapp 散景图保存到独立的 html 文件
【发布时间】:2015-04-27 01:35:32
【问题描述】:

以下脚本(来自this SO answer)生成一个简单的应用程序散景图。

如何将输出保存到独立的 html 文件中?

import bokeh.plotting as bk
from bokeh.models import ColumnDataSource, Plot
from bokeh.models.widgets import Select, AppVBox
from bokeh.simpleapp import simpleapp

data = {"a": {"x": [1,2,3], "y": [1,2,3]},
        "b": {"x": [3,2,1], "y": [1,2,3]},
        "c": {"x": [2,2,2], "y": [1,2,3]},}


options = ["a", "b", "c"]
select1 = Select(name = 'ticker1', value = options[0], options = options)

@simpleapp(select1)
def test_layout(ticker1):
    p = bk.figure(title = "layout test")

    chart_data = data[ticker1]

    df = ColumnDataSource(data = chart_data)
    p.circle(x = chart_data["x"], y = chart_data["y"])

    return {'plot': p}

@test_layout.layout
def layout(app):
    return AppVBox(app=app, children=['ticker1', 'plot'])

test_layout.route("/bokeh/layout/")

我尝试更改layout(app) 以使用file_html。这会生成初始图,但组合框不起作用。我认为file_html 仅适用于单个绘图对象,不适用于我正在使用的simpleapp

from bokeh.embed import file_html
from bokeh.resources import CDN

def layout(app):
    # save standalone html
    html = file_html(AppVBox(app=app, children=['ticker1', 'plot']), CDN, "title")
    f = open("bokeh_standalone.html", "w")
    f.write(html)
    f.close

【问题讨论】:

    标签: python plot bokeh


    【解决方案1】:

    目前没有太多关于 Bokeh 的文档,因此一些简单的任务似乎支持不足。 TL;DR:通过扩展使用here 提供的示例。

    但是,要启用“选择”框,您需要在其上调用on_change('value', func),其中func 是一个更新数据源的可调用对象。完成后,您可以设置datasource._dirty = True。重要的是,所有这些事情都必须发生在 另一个 属于应用程序类的方法中,该方法名为 setup_events

    散景服务器处理交互的方式是创建新的 Python 对象实例,这些对象存储在它选择的内部数据库(默认为 redis)中。这意味着每次都需要“设置事件”。

    强烈建议阅读 ContinuumIO 在 Bokeh Github 页面上提供的 Sliders App 示例:https://github.com/bokeh/bokeh/tree/master/examples/app/sliders_applet

    过去几周我一直在为此烦恼,但我们每天都在了解越来越多。我希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多