Production and development enviroments

Good evening everyone I also have a similar case. I am new to dash and server creation. I had to create a plot with Dash (the code follows) and I would like to have it in my already created Appache server.

import dash
from dash.dependencies import Output, Input
import dash_core_components as dcc
import dash_html_components as html
import plotly
import random
import plotly.graph_objs as go
from collections import deque
import pandas as pd

X = deque(maxlen=20)
X.append(0)
Y = deque(maxlen=20)
Y.append(1)

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

app = dash.Dash(name, external_stylesheets=external_stylesheets’)
server = app.server

app = dash.Dash(name)
app.layout = html.Div(
[
dcc.Graph(id=‘live-graph’, animate=True),
dcc.Interval(
id=‘graph-update’,
interval=1*1000
),
]
)

@app.callback(Output(‘live-graph’, ‘figure’),
[Input(‘graph-update’, ‘n_intervals’)])
def update_graph_scatter(input_data):
Daten = pd.read_csv(“/data_probe.csv”)
X = Daten[“zeit”]
Y = Daten[“i”]

data = plotly.graph_objs.Scatter(
        x=list(X),
        y=list(Y),
        name='Scatter',
        mode= 'lines+markers'
        )


return {'data': [data],'layout' : go.Layout(xaxis=dict(range=[max(X)- 50,max(X)]),
                                            yaxis=dict(range=[min(Y),max(Y)]),
                                            title = "Verlauf",
                                            
                                            )}

if name == ‘main’:
app.run_server(debug=True)

Gotta say this thread is great! GUIUnicorn works. Flask sucks

The warning from Flask is there to prevent you from using the development server in production. Indeed, it is not very efficient or secure because it includes a set of functionality for developing and debugging (such as the debugger and reloader). You can find a list WSGI server you can use at Deploying to Production — Flask Documentation (3.0.x). We will dwell into details on using Waitress which is a very easy to setup solution with good performances:

First, you need to install Waitress on your virtual environment with:

pip install waitress

Then you will need to launch your app. This will depend on where your app is created. This is generally done in a Python file close to the root with a line that looks like:

app = flask.Flask(__name__)

(you need to find where Flask is called)

Let’s say this line is in a file named flask_app.py at the location: /projects/flask_deployement/src/flask_app.py

Move to the project root with cd /projects/flask_deployement, noting that the name of the variable running flask was app, we will send the location of the app to Waitress with the path: src.flask_app:app

If we then run Waitress on our server, on port 5000 (this is an example, you can take any available port), we will run the command:

waitress-serve --host 127.0.0.1 --port 5000 src.flask_app:app

Your app is now running on port 5000. If you have a page on the root endpoint, you can test that the app runs successfully with: curl <http://127.0.0.1:5000/

If you want help with the whole process of deploying a Flask app in production on a server, I have written a whole tutorial here

Hello @ThomasV,

Welcome to the community!

waitress is a valid way to deploy the server, however, it is important to note the differences between waitress and gunicorn when hosted.

As well as we should point out the need for the dash app to expose the flask server after it has been init. eg: server = app.server in most cases.

With most generic hosting, this is done via gunicorn vs waitress as such the default server is :8000 as well.

1 Like