Streamlit Tutorial: How to Deploy Streamlit Apps on RStudio (Posit) Connect


Streamlit Tutorial on RStudio (Posit) Connect

At Appsilon we specialize in R and Shiny, but as data scientists and ML engineers we also know our way around Python. In our recent Streamlit tutorial series, we created a Streamlit application from scratch. If you completed the tutorial you might’ve noticed it was deployed using RStudio (Posit) Connect. At the time of writing this post, RStudio was in transition for rebranding including products such as Posit Connect – we’ll refer to it here as RSConnect/RStudio Connect.

RStudio Connect provides strong support for Streamlit out-of-the-box. In this article, we’ll provide a walk-through from start to finish and give some tips for error debugging.

Curious to know if Python or R is better for your project? We’ve explored the topic in detail in these blogs:

Table of contents:


RStudio (Posit) Connect and Python

It should be noted that RStudio is not all about R. It’s very useful for Pythonistas as well! Check out RSConnect documentation on deploying apps in:

  • Flask
  • FastAPI
  • Bokeh
  • Dash
  • Jupyter notebooks
  • Streamlit

Like most Data Science teams, RStudio understands the value Python and R can provide in different contexts and when leveraged together. That’s why the RStudio team has been focusing on creating the ultimate open-source data science ecosystem with the RStudio suite. You can now merge your projects and teams seamlessly using R and Python with:

  • RStudio (Posit) IDE
  • RStudio (Posit) Workbench
  • RStudio (Posit) Connect
  • RStudio (Posit) Package Manager

If you’d like to know how RStudio’s products can be better leveraged for your enterprise contact us. Appsilon is a proud RStudio Full Service Certified Partner.

Streamlit

If you haven’t heard of Streamlit before, you may want to check out the third post in our series on creating Streamlit applications. Streamlit is a fairly new framework for data science web applications that has taken the data science world by storm. It allows creating interactive dashboards with ease to show the results of your analyses or models’ predictions. It’s quickly becoming a must-have tool for your data science workbench.

Deploying Streamlit Using RStudio (Posit) Connect

Deploying your Streamlit application is pretty straightforward. In the conda/pipenv environment in which you run Streamlit application install rsconnect-python by running pip install rsconnect-python. Suppose that your application is located in MyApplication directory and main application script is written in MyApplication/main.py file:

  1. Go to MyApplication directory and run streamlit run main.py to check if everything works as planned.
  2. Prepare requirements.txt file by running, e.g., pip freeze > requirements.txt.
  3. Go to the parent directory (with respect to MyApplication folder) and publish the application using:

If you’re lucky, you will see a message like:

If so, congratulations! You’ve successfully published your application.🎉🎉🎉

Solving Potential Deployment Problems

Unfortunately, simple as it may seem, there’s a good chance you’ll run into some problems. But don’t worry, we’re here to help you with this tutorial. Read on to see potential issues and ways of resolving them.

RStudio Connect Version

The possibility to deploy a Streamlit application has been added in RStudio Connect 1.8.4. Check if you satisfy the minimum version requirement by going to the Documentation tab in RSConnect.

 

 

It might be confusing at first but version 1.9.0.1 is followed by version 2021.08.0. Starting from this point in time, RSConnect version names follow the year.month.minor convention.

Python Version

Another problem you may encounter is that your local Python version doesn’t match the RSConnect version. In the Documentation section, you can find available Python versions (see picture above). Be sure to use version matching RSConnect. You can do this by creating an appropriate environment with conda create -n myenv python==3.9.5 if you use conda.

Pip Installation Error

This error can be quite vague. What might help here is to request RSConnect to not use any caches. Instead, you should build the app again. You can force it by adding a --new flag. Once you’ve deployed your app successfully and just want to add some features, fix typos, etc. omit the --new flag.

File Not Found Error

 

Ok, so your application is up and running! You open it only to discover that some files are missing or there is a problem with paths. We have a potential solution for you! Write all paths in the application with respect to main.py – the script would be inside MyApplication folder.

Stuck at Uploading

So you feel like you’ve done everything right but your rsconnect-python is stuck on this single line for ages and doesn’t respond. Don’t worry! Just take a moment to make some tea or coffee. Your application is being uploaded and it needs some time, especially if you want to upload a big model.pkl. Unfortunately, there is no progress bar yet. Keep in mind that if your RSConnect server is accessible only through the VPN, upload speed might be significantly lower than if you were to do it outside of the VPN.

A simple trick to debug environment-related issues first is to create an environment with all the libraries and use it to upload a simple Hello World application. If it works, you can proceed and upload the entire app with model.pkl. This way you can avoid the trap of having to wait 20 minutes for the application to upload, only to discover that the Python versions don’t match.

Building the Environment

Try to create a fresh environment and install only the packages required to run your Streamlit application. Create a requirements file with pip freeze > requirements.txt and only then install the rsconnect-python package.

RStudio Connect Not Respecting Requirements File

In some cases, you may feel like RStudio Connect doesn’t respect your requirements.txt file. You set torch==1.9.1 but you see under Successfully installed section line: torch-1.10.1. There’s a good chance that you have some libraries set as external libraries. Try to investigate this clue to solve your deployment problems.

Streamlit Tutorial Summary

By now, you should have a fully functioning Streamlit application, deployed on RSConnect. Congratulations!!!

In our case, we deployed the Solar Panels Detection Application.

RStudio Connect is a great way of serving your Streamlit applications. It comes in hand with cases where you already work in RStudio Connect and want to have all your applications in a single place. You can keep everything together – R Shiny, Jupyter notebooks, Streamlit, FastAPI, all of them! All the benefits of RStudio Connect like usage tracking are found here. There’s also no limit on file size when compared to other solutions for serving Streamlit apps.

Crucially, you don’t have to hire additional people to maintain any new servers!

This post was written under:

  • rsconnect-python==1.6.0
  • streamlit==1.2.0
  • RStudio Connect version 2021.11.0