Dash App Structure

This documentation is for Dash Enterprise.
Dash Enterprise is the fastest way to write & deploy Dash apps and
Jupyter notebooks.
10% of the Fortune 500 uses Dash Enterprise to productionize AI and
data science apps. Find out if your company is using Dash Enterprise.

A few key files are required for your Dash app to successfully deploy on Dash Enterprise. In addition to app.py, you’ll need a requirements.txt file to describe your app’s dependencies and a Procfile to declare what commands and processes should be run to run the Dash app and any other background processes.

The project structure for a Dash app containing only these required files looks like:

|-- app.py
|-- Procfile
|-- requirements.txt

You may also include optional files to further configure your app deployment, such as an Aptfile if your app requires additional system-level packages like database
drivers or extra tools in Workspaces.

A more complex project structure might look like:

|-- app.py
|-- Procfile
|-- requirements.txt
|-- project.toml
|-- Aptfile

A few key files are required for your Dash app to successfully deploy on Dash Enterprise. In addition to app.py, you’ll need an environment.yml file to describe your app’s dependencies and a Procfile to declare what commands and processes should be run to run the Dash app and any other background processes.

The project structure for a Dash app containing only these required files looks like:

|-- app.py
|-- Procfile
|-- environment.yml

You may also include optional files to further configure your app deployment, such as an Aptfile if your app requires additional system-level packages like database
drivers or extra tools in Workspaces.

A more complex project structure might look like this:

|-- app.py
|-- Procfile
|-- environment.yml
|-- project.toml
|-- Aptfile

If your project includes an environment.yml file, Dash Enterprise automatically uses the Conda buildpack,
unless your project also contains a requirements.txt file.
If your project has a requirements.txt file, Dash Enterprise always uses the pip buildpack, even if your
project also has an environment.yml file.

Required Files

app.py

By convention, this is usually called app.py or index.py. This file is
called by the command you specify in your Procfile. It contains your Python
code and must be placed in your project’s root directory.

If your app uses Dash 3.0.4 or earlier, this file must also contain a line that defines the server variable so that it can be exposed for the Procfile:

from dash import Dash

app = Dash()
server = app.server
...

requirements.txt or environment.yml

Describe your app’s Python dependencies in a requirements.txt file if you want Dash Enterprise to use the pip buildpack, or describe them in
an environment.yml file if you want Dash Enterprise to use the Conda buildpack.

Note that adding both requirements.txt and environment.yml to your app currently causes Dash Enterprise to detect only the pip buildpack, so it
is not possible to use both sets of features from these files in the same app.

We recommend using the pip buildpack because it allows for faster app deployments. Conda’s dependency resolver increases the time it takes to install dependencies during development and deployment (15 mins compared to 7 mins).
Use Conda if mandated by your organization or if your app requires packages that cannot be installed with pip, like cupy or cuda.

The Conda buildpack is not currently compatible with internet-restricted Dash Enterprise instances.

Dash Enterprise uses pip to install the Python packages listed in your requirements.txt file.

A basic requirements.txt file looks like this:

dash-design-kit==1.6.7
dash==2.4.1
gunicorn==20.0.4
pandas==1.1.4

You’ll be declaring gunicorn as your Dash app’s web server in your Procfile, so don’t forget to include gunicorn in your app’s dependencies. If your app uses a FastAPI or Quart backend, include dash[fastapi] or dash[quart] and uvicorn instead of gunicorn.

We recommend including all the Python packages that your app requires, as well as their version numbers, in your requirements.txt.

You can inspect the packages and version numbers that are currently in your development environment
by running pip freeze in your terminal.
This includes all subdependencies of the packages that your app relies on.
For example, dash itself relies on 17 other packages that have their own version numbers.

If you don’t include the subdependencies in your requirements.txt file, then
Dash Enterprise installs the latest versions of these subdependencies when you
deploy your app for the first time. This could mean that your deployed app
uses different versions of packages than your development environment, which could
cause unpredictable errors.

If you’re developing your app locally, remember that certain versions of packages that you install on
Windows may not be available on the Linux containers that Dash Enterprise uses. The best way to avoid cross-operating system package
issues is to develop your app using Dash Enterprise Workspaces.

