从 Jupyter Notebook 启动 Dash

Launch Dash from Jupyter Notebook

有没有一种方法可以在 Jupyter notebook 中使用以下代码行来启动 Dash 仪表板?

# if __name__ == '__main__':
#     app.run_server(debug=True)

当我尝试启动它时出现错误。

纠正它的唯一方法是将 debug 设置为 false。但是,当图表中的数据发生变化时,仪表板不会自动更新。

编辑

我刚刚发现一位有价值的 GitHub 用户发布了 following library. This is the direct link to the respective repo。请参阅他的文档和示例以在 jupyter 中成功实施它。它对我有用。

在安装库之前不要低估这个声明:"Use of a virtualenv environment is strongly recommended"!

原版Post

我自己前段时间也遇到过这个问题。我不知道它现在是否已本地修复。我当时使用的解决方法就是这个。它直接来自 Chris 本人,但仍然 debug = False

from IPython import display
def show_app(app,  # type: dash.Dash
             port=9999,
             width=700,
             height=350,
             offline=True,
             style=True,
             **dash_flask_kwargs):
    """
    Run the application inside a Jupyter notebook and show an iframe with it
    :param app:
    :param port:
    :param width:
    :param height:
    :param offline:
    :return:
    """
    url = 'http://localhost:%d' % port
    iframe = '<iframe src="{url}" width={width} height={height}></iframe>'.format(url=url,
                                                                                  width=width,
                                                                                  height=height)
    display.display_html(iframe, raw=True)
    if offline:
        app.css.config.serve_locally = True
        app.scripts.config.serve_locally = True
    if style:
        external_css = ["https://fonts.googleapis.com/css?family=Raleway:400,300,600",
                        "https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css",
                        "http://getbootstrap.com/dist/css/bootstrap.min.css", ]

        for css in external_css:
            app.css.append_css({"external_url": css})

        external_js = ["https://code.jquery.com/jquery-3.2.1.min.js",
                       "https://cdn.rawgit.com/plotly/dash-app-stylesheets/a3401de132a6d0b652ba11548736b1d1e80aa10d/dash-goldman-sachs-report-js.js",
                       "http://getbootstrap.com/dist/js/bootstrap.min.js"]

        for js in external_js:
            app.scripts.append_script({"external_url": js})

    return app.run_server(debug=False,  # needs to be false in Jupyter
                          port=port,
                          **dash_flask_kwargs)

这么用就是给你切换回调函数?请展示你的程序的一些逻辑。也许我可以提供更多提示。干杯。