Production and development enviroments

Hi!
I’m creating a dash that I need to run in 2 different computers. In one of them is working all fine, in the other computer I tried to copy everything to run it but a message pops up:

Serving Flask app "myDashApp" (lazy loading)
* Environment: production
 WARNING: Do not use the development server in a production environment.
 Use a production WSGI server instead.

I think I did exactly the same in both computers, but I don’t manage to run the code in the second computer at it does in the first. Do you have any idea of what I’m doing wrong?

Thank you!

Any version of app.run_server() should produce this warning, Dash is using a library called Flask and calling the .run() method on the Flask app which by default uses the werkzeug development server which is similar to Python’s http.server. While the a great library for quickly running web-server in development it is not tested for security or performance.

So what do you use instead? Popular choices are gunicorn and uWSGI .

I personally use gunicorn, so in my Dash index.py (which is the script that launches my Dash app, otherwise replace index with the name of your script) I have the following code:

if __name__ == '__main__':
    # For Development only, otherwise use gunicorn or uwsgi to launch, e.g.
    # gunicorn -b 0.0.0.0:8050 index:app.server
    app.run_server(
        port=8050,
        host='0.0.0.0'
    )

And so I have gunicorn installed and when I want to launch via gunicorn I do the following:

gunicorn -b 0.0.0.0:8050 index:app.server

This, among other things, produces better performance.

Finally you may want to hook gunicorn/uWSGI to Apache, nginx, or IIS, these web servers lots of production stability options, directly serving and caching your resources, timeout options, and many other other things.

1 Like

Hi Damian, thank you for the answer! I’m trying to do it, but I seem to have a problem with gunicorn. It warns me that he cannot import the module fcntl. I saw that might be related because I’m trying to run it on windows. I also tried with waitress but it’s still not working:

waitress-serve dash_alphav6:app

I’ve never used waitress-serve but assuming dash_alphav6 is your python file (i.e. dash_alphav6.py) and app is your dash.Dash() then you will probably need to use the command:

waitress-serve dash_alphav6:app.server

As app.server is the underlying Flask server these frameworks usually need.

2 Likes

It’s working! thank you soooo much!

1 Like

@Damian,
I am new to Python and just installed Dash and plotly. I took the simplest app example from the user documentation and tried to run it in Spyder. Initially I was getting the same error/warning to not use Debug mode. I then installed gunicorn after I looked at this post. However, I am unable to follow where exactly the gunicorn call goes. Any help/guidance is greatly appreciated.

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)
    app.run_server(port=8050,host='0.0.0.0')
#    gunicorn -b 0.0.0.0:8050 index:app.server

This results in the following:

  • 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: off
  • Running on http://0.0.0.0:8050/ (Press CTRL+C to quit)

If you’re not actually trying to deploy an app into production, then it’ll be easier to avoid gunicorn. Instead of running in Spyder, try running on the command line. ie:

$ python your_app.py

@nedned,
Thank you for your response. I tried to run the app in my Anaconda Prompt using the following command

python app.py

[quote=“nedned, post:7, topic:21348, full:true”]
If you’re not actually trying to deploy an app into production, then it’ll be easier to avoid gunicorn. Instead of running in Spyder, try running on the command line. ie:

$ python your_app.py

5-4-2019%209-43-50%20AM

It does not really yield anything in my browser, so I am not sure what to expect.

Maybe turn debug mode back on to start with. Can you be more specific about what you’re seeing in the browser when you load http://localhost:8050? Is anything displayed at all?

@nedned,
Nothing is displayed at all, it does not open a new browser window even

You need to open a browser yourself and load the URL: http://localhost:8050/

2 Likes

Hi guys! What @nedned is saying is true, you need to open http://0.0.0.0:8050/ in the web browser. @UdayGuntupalli but instead of running in the CMD:python main.py I recommend you to install waitress and run waitress-serve main:app.server

But note that running with waitress (ie as a WSGI app) will turn result in debug mode being turned off (unless you set the relevant environment variable to turn it on) so you may want to do that in production, while sticking to the dev server approach when developing.

@mifabpre @nedned

Hello,

I am trying to deploy my program in production mode with waitress, but I am getting an issue of not being able to load again. Basically, when I run it one, it works, then when I try to run it again, it doesn’t.

I think it’s because I am using waitress wrong in my python program. I basically import waitress and use serve(app). Is this wrong? How should I be using waitress? Any sample code would be greatly appreciated as well!

Hi!
I don’t know how are you doing it. I guess that you are not clossing waitress once you launch it for the first time. In my case I’ve build a *.bat file and I’m running the following code:

start chrome http://localhost:8050
waitress-serve --listen=*:8050 mydashapp:app.server

And then it closes when you close the cmd window.

What I didn’t manage yet is to continue the development in other computer running the debug mode. As @nedned says, the it turns off. So waitress is just working to run your dash propperly, but I don’t think it’s good enough to do development.

Thanks. So where do I type this in my python code?

start chrome http://localhost:8050
waitress-serve --listen=*:8050 mydashapp:app.server

Is waitress used wtihin the python code typically as a package? There’s little documentation explaining what waitress is and how to use it in python. Thanks

That code must be in a *.bat file. Or run it straight in the CMD of windows.

GIYF! https://docs.pylonsproject.org/projects/waitress/en/stable/

Thanks. Just to make sure that I’m clear, I run the program, first, then run this in the command lines?

Also, when I use waitress.serve in the python program, it doesn’t automatically kill the connection when I stop running the program. To kill the connection, I manually go to the CMD and kill the connection. This is not a thing with Flask because Ctrl + C kills the connection. Do you know how I can go about this?

from waitress import serve
#program for python

serve(app.server,host=host,port=1001) #this connects with the server

I have also tried:

server = create_server(app)
server.run()

But this doesn’t work as well. Any help will be appreciated!