We recommend generating and populating your requirements.txt with:

pip freeze > requirements.txt

The following example file was generated by running pip freeze > requirements.txt
for an app that used the latest versions of dash, gunicorn, pandas, dash-design-kit, and dash-snapshots in June 2022.

View example requirements.txt generated with pip freeze

amqp==5.1.1
asn1crypto==1.5.1
billiard==3.6.4.0
Brotli==1.0.9
celery==5.2.6
certifi==2022.5.18.1
charset-normalizer==2.0.12
click==8.1.3
click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.2.0
dash==2.4.1
dash-core-components==2.0.0
dash-design-kit==1.6.7
dash-enterprise-auth==0.0.5
dash-html-components==2.0.0
dash-snapshots==2.2.0
dash-table==5.0.0
Flask==2.1.2
Flask-Compress==1.12
Flask-SQLAlchemy==2.5.1
future==0.18.2
greenlet==1.1.2
gunicorn==20.1.0
idna==3.3
importlib-metadata==4.11.4
itsdangerous==2.1.2
Jinja2==3.1.2
kombu==5.2.4
MarkupSafe==2.1.1
numpy==1.22.4
packaging==20.9
pandas==1.4.2
pg8000==1.29.1
plotly==5.8.0
prompt-toolkit==3.0.29
PyJWT==2.4.0
pyparsing==3.0.9
python-dateutil==2.8.2
pytz==2022.1
redis==3.5.3
requests==2.27.1
retrying==1.3.3
scramp==1.4.1
six==1.16.0
SQLAlchemy==1.4.36
tenacity==8.0.1
urllib3==1.26.9
vine==5.0.0
wcwidth==0.2.5
Werkzeug==2.1.2
zipp==3.8.0

 

Adding private packages

You can supply additional options at the top of the file. See the
Adding Private Packages chapter and the
official pip requirements.txt docs
for more details.

 

Requirements.txt with redeploys

Redeploying your app does not reinstall dependencies unless you have changed
the requirements.txt file.

If you are including tarball files of Python packages, the filenames need to change
for Dash Enterprise to reinstall the packages. If you are developing and testing your own Python
packages, we recommend using increasing pre-release version numbers (e.g. rc-1) when you deploy
changes to your package.

Dash Enterprise uses Conda to install the packages listed under dependencies in environment.yml. The Conda buildpack uses Miniconda to create a Conda environment and install these dependencies.
This dependencies list supports a nested list of dependencies to be installed with pip.

List packages that are not available on Conda channels—notably all Dash Enterprise packages—under pip. In the example environment.yml file below, dash-design-kit is installed with pip.

The channels section of the environment.yml file is where you add Conda channels to search for packages.
The following example has conda-forge, which is a Conda community-led collection of packages.

name: my-conda-env
channels:
  - conda-forge
dependencies:
  - dash==2.4.1
  - gunicorn==20.0.4
  - pandas>=1.1.5
  - pip:
      - dash-design-kit==1.6.7

You’ll be declaring gunicorn as your Dash app’s web server in your Procfile, so don’t forget to include gunicorn in your app’s dependencies.

You can inspect the packages and version numbers currently used in your development environment by running conda list in your terminal.

If you’re developing your app locally, remember that certain versions of packages that you install on Windows may not be available on the Linux containers
that Dash Enterprise uses. We recommend only including the packages that your app imports in environment.yml and letting Conda handle subdependencies.
For example, adding pandas to your environment.yml lets Conda install the appropriate versions of Pandas subdependencies, which may differ per operating system.

The best way to avoid cross-operating system package issues is to develop your app using Dash Enterprise Workspaces.

If you’re developing your app in Workspaces, we recommend including all Python packages, their subdependencies, and their version numbers in your
environment.yml file. To quickly get this complete package information, export your Conda environment to an environment.yml file by running the
following command in the workspace IDE:

conda env export > environment.yml

The following example file was generated by running conda env export > environment.yml
in an environment that used dash, pandas, gunicorn, and dash-design-kit in June 2022.

Note: Conda is not automatically available in a workspace. You’ll need to rebuild
your workspace after adding an environment.yml file to install packages with Conda.

