Streamlit 1.0 Just Landed — A 5-minute Tutorial | by Yong Cui | Towards Data Science

Web apps are still one of the essential outlets for any company or any project. The reason is simple — web apps are cross-platform and require almost zero learning curve nowadays. For data scientists, we’re not trained as a web developer, and making web apps is never the most straightforward thing for us. Fortunately, a series of web frameworks have been available to make the development of web apps easier.
Among them, streamlit has been a rising star, particularly in the world of data science where the majority of the users use Python as their daily work language. Notably, streamlit is totally Python-friendly, because you can create a web app using only Python.
A few days ago, streamlit reached a milestone by releasing its official 1.0 version, as reported by its official blog. With the increasing support from multiple investors, we can expect streamlit to grow better.
Anyhow, this tutorial is to provide you a quick get-started for you, which I hope it takes just a few minutes for you to launch your first web app.
Don’t believe it? Let’s get our hands dirty.
Assuming that you’ve worked with Python for some time, you should be familiar with creating a virtual environment and installing Python packages. I’m used to using conda for managing virtual environments, so we’re beginning with creating a virtual environment with conda. In the Terminal (if you use Mac), navigate to the desired directory where you want to place the virtual environment, and run the following commands.
Create the virtual environment named st_web:
Create the virtual environment named st_web:
conda create -n st_web
2. Activate the virtual environment:
conda activate st_web
3. Install the streamlit package. Although conda can install streamlit through the conda-forge, I encountered some issues. Thus, I recommend using pip, Python’s official package installation manager instead by running the following commands:
conda install pippip install streamlit
4. Check the installation by simply finding out the installed version.
streamlit --version
If you find that your version isn’t 1.0.0, you can update your version by running:
pip install -U streamlit
In the directory, let’s create a Python script file called st_first_app.py. In the file, start with the following lines of code:
import streamlit as stst.title("My First Web App")
In the Terminal, you can run the web app by running: streamlit run st_first_app.py. You’ll see the web app automatically popped up in your default web browser.
Pandas is the go-to library for many data scientists. As the key data structure, DataFrame is used to process spreadsheet-like data. Let’s try showing a DataFrame in the app. If you’ve not installed pandas in the virtual environment, you can do it by running pip install pandas.
Now, our script is becoming this:
Here’s the screenshot for the web app with the dataset showing.
Figures are the best way to present information, as they can allow users to have an intuitive impression of the dataset. Streamlit natively provides different kinds of plots, such as line, area, and bar charts. For simplicity, let’s just add the following lines of code:
st.header("Fare Cumulative")fare = df['fare'].cumsum()st.line_chart(fare)
With that, the web app will be showing this:
Sometimes, you want to show the pertinent code for a particular operation. You can add a code snippet to the web app:
st.header("Code Snippet")st.code("""a = 100b = 'text'c = [1, 2, 3]""")
With that, you’ll see a code snippet showing on the app.
Streamlit includes many commonly used web widgets, such as radio buttons, sliders, text field, multi-selection, and more. For simplicity, let’s just see a few widgets by adding the following lines of code to your script file.
st.header("Interactive Widgets")st.radio("Choose One", ["To be", "Not to be"])st.slider("How old are you?", min_value=1, max_value=100)st.date_input("Choose your favorite date")
You’ll see these widgets showing on your app.
This is something that I must mention. You can consider that it’s an Easter egg. Simply add the following to your script.
st.header("Show Balloons")your_energy = st.slider(    "Try setting your energy level to 90 or above",    min_value=1,     max_value=100)if your_energy >= 90:    st.balloons()
You’ll see a bunch of nice balloons!
In this article, I showed you some common elements that you can add to your streamlit web app. As you can see, all the elements are nicely shown on the web without your worrying about the layout of any of the elements. Importantly, it includes probably all the elements that you need to build a data science or machine learning app.
Haven’t tried Streamlit? It’s time!
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.