Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 42 ยท Dash and Deployment

Deploying Plotly and Dash Apps

Deploy a Dash app with gunicorn, Docker and a reverse proxy, and keep Plotly charts fast with scattergl, downsampling, caching, smaller figures and safe configuration.

Running python app.py on your laptop is development. A Dash app for real users must start reliably, serve many people at once, stay fast when the data grows and keep secrets out of your code. This lesson covers the steps from a working app to a deployed one, and the performance tricks that matter most for Plotly figures.

Everything about hosting itself is read-only here, because this site's Python runs in your browser and cannot start a web server. The performance part, however, is something you can run and measure below.

What happens when a Dash app runs

A Dash app is a normal Flask web app. app.server is the Flask object. The browser downloads a small JavaScript app (React), which asks /_dash-layout for the page structure and then calls /_dash-update-component every time a callback input changes. So deploying Dash is deploying a Flask app: you need a production server, not the development server.

flowchart LR
    B["Browser"] -->|HTTPS| P["Reverse proxy: nginx / Caddy / cloud load balancer"]
    P --> G["gunicorn: several workers"]
    G --> A["app.server (Flask) with your Dash callbacks"]
    A --> D[("Database / files / cache")]

The proxy handles HTTPS and static files; gunicorn runs several copies of your app.

Step 1: make the app deployable

Use app.run(debug=True) only in development. In the main file, expose the Flask object so a server can import it:

Python
# app.py
from dash import Dash, html, dcc

app = Dash(__name__)
server = app.server          # gunicorn needs this name

app.layout = html.Div([html.H1("Sales"), dcc.Graph(id="sales")])

if __name__ == "__main__":
    app.run(debug=True)      # local development only

Then add a requirements.txt with pinned versions, so the server installs exactly what you tested:

TEXT
dash==3.2.0
plotly==6.3.0
pandas==2.3.2
gunicorn==23.0.0

(Use the versions you actually run: pip freeze lists them.)

Step 2: run with gunicorn

gunicorn is the standard production server for Python web apps on Linux and macOS (on Windows use waitress).

Terminal
gunicorn app:server --workers 3 --bind 0.0.0.0:8050 --timeout 60

app:server means "module app, variable server". A rule of thumb for --workers is two times the CPU cores plus one. Each worker is a separate process with its own memory, so a global variable that one callback changes is not seen by another worker. That is the most common Dash bug in production. Dash callbacks must be stateless: keep shared state in a database, a cache such as Redis, or in the browser with dcc.Store.

Step 3: a Dockerfile

Docker packages your app and its dependencies into an image that runs the same everywhere.

DOCKERFILE
FROM python:3.13-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
ENV PORT=8050
EXPOSE 8050
CMD ["sh", "-c", "gunicorn app:server --workers 3 --bind 0.0.0.0:${PORT}"]

Build and run: docker build -t sales-dash . and docker run -p 8050:8050 sales-dash. Copying requirements.txt first lets Docker cache the slow pip install layer when only your code changes. Many hosting services (Render, Railway, Fly.io, Google Cloud Run, Azure App Service, a plain VPS) accept this image or a Procfile with the same gunicorn command.

Step 4: configuration and security

  • Secrets in the environment. Read API keys and database URLs with os.environ["DB_URL"], never from source code.
  • Authentication. Dash has no login built in. Put the app behind your company's login proxy, or use dash-auth (basic auth) for small cases, or Flask-Login on app.server.
  • HTTPS is terminated by the proxy or the hosting service.
  • Debug off. debug=True exposes a code console. Never enable it on a public server.
  • Untrusted input. Do not pass user text to eval, SQL strings or file paths. Use parameterized queries.
  • Dash's url_base_pathname (or requests_pathname_prefix) sets a sub-path when the app lives at https://example.com/dash/.

Performance: make the figure smaller

The slowest part of a data app is usually not Python: it is sending a huge figure to the browser and drawing it. Three cheap techniques give most of the benefit.

  1. scattergl instead of scatter. px.scatter(..., render_mode="webgl") or go.Scattergl draws with the GPU and stays smooth with hundreds of thousands of points. The SVG-based scatter slows down after a few thousand.
  2. Aggregate or downsample before plotting. A screen has only about 2,000 pixels across. Plotting 2 million points as a line draws almost nothing extra. Resample time series (df.resample("1h").mean()), bin 2D data (px.density_heatmap), or keep every Nth point.
  3. Remove what you do not need. Drop unused columns before px (hover data and customdata are copied into the figure), and avoid the default template if you ship many figures.
Python Measure how downsampling shrinks a figure

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Min-max decimation keeps the extremes of each bucket, so spikes remain visible while the figure shrinks by two orders of magnitude. Libraries such as plotly-resampler automate this and re-sample when the user zooms in, so a million-point series still feels instant.

A WebGL chart with many points runs fine in the browser. Here is 50,000 points with scattergl:

50,000 points with scattergl (Plotly.js)

Performance: make the server faster

  • Cache expensive work. Load and clean the data once at start-up (module level), not inside every callback. For queries use flask-caching or functools.lru_cache, keyed by the callback inputs.
  • Move heavy callbacks to the background. Dash's background=True callbacks use a queue (Celery or diskcache) so a slow job does not block a worker.
  • Partial updates. Return a Patch() to change part of a figure, or update only the needed output instead of rebuilding everything. Use prevent_initial_call=True where a callback does not need to fire at page load.
  • Clientside callbacks run JavaScript in the browser for small tasks (filtering already loaded data), with no server trip at all.
  • Fewer round trips. Combine several Inputs in one callback if the work is shared.

Alternatives to hosting a server

Not everything needs a server. A static HTML export (exporting and embedding) can be hosted for free on any static host. Notebook users can share via a notebook viewer, and Pyodide-based tools can run Python in the visitor's browser. Choose a server only when you need live data, user input that triggers computation or authentication.

Dash Enterprise, Posit Connect, Streamlit Community Cloud and others offer one-click hosting, but the basics in this lesson (a pinned requirements.txt, a production server, stateless callbacks and secrets in the environment) apply everywhere.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does app:server mean in gunicorn app:server?

    gunicorn imports the module before the colon and uses the variable after it as the WSGI application. For Dash that is app.server.
  2. 2. Why is a global variable that callbacks modify a bug in production?

    Workers do not share memory, so state belongs in a database, cache or dcc.Store.
  3. 3. Which change helps most when plotting 500,000 scatter points?

    WebGL drawing and reducing the data to what a screen can show are the two big wins.
  4. 4. Why must debug=True never be used on a public server?

    The debug console shows tracebacks and, in some configurations, lets visitors run code on your server.

Summary

  • A Dash app is a Flask app: expose server = app.server and run it with gunicorn behind a proxy.
  • Pin dependencies, package with Docker if you like, keep secrets in environment variables and never deploy with debug on.
  • Callbacks must be stateless because workers do not share memory.
  • For speed, send fewer points (scattergl, aggregation, decimation) and cache expensive work.
  • Prefer static HTML export when no live computation is needed.

Next: Project: A Sales Dashboard