View example environment.yml generated with conda env export

name: example-yml-file
channels:
  - defaults
  - plotly
  - conda-forge
dependencies:
  - brotli-python=1.0.9=py38h038c8f4_7
  - ca-certificates=2022.5.18.1=h033912b_0
  - click=8.1.3=py38h50d1736_0
  - dash=2.4.1=pyhd8ed1ab_0
  - flask=2.1.2=pyhd8ed1ab_1
  - flask-compress=1.12=pyhd8ed1ab_0
  - gunicorn=20.1.0=py38h50d1736_2
  - importlib-metadata=4.11.4=py38h50d1736_0
  - itsdangerous=2.1.2=pyhd8ed1ab_0
  - jinja2=3.1.2=pyhd8ed1ab_0
  - libblas=3.9.0=14_osx64_openblas
  - libcblas=3.9.0=14_osx64_openblas
  - libcxx=14.0.4=hc203e6f_0
  - libffi=3.4.2=h0d85af4_5
  - libgfortran=5.0.0=9_3_0_h6c81a4c_23
  - libgfortran5=9.3.0=h6c81a4c_23
  - liblapack=3.9.0=14_osx64_openblas
  - libopenblas=0.3.20=openmp_hb3cd9ec_0
  - libzlib=1.2.12=h6c3fc93_0
  - llvm-openmp=14.0.4=ha654fa7_0
  - markupsafe=2.1.1=py38hed1de0f_1
  - ncurses=6.3=h96cf925_1
  - numpy=1.22.4=py38h3ad0702_0
  - openssl=3.0.3=hfe4f2af_0
  - pandas=1.4.2=py38h2b30649_2
  - pip=22.1.2=pyhd8ed1ab_0
  - plotly=5.8.0=pyhd8ed1ab_1
  - python=3.8.12=h43ca1e7_3_cpython
  - python-dateutil=2.8.2=pyhd8ed1ab_0
  - python_abi=3.8=2_cp38
  - pytz=2022.1=pyhd8ed1ab_0
  - readline=8.1=h05e3726_0
  - setuptools=62.3.2=py38h50d1736_0
  - six=1.16.0=pyh6c4a22f_0
  - sqlite=3.38.5=hd9f0692_0
  - tenacity=8.0.1=pyhd8ed1ab_0
  - tk=8.6.12=h5dbffcc_0
  - werkzeug=2.1.2=pyhd8ed1ab_1
  - wheel=0.37.1=pyhd8ed1ab_0
  - xz=5.2.5=haf1e3a3_1
  - zipp=3.8.0=pyhd8ed1ab_0
  - zlib=1.2.12=h6c3fc93_0
  - pip:
    - dash-core-components==2.0.0
    - dash-design-kit==1.6.7
    - dash-html-components==2.0.0
    - dash-table==5.0.0

 

Configuring the Python version

By default, Miniconda uses Python 3.13. You can use a different version of Python in your deployed app by adding it to your environment.yml dependencies. For example, to use Python 3.10:

name: conda-app
channels:
  - conda-forge
  - defaults
dependencies:
  - python==3.10
  - dash==2.3.1
  - gunicorn==20.0.4
  - pandas>=1.1.5
  - pip:
      - dash-design-kit==1.6.7

 

environment.yml with redeploys

Redeploying your app does not reinstall dependencies unless you have changed
the environment.yml file.

 

Additional pip options

You can supply additional pip options in the pip section of the environment.yml file.

For example, you can:
- Add extra-index-urls.
- Install pip requirements from another file.
- Install a local package.

Here is an example of setting advanced pip options within an environment.yml file.

Procfile

A Procfile is a required text file that tells Dash Enterprise which processes to run on
startup, like starting your app’s web server, scheduling jobs, and running background processes.

This file is always named Procfile—with no file extension. It must be placed in your app’s root
directory and uses the following format:

<process>: <command>

 

Basic Procfile Example

A simple Dash app Procfile with Dash 3.1.0 and later looks like this:

web: gunicorn app:app --workers 4

In this example, the first app is the name of the file that has your Dash app instance. The second app is the variable name that the Dash app instance in that file is assigned to.

