【问题标题】:Plotly Dash application not runningPlotly Dash 应用程序未运行
【发布时间】:2020-04-22 12:59:07
【问题描述】:

我正在尝试从 jupyter notebook 运行以下代码:

import dash
import dash_core_components as dcc
import dash_html_components as html

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div(children=[
    html.H1(children='Hello Dash'),

    html.Div(children='''
        Dash: A web application framework for Python.
    '''),

    dcc.Graph(
        id='example-graph',
        figure={
            'data': [
                {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
                {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
            ],
            'layout': {
                'title': 'Dash Data Visualization'
            }
        }
    )
])

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

它产生以下内容:

 * Serving Flask app "__main__" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:8050/ (Press CTRL+C to quit)
 * Restarting with stat
An exception has occurred, use %tb to see the full traceback.

SystemExit: 1


//anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py:3333: UserWarning:

To exit: use 'exit', 'quit', or Ctrl-D.

当我点击 http://127.0.0.1:8050/ 时,页面永远不会加载。我已经尝试了一段时间了。回复中是否有我遗漏的内容?这应该是一个基本的破折号应用程序。

这是回溯:

---------------------------------------------------------------------------
SystemExit                                Traceback (most recent call last)
<ipython-input-1-b057c246c3cf> in <module>
     29 
     30 if __name__ == '__main__':
---> 31     app.run_server(debug=True)
     32 
     33 get_ipython().run_line_magic('tb', '')

//anaconda3/lib/python3.7/site-packages/dash/dash.py in run_server(self, port, debug, **flask_run_options)
    566                    debug=False,
    567                    **flask_run_options):
--> 568         self.server.run(port=port, debug=debug, **flask_run_options)

//anaconda3/lib/python3.7/site-packages/flask/app.py in run(self, host, port, debug, load_dotenv, **options)
    988 
    989         try:
--> 990             run_simple(host, port, self, **options)
    991         finally:
    992             # reset the first request information if the development server

//anaconda3/lib/python3.7/site-packages/werkzeug/serving.py in run_simple(hostname, port, application, use_reloader, use_debugger, use_evalex, extra_files, reloader_interval, reloader_type, threaded, processes, request_handler, static_files, passthrough_errors, ssl_context)
   1005         from ._reloader import run_with_reloader
   1006 
-> 1007         run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
   1008     else:
   1009         inner()

//anaconda3/lib/python3.7/site-packages/werkzeug/_reloader.py in run_with_reloader(main_func, extra_files, interval, reloader_type)
    330             reloader.run()
    331         else:
--> 332             sys.exit(reloader.restart_with_reloader())
    333     except KeyboardInterrupt:
    334         pass

SystemExit: 1

【问题讨论】:

  • 回溯会很有帮助。
  • 在 vscode 或其他 ide 中试试这个,它会工作,它与 Jupyter notebbok 有问题。我用vs code试过了,sn-p代码没有问题。
  • @Seb 编辑 OP 以显示 %tb

标签: python plotly-dash


【解决方案1】:

如前面的回答所述,开箱即用,您无法运行 debug=True。因此人们坚持:

在 jupyter 上,做:

if __name__ == '__main__':
    app.run_server()

在 VSCode 等编辑器上,你可以这样做:

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

您仍然热衷于使用 Jupyter 而不是代码编辑器?
问题的讨论从这个解决的Github issue开始; here in plotly 强调了禁用调试的需要;讨论催生了jupyter-dash作为解决方案。

问题是:
启用调试模式直接启用重新加载器。反过来,重新加载器会导致任何 Flask 应用程序在启动时被初始化两次,这是 Jupyter 无法应对的。事实上,只要禁用重新加载器,您甚至可以在 jupyter 中运行 debug=True 。

因此,您可以这样做:

if __name__ == '__main__':
    app.run_server(debug=True, use_reloader=False)

【讨论】:

  • 谢谢@I.Ewetoye,我会看看他们的小部件。给你的问题 - 为什么认为/感觉代码编辑器优于 Jupyter?我错过了什么?
  • 我已经编辑了解决方案以包含一个简短的解释
【解决方案2】:

您不能在 jupyter 笔记本上以调试模式运行 dash,您必须禁用调试模式或转移到一个好的老式文本编辑器/IDE:

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

【讨论】:

  • jupyter 不是 IDE 吗?
  • Jupyter notebooks 实际上只是一种编辑和运行 ipython notebooks 的方法。另一方面,我肯定会说 Jupyter 实验室是一个 IDE。但是,这并不影响 .ipynb 文件与源代码不同的事实。为了运行调试模式,运行应用程序的实际文件必须是纯文本格式,而 .ipynb 不是。
【解决方案3】:

确保在运行代码期间中断内核。可能这就是你忘记的。

if __name__ == '__main__':
   app.run_server()

这对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 2022-01-20
    • 2018-02-01
    • 2019-01-21
    • 2021-03-07
    • 1970-01-01
    相关资源
    最近更新 更多