Publishing Streamlit Application using Posit Team
Streamlit is a popular open-source framework for creating interactive web applications in Python and can easily be shared with viewers via Posit Connect. In this blog post, we’ll discuss how to publish a Streamlit app written in VS Code on Posit Workbench to Posit Connect.
Using VS Code in Posit Workbench
For this session, we are deploying a Streamlit 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.
How to Publish a Streamlit app to Posit Connect
Teams lose value, time, and money when data insights cannot be conveyed easily with decision makers. In order to make share Streamlit apps as easy as possible, we are going to publish a Streamlit app 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.
Step 1: Download the example Streamlit app into Posit Workbench
First, you’ll need to open a VS Code session within Posit Workbench and create a new directory called streamlit-demo
. Next, take all of the code for today’s session (found here) and place it within the newly created streamlit-demo
directory.
Step 2: Install the Correct Open-Source Packages and Versions
Open-source data science promotes a collective knowledge source where ideas and solutions are openly shared with other like-minded researchers and data enthusiasts. Often, these ideas are encapsulated within libraries which evolve over time. In Python, it’s common to document the currently used libraries within a file named requirements.txt
. In our streamlit-demo
directory, you’ll find such a file with various libraries and their corresponding version numbers listed. Creating this document is very easy, and entails fowarding the output of pip freeze
to a requirements.txt
file (pip freeze > requirements.txt
). Here are the first 10 lines of our requirements.txt
file:
aiofiles==22.1.0
aiohttp==3.8.4
aiosignal==1.3.1
aiosqlite==0.18.0
altair==4.2.2
anyio==3.6.2
appdirs==1.4.4
argon2-cffi==21.3.0
argon2-cffi-bindings==21.2.0
arrow==1.2.3
To make sure these libraries are installed, we can run the following command within the Terminal of VS Code. Make sure you are running this command from within the same diretory as the requirements.txt
file:
pip install -r requirements.txt
Step 3: Deploy Streamlit app to Posit Connect
The last step is to deploy this application to Posit Connect using 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 Streamlit 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 Streamlit application with the API key.
rsconnect deploy dash --server https://colorado.posit.co/rsc/ --api-key [api-key-goes-here] ./
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.
Remember to always treat your API keys like passwords!
The ./
syntax at the end of the above command tells rsconnect-python
to look in our current working directory for an app.py
file and publish it Posit Connect. Once you hit ENTER
on your keyboard, the rsconnect-python
package will validate the Connect server and making sure that the bundle looks good. If anything goes awry, it will print useful messages 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!