When using Dash 3.0.4 and earlier, you need to expose a server variable (see the app.py section earlier for more details), so the Procfile will look like this:

web: gunicorn app:server --workers 4

In this example, the first app is the name of the file that has your Dash app instance. server is the variable name that the Dash app instance’s server is assigned to in that file.

If your app uses a FastAPI or Quart backend, use uvicorn instead of gunicorn:

web: uvicorn app:server --host 0.0.0.0 --port 8000

Where app is the name of your Python file and server is the FastAPI or Quart server variable. See the server backends deployment sections for more details.

 

Background Task Dash App Example

A Dash app running a background task queue might have a Procfile similar to this when using Dash 3.1.0 and later:

web: gunicorn app:app --workers 4
worker: celery -A app:celery_instance worker

Or similar to this in earlier versions of Dash:

web: gunicorn app:server --workers 4
worker: celery -A app:celery_instance worker

Note: Process type names are arbitrary except for web—a special process.
In the example above, worker could be anything, but we recommend using descriptive names as they appear in logs.

A Dash app periodically generating reports with the Snapshot Engine might have a
Procfile like this:

web: gunicorn index:<your> --workers 4
worker: celery -A index:celery_instance worker --concurrency=2
scheduler: celery -A index:celery_instance beat

In this example, index in the commands refers to the file that has the app or server variable, as well as the celery_instance variable.

A Dash app whose app.py file is not located at the root, but rather inside a folder, might have a Procfile like this:

web: gunicorn app:<your> --workers 4 --chdir <folder_name>

More on Web Processes

web is the only process type that can receive external HTTP traffic. Use this
to run your Dash app with gunicorn, your Dash app’s web server. In the following
examples, we declare gunicorn as our web server with web: gunicorn:

Example with Dash 3.1.0 and later

web: gunicorn app:app --workers 4 --preload

app refers to a file named app.py and app refers to the variable named app
inside that file (see the app.py section in this document). If the entry point to your
app is index.py instead of app.py, then this would be web: gunicorn index:app --workers 4 --preload.

Example with Dash 3.0.4 and earlier

web: gunicorn app:server --workers 4 --preload

app refers to a file named app.py and server refers to the variable named server
inside that file (see the app.py section in this document). If the entry point to your
app is index.py instead of app.py, then this would be web: gunicorn index:server --workers 4 --preload.

This command is a standard gunicorn command
used to run your Dash app. It’s the production alternative to running your app with python app.py.

gunicorn accepts a wide variety of settings.
Here are a few common flags:

```
web: gunicorn app:<your> –workers 4

```

About choosing the number of workers: The Gunicorn documentation recommends a worker count that is based on the number of CPU cores on the server. However, because Dash Enterprise is a platform that serves many apps—each with their own Procfile—this recommendation doesn’t directly apply. In practice, Plotly has found that individual apps only need between 2 and 4 workers. A test app might use 2 workers, while a production app with higher expected traffic might use 4. Warning: A very high number of workers can lead to resource thrashing issues at the system level.

Tip: Another way to allow your app to serve more concurrent users is to configure the autoscaling settings for the Web process. Learn more in Scaling Your App.

web: gunicorn app:&lt;your&gt; --workers 4 --preload

web: gunicorn app:&lt;your&gt; --workers 4 --timeout 240

See the Gunicorn docs on timeout for more information.

Do not increase the --timeout to resolve an issue with a callback taking more than 30 seconds to execute; use

project.toml is an optional file. To use it, place it in your app’s root directory. project.toml allows you to:
- Set environment variables that are used by your app at build time. For example:

  • Specify a pip version for Dash Enterprise to use when building your apps. The default pip version used if you don’t specify another one in a project.toml file is 26.0.1.

Note: If you specify a pip version using environment variables in your app Settings as well as in a project.toml file, the value in the Settings takes precedence.

Note: In earlier versions of Dash Enterprise, overriding the default Python was also done with project.toml. It is now done in the Dash Enterprise App settings.
- Run predeploy and postdeploy scripts.

Pip Version

To change the pip version, specify the version you want to use in a project.toml.

A project.toml file to use pip 23.1 looks like this:

[build]
  [[build.env]]
    name = 'BP_PIP_VERSION'
    value = '23.1.*'

