Plotly Dash 应用程序无法运行

7

我正在尝试从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/时,页面无法加载。我已经尝试了一段时间了。是否存在响应中我所缺少的内容?这应该是一个基本的dash应用程序。
以下是回溯信息:
---------------------------------------------------------------------------
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

回溯信息会很有帮助。 - Seb
在VSCode或其他IDE中尝试此代码,它可以正常工作,但在Jupyter Notebook中存在问题。我已经在VSCode中尝试过,代码片段没有问题。 - Vikas Sharma
@Seb 编辑 OP 以显示 %tb - AdamA
4个回答

13

正如之前的回答所述,开箱即用时无法运行debug=True。因此人们会选择:

在jupyter上执行以下操作:

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

在像VSCode这样的编辑器中,你可以做到:
if __name__ == '__main__':
    app.run_server(debug=True)

你仍然喜欢使用Jupyter而不是代码编辑器吗?
这个问题的讨论始于这个已解决的Github问题;需要禁用调试模式在plotly中被强调;并且该讨论产生了jupyter-dash作为解决方案。

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

因此,您可以执行以下操作:

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

谢谢@I.Ewetoye,我会查看他们的小部件。我有一个问题 - 你为什么认为代码编辑器比Jupyter更优秀?我错过了什么吗? - AdamA
我已经编辑了解决方案,包括简要说明。 - I.Ewetoye
太棒了,它起作用了。谢谢您的答案!!! - Usman Afridi

3

你无法在jupyter笔记本上以调试模式运行dash,你需要禁用调试模式或切换到传统的文本编辑器/IDE:

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

Jupyter不是一个IDE吗? - AdamA
1
Jupyter笔记本实际上只是编辑和运行ipython笔记本的一种方式。另一方面,我肯定会说Jupyter lab是一个IDE。然而,这并不影响.ipynb文件与源代码不同的事实。为了运行调试模式,运行应用程序的实际文件必须是纯文本格式,而.ipynb不是。 - Jay Mody

1

要使(debug=True)在jupyter上工作。导入JupyterDash并使用JupyterDash代替dash.Dash。

from jupyter_dash import JupyterDash
from dash import html
from dash import dcc

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

app = JupyterDash(__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)

enter image description here


0

确保在运行代码时中断内核,这可能是您忘记的事情。

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

这对我有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接