Streamlit: A Super Simple Data Application Library!

If you’ve ever thought about quickly building a data application or visualization tool, you might be intimidated by those frameworks that look complex and cumbersome, requiring a bunch of complicated configurations and even advanced front-end knowledge. But with Streamlit, all these problems are solved. Streamlit’s slogan is: Build data applications with Python, simple, fast, and flexible.

If you still feel that data applications are too far from you, why not start from scratch with me and see how easy it is to implement this library.

Streamlit: A Super Simple Data Application Library!

What is Streamlit?

Streamlit is a Python library for quickly building and sharing data applications. Its strength lies in the fact that it requires almost no front-end code; essentially, you just need to focus on the logic and data processing in Python. The Python script you write is almost all of your application.

Does that sound a bit magical? That’s right, Streamlit is such a tool that simplifies the development process of data applications, especially suitable for those who don’t want to spend too much time on front-end technology. You just write a simple piece of code, and Streamlit can quickly generate a web application to display data and even allow user interaction.

Why Choose Streamlit?

1. Simple

The most important point is that Streamlit is very simple. Using it to create data applications requires almost no extra setup. A Python file with a small amount of code can quickly generate an interactive data application.

2. Automatic Updates

One of the powerful features of Streamlit is its automatic update mechanism. As long as you modify the Python code, the application will automatically reload. This makes debugging and modification particularly convenient; you don’t need to manually refresh the page or write complex front-end code.

3. Flexible

Streamlit does not force you to use any specific framework or library. You can freely choose various tools in Python, such as Pandas, NumPy, Matplotlib, Plotly, etc. Streamlit will automatically help you display these tools on the web, creating visualizations and interactions.

4. Interactive Components

Streamlit provides many interactive components, such as buttons, sliders, and selection boxes. You can use these components to allow users to interact with the data application, displaying changes in real-time. This is much more flexible than traditional static web pages.

Installing Streamlit

Installing Streamlit is so simple that you’ll want to laugh. Just run the following command in the terminal:

pip install streamlit

And that’s it! Isn’t it concise? After installation, you can check if it was successful with the following command:

streamlit --version

If you see the version number, it means the installation was successful.

Your First Streamlit Application

Now that we’ve installed it, let’s try our first application. Create a new Python file named app.py and write the following code in the file:

import streamlit as st

st.title("My First Streamlit Application")
st.write("Welcome to the world of Streamlit!")

This code does two things:

  1. Displays a title: “My First Streamlit Application”
  2. Displays a text: “Welcome to the world of Streamlit!”

Next, run the following command in the terminal:

streamlit run app.py

After running, Streamlit will automatically open your application page in the browser. You should see that simple title and text; it’s easier than building a web page.

Add Some Interactivity

Let’s make this application a bit more interesting by adding some interactivity. For example, allowing users to input their names and then displaying a personalized welcome message. Modify app.py to:

import streamlit as st

st.title("My First Streamlit Application")

name = st.text_input("Please enter your name:")
if name:
    st.write(f"Welcome to the world of Streamlit, {name}!")
else:
    st.write("Welcome to the world of Streamlit!")

Here we used st.text_input(), which displays a text input box on the web page. After the user inputs their name, the application will display a personalized welcome message. If the user doesn’t input a name, it will display the default welcome message.

Visualizing Data

One of the strongest features of Streamlit is its ability to display data very simply. Suppose you have a dataset that you want to visualize with a chart; Streamlit can easily handle that too. Let’s demonstrate with a simple matplotlib chart.

First, install matplotlib:

pip install matplotlib

Then modify app.py to include data visualization:

import streamlit as st
import matplotlib.pyplot as plt
import numpy as np

st.title("Display Data Chart")

# Create some data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Draw the chart
fig, ax = plt.subplots()
ax.plot(x, y)
st.pyplot(fig)

Here, we created a simple sine wave chart and displayed it on the web page using st.pyplot(). A few lines of code can achieve graphical data representation, which is incredibly convenient.

Enhancing Interactivity: Slider

Next, let’s add a more interesting interactive element—a slider. Suppose we want to allow users to adjust the frequency of the chart, making the waves oscillate faster or slower. We can add a slider with st.slider() to let users adjust the frequency.

Modify app.py to:

import streamlit as st
import matplotlib.pyplot as plt
import numpy as np

st.title("Display Data Chart - Slider Version")

# Use the slider to adjust frequency
freq = st.slider("Select the frequency of the wave", 1, 10, 1)

# Create data
x = np.linspace(0, 10, 100)
y = np.sin(freq * x)

# Draw the chart
fig, ax = plt.subplots()
ax.plot(x, y)
st.pyplot(fig)

Here, st.slider() creates a slider that allows users to select a frequency between 1 and 10. When users adjust the slider, the chart will update in real-time based on the selected frequency.

Common Questions and Tips

1. Page Not Refreshing

Sometimes you might encounter situations where the page does not refresh, or the application does not update in a timely manner. In this case, you can manually refresh the page, or Streamlit will automatically reload the page when you modify the code.

2. Dependency Issues

If you are using other libraries, remember to install them. For example, matplotlib, pandas, plotly, etc.

3. Optimizing Layout

Streamlit’s layout design is relatively simple, but you can also optimize the interface using some layout functions, such as st.columns() to create multi-column layouts, and st.expander() to fold content.

4. What If There Are Too Many Components?

Streamlit offers many components, and you may feel it difficult to choose. No worries, the best way is to try them out and see which ones meet your needs. Each component’s usage is very intuitive, and the official documentation provides detailed explanations.

Conclusion

Streamlit is a great tool for quickly building data applications. It is simple, flexible, and requires almost no front-end development experience. Whether you are a data scientist, machine learning engineer, or an ordinary developer wanting to showcase data, Streamlit is a tool worth trying.

With just a few lines of code, you can present your analysis results to everyone, and even share it with others for interaction and exploration of data together. Don’t you feel like you instantly became a super data presentation master?

Give it a try, don’t be intimidated by those complicated frameworks; Streamlit has simplified everything to the extreme. All that’s left is for you to play with data using Python!

Leave a Comment