首页 > 解决方案 > 从 jupyter 笔记本运行破折号

问题描述

我正在尝试通过 jupyter notebook 运行 dash,但该应用程序未启动。我得到 SystemExit: 1 错误。请帮忙。我不是要求在笔记本中运行,而只是启动网络应用程序。如果我使用 app.py 文件,它会运行,但为什么我不能通过 jupyter notebook 运行?

'''
import dash
import dash_cytoscape as cyto
import dash_html_components as html

app = dash.Dash(__name__)
app.layout = html.Div([
    cyto.Cytoscape(
        id='cytoscape',
        elements=[
            {'data': {'id': 'one', 'label': 'Node 1'}, 'position': {'x': 50, 'y': 50}},
            {'data': {'id': 'two', 'label': 'Node 2'}, 'position': {'x': 200, 'y': 200}},
            {'data': {'source': 'one', 'target': 'two','label': 'Node 1 to 2'}}
        ],
        layout={'name': 'preset'}
    )
])

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

我收到此错误:

Running on http://127.0.0.1:5001/
Running on http://127.0.0.1:5001/
Debugger PIN: 699-998-824
Debugger PIN: 699-998-824
 * Serving Flask app "__main__" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
An exception has occurred, use %tb to see the full traceback.

SystemExit: 1


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

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

%tb 对于这个烧瓶问题:

烧瓶问题

标签: pythonjupyter-notebook

解决方案


@idontcare 您必须更改几行才能在 jupyter notebook 中运行此代码。首先安装 jupyter plotly dash with

[pip install jupyter-plotly-dash] 本次安装中的django需要通过安装[pip install -U django-plotly-dash==0.9.8]来设置。

那么jupyter-plotly-dash安装自带的dash有bug,需要降级到0.38.0。您可以使用 [pip install dash==0.38.0] 安装它

查看更新的代码:

'''

from jupyter_plotly_dash import JupyterDash
import dash
import dash_cytoscape as cyto
import dash_html_components as html

app = JupyterDash('YourAppExample')

app.layout = html.Div([
    cyto.Cytoscape(
        id='cytoscape',
        elements=[
            {'data': {'id': 'one', 'label': 'Node 1'}, 'position': {'x': 50, 'y': 50}},
            {'data': {'id': 'two', 'label': 'Node 2'}, 'position': {'x': 200, 'y': 200}},
            {'data': {'source': 'one', 'target': 'two','label': 'Node 1 to 2'}}
        ],
        layout={'name': 'preset'}
    )
])

app


推荐阅读