Using Dash in Jupyter and Workspaces

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.

This page applies to Dash Enterprise 5. If your organization uses Dash Enterprise 4, view the Workspaces docs at https://<your-dash-enterprise-server>/Docs/workspaces.

JupyterDash is a library that makes it easy to build Dash apps from Jupyter environments.
In this chapter, we demonstrate using JupyterDash in Dash Enterprise Workspaces, including:

Installing JupyterDash

Install JupyterDash with pip:

$ pip install jupyter-dash

Creating a Dash App with JupyterDash

Standard Dash apps use dash.Dash to create a Dash app instance.
In JupyterDash, you’ll need to replace this with the jupyter_dash.JupyterDash subclass.

dash.Dash

<img>

jupyter_dash.JupyterDash

<img>

Serving your Dash App

Serve your Dash app using run_server:

<img>

Unlike the standard Dash.run_server method, the JupyterDash.run_server method doesn’t block execution of the notebook.
It serves the app in a background thread, making it possible to run other notebook calculations while the app is running.
This means you can update the app without rerunning the potentially expensive data processing steps.

Once you’ve added run_server to your Dash app, run the notebook by selecting Restart & Run All
<img> in the Notebook Editor toolbar.

<img>

View in New Browser Tab or Window

By default, run_server runs in external mode, displaying a URL that you can follow to view the app.
The URL is in the format https://&lt;your-dash-enterprise-server&gt;/workspace/view/workspace-&lt;your-app-name&gt;/.
Running the app with run_server() is equivalent to running it with run_server(mode=external).

View in Preview Window

You can also view your app in a tab within the workspace by running your notebook and selecting Preview.

<img>

The app loads in a preview panel:

View Inline

You can also view the running app directly in a Jupyter Notebook output cell by setting mode="inline" on run_server:

app.run_server(mode="inline")

Deploying a Dash App inside Jupyter

To deploy your Dash app:

  1. Add the following launch.sh and Procfile files to your project.
    The launch.sh script converts your notebook to a .py file to deploy and the Procfile tells Dash Enterprise which processes to run on startup.
    Be sure to create the Procfile without a file extension.

launch.sh

```sh
#!/bin/bash

# Name of notebook to deploy (excluding .ipynb extension)
<name>=app

# Convert notebook to py script
jupyter nbconvert –to script $NAME.ipynb

# Depending on notebook metadata, command above may output a .txt file
# If so, change extension to .py
if [ -f $NAME.txt ]; then
mv $NAME.txt $NAME.py
fi
`` where<name>is the name of your Jupyter notebook, excluding the.ipynb` extension.

Procfile
web: gunicorn &lt;name&gt;:server --workers 2
where &lt;name&gt; is the name of your Jupyter notebook, excluding the .ipynb extension.

  1. In your notebook, define the server variable for Gunicorn:
    server = app.server

  2. Create a requirements.txt file for your app dependencies with pip freeze > requirements.txt; then go in requirements.txt and add nbconvert and gunicorn.

  3. Run the launch.sh script in the workspace terminal:

./launch.sh

If you get an error “setuidgid: fatal: unable to run ./launch.sh: access denied”, then make the script executable and run it again:

bash chmod +x launch.sh

  1. Select Deploy.

Note: You’ll need to run the launch.sh script any time you make changes in the Jupyter Notebook
and want to deploy those changes.

More about JupyterDash