Streamlit Tutorial: How to Deploy Streamlit Apps on Posit Connect
<h2>Streamlit Tutorial on Posit (formerly RStudio) Connect</h2>
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 <a href="https://appsilon.com/using-streamlit-to-deploy-poc-app-part-3/" target="_blank" rel="noopener noreferrer">Streamlit tutorial series</a>, we created a Streamlit application from scratch. If you completed the tutorial you might've noticed it was deployed using Posit Connect. We've now updated this blog post to reflect the rebrand from RStudio to Posit.
Posit 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:
<ul><li><a href="https://appsilon.com/dash-vs-shiny/" target="_blank" rel="noopener noreferrer">Python Dash vs R Shiny</a></li><li><a href="https://appsilon.com/pandas-vs-dplyr/" target="_blank" rel="noopener noreferrer">Python Pandas vs R dplyr</a></li></ul>
Table of contents:
<ul><li><a href="#rsconnect">Posit Connect and Python</a></li><li><a href="#streamlit">Streamlit</a></li><li><a href="#deploying">Deploying Streamlit Using Posit Connect</a></li><li><a href="#problems">Solving Potential Deployment Problems</a></li><li><a href="#conclsuion">Streamlit Tutorial Summary</a></li></ul>
<hr />
<h2 id="reconnect">Posit Connect and Python</h2>
<p dir="auto">It should be noted that <a href="https://appsilon.com/posit-rstudio-rebrands/" target="_blank" rel="noopener">Posit is not all about R</a>. It's very useful for Pythonistas as well! Check out <a href="https://docs.rstudio.com/connect/user/flask/" target="_blank" rel="nofollow noopener noreferrer">RSConnect documentation</a> on deploying apps in:</p>
<ul dir="auto"><li>Flask</li><li>FastAPI</li><li>Bokeh</li><li>Dash</li><li>Jupyter notebooks</li><li>Streamlit</li></ul>
Like most Data Science teams, Posit understands the value Python and R can provide in different contexts and when leveraged together. That's why the Posit team has been focusing on creating the ultimate open-source data science ecosystem with the Posit suite. You can now merge your projects and teams seamlessly using R and Python with:
<ul><li>Posit IDE</li><li>Posit Workbench</li><li>Posit Connect</li><li>Posit Package Manager</li></ul>
If you'd like to know how Posit's products can be better leveraged for your enterprise contact us. Appsilon is a proud <a href="https://appsilon.com/appsilon-is-an-rstudio-full-service-certified-partner/" target="_blank" rel="noopener noreferrer">Posit Full Service Certified Partner</a>.
<h2 id="streamlit">Streamlit</h2>
If you haven't heard of <a href="https://streamlit.io/" target="_blank" rel="noopener noreferrer">Streamlit</a> before, you may want to check out the third post in our series on <a href="https://appsilon.com/using-streamlit-to-deploy-poc-app-part-3/" target="_blank" rel="noopener noreferrer">creating Streamlit applications</a>. 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.
<h2 id="deploying">Deploying Streamlit Using Posit Connect</h2>
<p dir="auto">Deploying your Streamlit application is pretty straightforward. In the <code>conda</code>/<code>pipenv</code> environment in which you run Streamlit application install <code>rsconnect-python</code> by running <code>pip install rsconnect-python</code>. Suppose that your application is located in <code>MyApplication</code> directory and main application script is written in <code>MyApplication/main.py</code> file:</p>
<ol dir="auto"><li>Go to <code>MyApplication</code> directory and run <code>streamlit run main.py</code> to check if everything works as planned.</li><li>Prepare <code>requirements.txt</code> file by running, e.g., <code>pip freeze > requirements.txt</code>.</li><li>Go to <strong>the parent</strong> directory (with respect to <code>MyApplication</code> folder) and publish the application using:</li></ol>
<script src="https://gist.github.com/MicahAppsilon/24e4a9a25160aa38de9fb9314bc8371d.js"></script>
If you're lucky, you will see a message like:
<script src="https://gist.github.com/MicahAppsilon/876d0e3aac5ccc4983198803018021c3.js"></script>
If so, congratulations! You've successfully published your application.🎉🎉🎉
<h2 id="problems">Solving Potential Deployment Problems</h2>
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.
<h3>Posit Connect Version</h3>
<script src="https://gist.github.com/MicahAppsilon/0d93f85cadf1bc1167ad8911d77b073e.js"></script>
The possibility to deploy a Streamlit application has been added in Posit Connect 1.8.4. Check if you satisfy the minimum version requirement by going to the Documentation tab in RSConnect.
<img class="alignnone size-full wp-image-12166 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ec9385c8d506b9bf42e_documentation.webp" alt="" width="556" height="44" />
It might be confusing at first but version 1.9.0.1 is <strong>followed by</strong> version 2021.08.0. Starting from this point in time, RSConnect version names follow the <code>year.month.minor</code> convention.
<img class="size-full wp-image-12172 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ecafeabe8f16c325d58_versions.webp" alt="" width="454" height="251" />
<h3>Python Version</h3>
<script src="https://gist.github.com/MicahAppsilon/d1b4822564e2d6e22f75a89aef0179bc.js"></script>
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 <code>conda create -n myenv python==3.9.5</code> if you use <code>conda</code>.
<h3>Pip Installation Error</h3>
<script src="https://gist.github.com/MicahAppsilon/155e409e0b247e83f5aa6d346dc68b00.js"></script>
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 <code>--new</code> flag. Once you've deployed your app successfully and just want to add some features, fix typos, etc. omit the <code>--new</code> flag.
<script src="https://gist.github.com/MicahAppsilon/3d04156603791cd2e5167b17f864caef.js"></script>
<h3>File Not Found Error</h3>
<img class="size-full wp-image-12168 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01eca60c4b5e759a9cc36_file_not_found_error.webp" alt="" width="957" height="336" />
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 <code>main.py</code> - the script would be <strong>inside</strong> <code>MyApplication</code> folder.
<h3>Stuck at Uploading</h3>
<script src="https://gist.github.com/MicahAppsilon/c9232ad021b7e1378b8be45192db3181.js"></script>
<p dir="auto">So you feel like you've done everything right but your <code>rsconnect-python</code> 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 <code>model.pkl</code>. 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.</p>
<p dir="auto">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 <code>Hello World</code> application. If it works, you can proceed and upload the entire app with <code>model.pkl</code>. 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.</p>
<h3>Building the Environment</h3>
Try to create a fresh environment and install only the packages required to run your Streamlit application. Create a requirements file with <code>pip freeze > requirements.txt</code> and only then install the <code>rsconnect-python</code> package.
<h3>Posit Connect Not Respecting Requirements File</h3>
In some cases, you may feel like Posit Connect doesn't respect your <code>requirements.txt</code> file. You set <code>torch==1.9.1</code> but you see under <code>Successfully installed</code> section line: <code>torch-1.10.1</code>. There's a good chance that you have some libraries set as <a href="https://docs.rstudio.com/connect/admin/python/package-management/#external-package-installation" rel="nofollow">external libraries</a>. Try to investigate this clue to solve your deployment problems.
<h2 id="conclusion">Streamlit Tutorial Summary</h2>
<p dir="auto">By now, you should have a fully functioning Streamlit application, deployed on RSConnect. Congratulations!!!</p>
<p dir="auto">In our case, we deployed the <a href="https://demo.appsilon.com/apps/solar-panels-detection-streamlit/" target="_blank" rel="nofollow noopener noreferrer">Solar Panels Detection Application</a>.</p>
<p dir="auto">Posit Connect is a great way of serving your Streamlit applications. It comes in hand with cases where you already work in Posit 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 Posit Connect like usage tracking are found here. There's also no limit on file size when compared to other solutions for serving Streamlit apps.</p>
<p dir="auto">Crucially, you don't have to hire additional people to maintain any new servers!</p>
<p dir="auto">This post was written under:</p>
<ul dir="auto"><li><code>rsconnect-python==1.6.0</code></li><li><code>streamlit==1.2.0</code></li><li>Posit Connect version <code>2021.11.0</code></li></ul>