7

I am attempting to run the following code from 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)

It produces the following:

 * 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.

When I click on http://127.0.0.1:8050/ , the page never loads. I've been trying this for awhile now. Is there something in the response that I am missing? This should be a basic dash application.

Here is the traceback:

---------------------------------------------------------------------------
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
3
  • The traceback would be helpful. Commented Jan 4, 2020 at 2:39
  • try this in vscode or in other ide it will work, it is having issue with Jupyter notebbok. i tried with vs code , there is no issue with code snippet. Commented Jan 4, 2020 at 2:51
  • @Seb editing OP to show the %tb Commented Jan 4, 2020 at 2:59

4 Answers 4

13

Out of the box, as stated by a previous answer, you can't run debug=True. Hence people stick with:

On jupyter, do:

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

on editor such as VSCode, you can do:

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

You're still keen on using Jupyter instead of a code editor?
The discussion of the issue started from this resolved Github issue; the need to disable debug was highlighted here in plotly; and the discussion gave birth to jupyter-dash as a solution.

The problem is that:
Enabling the debug mode directly enables the reloader. In turn, the reloader will cause any Flask app to be initialized twice at startup this is what Jupyter can't cope with. Infact, you can even run debug=True in jupyter provided you disable the reloader.

Thus, you can do:

if __name__ == '__main__':
    app.run_server(debug=True, use_reloader=False)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @I.Ewetoye, I will check out their widget. Question for you - why do think/feel that a code editor is superior to Jupyter? What am I missing?
I have edited the solution to include a brief explanation
3

You can't run dash in debug mode on a jupyter notebook, you'll have to disable debug mode or move to a good old fashion text editor/IDE:

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

2 Comments

Isn't jupyter an IDE?
Jupyter notebooks is really just a way to edit and run ipython notebooks. Jupyter lab on the other hand I would definetly say is an IDE. However, it doesn't affect the fact that a .ipynb file is not the same as source code. In order to run debug mode, the actual file that the runs the app has to be in plain text, which a .ipynb is not.
1

to make (debug=True) work on jupyter. import JupyterDash and use JupyterDash instead of 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

Comments

0

Make sure you interrupt the Kernel during running the code. Probably that's what you are forgetting.

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

This works for me.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.