Deploy a Public Streamlit Web App for Free — Here’s How | by Yong Cui | Towards Data Science

With the growing popularity of Python as the go-to choice for many data scientists, the dissemination of their findings has become an urgent need. Web apps represent a cross-platform solution that provides sufficient interactivity. However, the conventional development requires Python users’ sufficient familiarity with relatively complicated web frameworks, such as Flask and Django. Although it’s not the hardest thing for any experienced Python users to learn these frameworks, it might be an overkill for data scientists to showcase their data science projects.
Fortunately, we data scientists have a better option now — the Streamlit framework. It simply converts simple Python scripts to functional, interactive, and shareable web apps within minutes. Because it’s designed toward showcasing data science products, it natively supports the data models and graphs with major data science libraries, such as pandas and matplotlib. Thus, it requires little learning curve if you already know Python well. If you’ve not tried that, here’s a couple of articles that I published before to get you started with streamlit.
towardsdatascience.com
towardsdatascience.com
In this article, I want to take you guys a step further by showing you how to publish your web app such that others can access your data science projects publicly. More importantly, I’ll show you how to integrate Google Sheets as a free database host for our project. Please note that Google Sheets isn’t an industry-level database, but it’s good enough to host a small dataset. What’s more — it’s free.
Without further ado, let’s get it started.
First, I want to make sure that you’ve installed streamlit on your computer and you’re able to launch a web app locally.
You can install the streamlit framework using the pip tool: pip install streamlit.
You can check if it’s installed by simply viewing the version: streamlit --version. This command should tell you what version the installed streamlit package is. The version information displayed is Streamlit, version 0.89.0 while I’m writing this tutorial.
Create a Python script file and let’s simply call it streamlit_sharing.py. Because the goal of this tutorial is to show you how to publish the web app, we’ll keep the stuff in the app minimum. Suppose that the script includes the following lines of code.
To launch the app locally, in the terminal, you can run the following command: streamlit run streamlit_sharing.py. Please make sure that you need to navigate to the directory where the Python script is saved. Otherwise, you’ll have to specify the full path to the file.
The above screenshot shows you the web app running on your local host. Great! You’ve simply launched your first Streamlit web app.
We should understand that Streamlit supports the connection to many different kinds of database, but online database hosts are not typically free. To provide you with a proof of concept, we’ll use a public Google Sheet as the data source for our web app. Suppose that our sheet has the following data.
Once you create the Google sheet, please turn on sharing publicly such that our web app can access it directly.
IMPORTANT: To keep the app simple, we’ll only consider the set-up that uses a public Google Sheet. If you want to use a private Google Sheet, it requires enabling Google APIs and interested readers can refer to Streamlit’s website for additional instruction.
To access the Google sheet programmatically, we need to install the package gsheetsdb by running pip install gsheetsdb. Once it’s installed, we can update our script to the following version:
The above code involves the following changes:
We use the gsheetsdb for connecting to the Google sheet.
To use the sheet, we use a SQL-like syntax to retrieve the data. Notably, instead of specifying the sheet’s name, we need to specify the URL to the sheet. Don’t forget about the double quotes for the sheet’s URL.
We create a DataFrame object from the retrieved rows.
With all these changes, our web app looks like the following now. As you can see, we’re indeed able to retrieve the data from the created Google sheet.
There are different options to host a Streamlit app, such as Heroku (You can find a tutorial in a Medium article). Here, I want to show you to use Streamlit Share — a free Streamlit web app sharing service provided by Streamlit. You can find more information at its official blog. Here are the general steps.
Because the Streamlit team is still developing the platform, you need to request an invite to be able to deploy the app on Streamlit Share. You can submit a request here: https://streamlit.io/cloudOnce you submit your request, it won’t take too long for them to approve your request. In my case, it took less than one day to receive the invite.You can now login into the Streamlit Share (https://share.streamlit.io/) with your GitHub authentication.
Because the Streamlit team is still developing the platform, you need to request an invite to be able to deploy the app on Streamlit Share. You can submit a request here: https://streamlit.io/cloud
Once you submit your request, it won’t take too long for them to approve your request. In my case, it took less than one day to receive the invite.
You can now login into the Streamlit Share (https://share.streamlit.io/) with your GitHub authentication.
As you’ll see next, we’ll share our app in a public GitHub repository, and thus we don’t want to expose the Google sheet URL, which can be a potential security concern. Certainly, using a private Google Sheet or other databases with authentications is more secure.
Instead of specifying the URL in the script directly, you’ll save the URL in a shared app’s settings, from which you can access using the setting’s name.
# Originalgsheet_url = "the_link"# Updated, when the URL is saved in the settingsgsheet_url = st.secrets["public_gsheets_url"]
Once you update your script, it’s finally the time to make your app public (to some extent). What you’ll do is to create a public GitHub repository. I’m not going to expand how you can do that here. I use PyCharm, and I just need to use the built-in Git tool to create a repository easily.
Go to the Streamlit Share website, you can create a new web app by clicking “New App”, which will pop up the window shown below (left panel).
If you’ve already linked your GitHub account, Streamlit is smart enough to pull out the list of repositories and you can simply choose the one and specify the file path.
Importantly, you click the “Advanced settings” and specify the link there, because as mentioned previously, this is how our streamlit script file accesses the confidential information. When needed, you need to choose a proper Python version for your project, because by default, it’s set to 3.7.
After that, click the “Deploy!” button, and wait for a couple of moments, and you should be able to see your app is up and running!
Because it’s hosted by Streamlit Share as a public website, you can share the link to your teammates or clients and they can access it from anywhere they want.
Streamlit has made web app developments easy for data scientists, because it takes care of the layout of the web elements, and we data scientists are just responsible for the core part of our app — the data.
If you have never tried Streamlit, I strongly recommend you give it a try, and I bet you’ll love it.
Thanks for reading this article. Stay connected by signing up my newsletter. Not a Medium member yet? Support my writing by using my membership link.