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

Estimated time:

<h2>Streamlit Tutorial on RStudio (Posit) 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="" 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 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: <ul><li><a href="" target="_blank" rel="noopener noreferrer">Python Dash vs R Shiny</a></li><li><a href="" target="_blank" rel="noopener noreferrer">Python Pandas vs R dplyr</a></li></ul> Table of contents: <ul><li><a href="#rsconnect">RStudio Connect and Python</a></li><li><a href="#streamlit">Streamlit</a></li><li><a href="#deploying">Deploying Streamlit Using RStudio 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">RStudio (Posit) Connect and Python</h2> <p dir="auto">It should be noted that <a href="" target="_blank" rel="noopener">RStudio is not all about R</a>. It's very useful for Pythonistas as well! Check out <a href="" 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, 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: <ul><li>RStudio (Posit) IDE</li><li>RStudio (Posit) Workbench</li><li>RStudio (Posit) Connect</li><li>RStudio (Posit) Package Manager</li></ul> If you'd like to know how RStudio's products can be better leveraged for your enterprise contact us. Appsilon is a proud <a href="" target="_blank" rel="noopener noreferrer">RStudio Full Service Certified Partner</a>. <h2 id="streamlit">Streamlit</h2> If you haven't heard of <a href="" target="_blank" rel="noopener noreferrer">Streamlit</a> before, you may want to check out the third post in our series on <a href="" 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 RStudio (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/</code> file:</p> <ol dir="auto"><li>Go to <code>MyApplication</code> directory and run <code>streamlit run</code> to check if everything works as planned.</li><li>Prepare <code>requirements.txt</code> file by running, e.g., <code>pip freeze &gt; 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=""></script> If you're lucky, you will see a message like: <script src=""></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>RStudio Connect Version</h3> <script src=""></script> 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. <img class="alignnone size-full wp-image-12166 aligncenter" src="" alt="" width="556" height="44" /> &nbsp; &nbsp; It might be confusing at first but version 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="" alt="" width="454" height="251" /> <h3>Python Version</h3> <script src=""></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=""></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=""></script> <h3>File Not Found Error</h3> &nbsp; <img class="size-full wp-image-12168 aligncenter" src="" 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></code> - the script would be <strong>inside</strong> <code>MyApplication</code> folder. <h3>Stuck at Uploading</h3> <script src=""></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 &gt; requirements.txt</code> and only then install the <code>rsconnect-python</code> package. <h3>RStudio Connect Not Respecting Requirements File</h3> In some cases, you may feel like RStudio 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="" 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="" target="_blank" rel="nofollow noopener noreferrer">Solar Panels Detection Application</a>.</p> <p dir="auto">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.</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>RStudio Connect version <code>2021.11.0</code></li></ul>

Contact us!
Damian's Avatar
Damian Rodziewicz
Head of Sales
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
rstudio connect