Deploying a Dash application with Posit Connect

2023-04-26
Text: "Deploying a Dash application with Posit Connect"

This is part 2 in our series of blog posts, “What can you do with Posit Connect?”

Posit Connect is a publishing platform for the authenticated sharing of R and Python data products in one convenient place that brings the power of data science to your entire organization. On the last Wednesday of every month, we host a Posit Team demo and Q&A session that is open to all. You can use this link to add the event to your calendar.

This month’s end-to-end data science workflow demonstrates publishing a Dash application using the three products that make up Posit Team:

  • Posit Workbench (creating your Dash app in VS Code)
  • Posit Package Manager (installing packages from the latest pypi repo)
  • Posit Connect (deploying your Dash app with the rsconnect-python package)

Dash applications make it easy to transform your analysis into an interactive dashboard using Python so users can ask and answer questions in real time without touching any code. Written on top of Flask, Plotly.js, and React.js, Dash is useful for building data visualization apps with highly custom user interfaces in pure Python.

 

Follow along

The Posit Solutions Engineering GitHub repository contains examples of Python content you can deploy to Connect, including the Dash example covered in the April 26th Posit Team Demo.

The Dash application in this example is one of the JumpStart examples included in Posit Connect.  JumpStart examples are short walkthroughs built into Connect and show you how to deploy various types of applications.

If the JumpStart examples do not pop up immediately when you log in to Connect, you can access the JumpStart examples in the top right-hand corner by clicking the Publish dropdown.

Front page of a Connect server showing the dropdown for JumpStart examples.

When you click on the JumpStart examples, select Python at the top to see a variety of example applications. Select the Dash Stock Pricing Dashboard, which you’ll get to explore once we actually deploy it.

Connect's JumpStart examples for Python, showing Jupyter Notebook, Dash app, Streamlit app, and Bokeh app options.

Click, “Get Started using ‘Stock Pricing Dashboard” to download the files.

 

Using VS Code in Posit Workbench

For this session, we are deploying the Dash app from a VS Code instance in Posit Workbench. 

Posit Workbench is the place for teams to collaboratively build open-source data science projects at scale. It supports R and Python, giving data scientists access to all the development environments they love, including RStudio, Jupyter Notebook, JupyterLab, and VS Code. Workbench provides enterprise-friendly features, such as centralized management, security, and commercial support. VS Code is a great all-purpose editor, and it is the IDE you will use today.

First screen when you open VS Code.

Create a blank directory in VS Code called “QUICK_START.” This is where you will house your Dash application.

Open the Jumpstart example in VS Code by grabbing the file you just downloaded, and dragging it over to your “QUICK_START” project directory.

Open the terminal within VS Code and unzip the file:

unzip stock-dashboard-python.zip

It will unzip the contents into a separate directory called “stock-dashboard-python.” To navigate into that directory, run this in the terminal:

cd stock-dashboard-python

Open the app.py file, which is the app you will run and deploy in Posit Connect:

VS Code with the app.py file open.

 

Install packages from Package Manager

 

Install required packages that live in the requirements.txt file using pip:

pip install -r requirements.txt

These packages are being installed from our demo Posit Package Manager instance. Package Manager organizes and centralizes data science packages across your team, department, or entire organization. Packages can come from a variety of sources including, but not limited to: CRAN, PyPI, Bioconductor, GitHub, and internally developed package sources.

Your system administrators or IT team use Package Manager to help set up the package environment for Posit Workbench, Posit Connect, and your developers. Posit Package Manager also has a user interface that developers can log into and see what packages are available to them.

Posit Package Manager list of available packages.

In this example, we are focused on the Python repositories. There are currently two in this instance: pypi and a local-python repo for any of your own Python packages that you want to easily share with your team for them to use in their projects.

If you go to the Setup tab, you will see the specific URL to receive the latest source packages from Package Manager.  When you go to install a package, you can see in the path here it says, “/latest”,  this means you’re always going to pull in the latest and greatest version of the package.

However, maybe you have a project that you worked on a year ago and you want to go back into that project and install packages as they were at that time. That’s where the calendar below comes into play. The URL will change to reflect the date you select in its path.

Posit Package Manager calendar for packages.

Heading back over to VS Code, you can confirm in the highlighted text below that it is pulling from the repo snapshot that you wish.

VS Code with highlighted info on what repo is being pulled from (in this case, the latest repo).

 

Deploy your application to Posit Connect

Posit Connect is a publishing platform for the authenticated sharing of data products. With Connect, it’s easy to share Shiny applications, Jupyter Notebooks, Quarto and R Markdown reports, Plumber and Flask APIs, Dash, Bokeh, Streamlit applications, Quarto projects, dashboards, plots, and more in one convenient place to bring the power of data science to your entire organization.

The last step is to deploy this application. You will use the rsconnect-python package. Install it by running:

pip install rsconnect-python

You will need to create and use an API key in order to deploy your Python application. This is important because when you deploy to Posit Connect using the rsconnect-python package, Connect needs to know who’s actually trying to deploy it to make sure you have the necessary credentials and privileges to do so. This is the command you’re going to use to deploy the example Dash application where you can provide your API key.

rsconnect deploy dash --server https://colorado.posit.co/rsc/ --api-key  ./

So where do you find this API key? In your Posit Connect instance, click on your name in the top right-hand corner and you will see “API Keys.” After clicking on this, you can create a brand new API key that you will copy over to VS Code and paste into the command above.

Posit Connect page with API key option.

Friendly reminder to always treat API keys just like you would any passwords to make sure they’re safe.

When you hit Enter and run the command, rsconnect-python will automatically detect the app.py application in your directory and deploy it.

It will go through a few checks, like validating the server and making sure that the bundle looks good. If anything is not in order, it will print some nice useful messages for you to help troubleshoot.

But what’s actually happening as you deploy an application? It captures your environment including what Python version you are using, what libraries you are using, and what versions of the libraries you are using- and it’s making sure Connect has that available to replicate your environment exactly as it is in VS Code.

You did it, you’ve deployed your Dash application!

If you jump over to Posit Connect, you’ll notice as a publisher that you have some additional controls. 

The most important thing will first be sharing it with those that need to see it. That’s going to be under access controls where you can define individual users or groups of users that should be able to see this application. Tight control over who can view this is great, especially if you have any sensitive data. 

Posit Connect authentication options.

A few other options as a publisher include creating a customized URL, control of the compute resources to handle more concurrent users, scheduling content to run, tags for organizing your content, and environment variables for supplying sensitive information like tokens, passwords, etc.

This example covered publishing a Dash application to Posit Connect, but you have many options when it comes to data products that you can deploy to Connect. 

With Connect, it’s easy to share Shiny applications, Jupyter Notebooks, Quarto and R Markdown reports, Plumber and Flask APIs, Dash, Bokeh, Voilà, Streamlit applications, Quarto projects, dashboards, plots, and more in one convenient place to bring the power of data science to your entire organization.

 

Learn more

We’d love to have you join us at one of our monthly Posit Team Demos where we walk through a different end-to-end data science workflow each month. They are on the last Wednesday of every month at 11 am ET on YouTube. You can add the event to your calendar here: pos.it/team-demo

If you have any questions and would like to talk with our team, you can schedule a time to chat here.