How to impress with your project: build a web app on Streamlit

Make your project stand out by creating an interactive web application in under an hour

Yara Kyrychenko
NYU Data Science Review
5 min readFeb 1, 2022

--

Photo by Luke Chesser on Unsplash

If you want to create a portfolio of your projects, public GitHub repositories work alright. But that’s boring. Instead, try creating interactive web applications where your professors, potential employers, or friends can play around with your analysis. It’s also an excellent alternative to a final project write-up for some classes. And, thanks to platforms like Streamlit, it is so much easier now.

With Streamlit, you can quickly turn your existing project code into a shareable and interactable data app. If you prefer R, you can try writing a Shiny app instead. But here is how to get you started with Streamlit.

To build an app, you use your Python code plus some extra Streamlit commands. You will also have to create a GitHub repository and sign up for a free Streamlit account to deploy the app, but that’s about it.

Here’s a straightforward project example to illustrate every step of the process! The web app we’re going to make asks for a Twitter username, and then creates a word cloud out of the last 200 tweets of that user. I call it TwitterCloud. Here’s the finished product. I will be using my existing Twitter API permissions and won’t go into details about the process of getting them. If you’d like to learn more, here’s a useful link.

We’ll need to create at least two separate files: app.py and requirements.txt. The txt file has to have exactly this name, but you can name your Python script whatever you want. To keep it clean, I’ll create a third file, helper.py, where I’ll write all the functions the app needs. I also decided to have a txt file with all word cloud stopwords to handle multiple languages easier (English, Russian, and Ukrainian). All my code is on GitHub, so feel free to repurpose it and download all_stopwords.txt from there. Or you can leave the stopwords file empty for now.

Create an empty GitHub repository for the project and clone it to your machine to get started. Then, create or add the four files: app.py, helper.py, requirements.txt, and all_stopwords.txt.

Install Streamlit in your terminal using:

pip install streamlit

To preview how your app is doing, you can run this in the terminal at any time:

streamlit run app.py

Let’s deal with the helper file and functions first. We’ll need to authenticate with Twitter, get user tweets, preprocess the raw tweets and then make a word cloud out of those. Let’s start by importing all the libraries and getting the list of tweets.

This is the only place I will import outside libraries (other than streamlit) so let’s add all of them to requirements.txt now. Add each module’s install name on a separate line like so:

requirements.txt

Obviously, I don’t want to hardcode my Twitter API keys (aka secrets). Streamlit has a procedure in place for that. I’ll come back to it later. For now, let’s add some functions that will preprocess our list of most recent tweets and turn it into a pretty word could.

That’s all for the helper file. Note that I didn’t use any Streamlit syntax in it. I reserved all of it for the main app file. In app.py , let’s import streamlit and all our new functions:

import streamlit as st
from helper import make_stopwords, authenticate, get_user_tweeets, make_wordcloud

Now we can finally start making the user-facing parts of the web app, yay! Let’s give the webpage a title and an explanatory subtitle. We also need a place for the user to enter a Twitter username.

Streamlit will remember the username entered. We can call it anytime during the session by its key (which I specified as“name”): st.session_state.name. Overall, Streamlit has tons of interactive features like sliders, multiple-choice and calendar date selection. Check out this handy cheat sheet from Daniel Lewis to see what else is there.

Screenshot from streamlit.io by the author

Now the painful stuff. Streamlit reruns your entire code all the time for no apparent reason (or, at least, that’s been my experience; I’m sure there is a valid reason). So, to work around that, we need to add a few if statements. There are also a few try-except statements to make sure everything runs smoothly.

Whew! Now that we’re done with all the code, time to talk about those Twitter permission codes we want to keep secret. To deploy the app, you need to register on Streamlit. The Streamlit team takes a few days (three for me) to approve your account. After that, you can deploy your app within minutes.

Sign into Streamlit here and click “New app.” After you’ve created the app, click on “Advanced settings…” and add your secrets (read more here). Warning: do not leave spaces before and after equals signs. You can call the secrets in your code using st.secrets["secret_name"] . No one else will be able to see them.

secrets.toml

That’s it. You can find your app using a link like this one: https://share.streamlit.io/GITHUB_USERNAME/REPO_NAME/main/app.py

Here’s the link for TwitterCloud: https://share.streamlit.io/yarakyrychenko/twittercloud/main/app.py

Feel free to explore your TwitterCloud and share the app with your friends! (I use it to spy on professors but don’t tell anyone ;) )

Last thing: if too few people look at your app for a month, Streamlit will put it to sleep. Meaning the next time someone tries to use it, they will have to wait for a few minutes for it to “wake up.”

Screenshot of TwitterCloud for POTUS (President of the United States) by the author

Thank you for reading! If you end up creating your web app with Streamlit, link it in the comments or tweet it at me (@YaraKyrychenko)! I’d be super excited to check it out!

--

--

Yara Kyrychenko
NYU Data Science Review

PhD candidate at Cambridge. Ukrainian. I love using data science to answer questions in psychology. github.com/yarakyrychenko