Dash Enterprise supports the following pip versions for deployed apps:

  • pip 26.2.1
  • pip 26.2.0
  • pip 26.0.1 (default)
  • pip 26.0.0
  • pip 23.3.2
  • pip 23.3.1
  • pip 23.3.0
  • pip 23.1.2
  • pip 23.1.1
  • pip 21.2.4
  • pip 21.2.3

Predeploy and Postdeploy Scripts

Add predeploy and postdeploy scripts you want to run in your app’s environment to your project.toml file.

Use predeploy scripts to install additional system dependencies, or download
files in your app or worker if that needs be done as part of app
startup or restart. This can be better than doing it in your app or as part
of your Procfile command as it is only run a single time rather than once per
container.

Use postdeploy scripts to signal that the deployment has finished.
Note that the containers are started after the postdeploy script is run, so
if your app and workers are not immediately available, they probably need the postdeploy script to complete.

If the postdeploy scripts creates files, those files aren’t accessible when
the containers are run. So, we don’t recommend using postdeploy scripts to download, cache,
or process data files in your app or worker.

Example of adding predeploy and postdeploy scripts:

[scripts]
predeploy = "predeploy.sh"
postdeploy = "postdeploy.sh"

Dash Enterprise executes files referenced under predeploy and postdeploy in the project.toml file as Bash scripts.
If you want to run a Python script, add a Bash script to your project that runs the Python script.

predeploy.sh

python predeploy.py

And then add the Bash script name in the project.toml file:

[scripts]
predeploy = "predeploy.sh"

The above example runs predeploy.sh, which then runs python predeploy.py.

Learn more in the Deployed App Environment chapter.

project.toml is an optional file that allows you to run predeploy and postdeploy scripts.

The project.toml file must be in your app’s root directory.

Use predeploy scripts to install additional system dependencies, or download
files in your app or worker if that needs be done as part of app
startup or restart. This can be better than doing it in your app or as part
of your Procfile command as it is only run a single time rather than once per
container.

Use postdeploy scripts to signal that the deployment has finished.
Note that the containers are started after the postdeploy script is run, so
if your app and workers are not immediately available, they probably need the postdeploy script to complete.

If the postdeploy scripts creates files, those files aren’t accessible when
the containers are run. So, we don’t recommend using postdeploy scripts to download, cache,
or process data files in your app or worker.

Example of adding predeploy and postdeploy scripts:

[scripts]
predeploy = "predeploy.sh"
postdeploy = "postdeploy.sh"

Dash Enterprise executes files referenced under predeploy and postdeploy in the project.toml file as Bash scripts.
If you want to run a Python script, add a Bash script to your project that runs the Python script.

predeploy.sh

python predeploy.py

And then add the Bash script name in the project.toml file:

[scripts]
predeploy = "predeploy.sh"

The above example runs predeploy.sh, which then runs python predeploy.py.

Learn more in the Deployed App Environment chapter.

.gitignore

.gitignore is a text file that determines which files and folders are ignored by Git.
Files listed in the .gitignore are not copied to the server when you deploy your app.

venv
*.pyc
.DS_Store
.env

Aptfile

An Aptfile can be used to install system-level packages via the APT package manager. It supports:

If an Aptfile is included in your project, an apt-get update is triggered before installing the packages.

 

Packages from standard APT repositories

Packages from APT repositories might include database drivers that your app requires.
In the following example we are installing unixodbc and unixodbc-dev:

unixodbc
unixodbc-dev

 

Specific .deb files

You can also add links to specific .deb files to be installed:

<a href="https://downloads.example.com/example.deb">https://downloads.example.com/example.deb</a>

 

Custom APT repositories

Your Aptfile can also contain additional APT
repositories:

:repo:deb <a href="https://apt.example.com/">https://apt.example.com/</a> example-distro main

Important: Dash Enterprise requires that custom APT repositories have a TLS/SSL certificate from a globally trusted certificate authority (CA).

To use APT packages in your apps when Dash Enterprise is internet-restricted, you need to specify a custom APT repository that Dash Enterprise has network access to. See an example in
Developing Apps when Dash Enterprise Is Internet-Restricted.

Additional Resources