Streamlit: The Lightning Builder for Data Applications!

Streamlit: The Lightning Builder for Data Applications!

Hello everyone, I am Yan Yu. Today we are going to explore Streamlit, a powerful tool that allows you to quickly create data science and machine learning applications. Whether you are a beginner in Python or an experienced developer, Streamlit can help you easily turn your data analysis results or models into an interactive application. Imagine being able to visualize your data and present interactive charts to the world with just a few lines of code. Isn’t that cool? Let’s take a look at how to use Streamlit!

What is Streamlit?

Streamlit is an open-source Python library designed specifically for data scientists and machine learning engineers to quickly build beautiful data applications. Compared to traditional web development, it greatly simplifies the process from data to web interface, allowing you to focus on data analysis and modeling rather than complex front-end code.

Installing Streamlit

Before we start, we need to install Streamlit. Open your terminal or command prompt and enter the following command:

pip install streamlit

Tip: Make sure your Python environment is up to date to avoid compatibility issues.

Creating Your First Streamlit Application

Creating a Streamlit application is very simple; just write a Python script and run it with streamlit run your_script.py. Let’s create the simplest application that displays a welcome message.

import streamlit as st

st.write("Welcome to my first Streamlit application!")

Running the above script will pop up a new page in your browser displaying the message we just wrote. Isn’t it easy?

Displaying Data Tables

In addition to text, Streamlit also supports directly displaying pandas data frames. This is very useful for showcasing and analyzing datasets.

import pandas as pd
import streamlit as st

data = {'Name': ['Zhang San', 'Li Si', 'Wang Wu'], 'Age': [28, 34, 29]}
df = pd.DataFrame(data)

st.write(df)

This code will generate a data table containing names and ages, and Streamlit will automatically format it into an easy-to-read form.

Note: When dealing with large datasets, consider using st.dataframe or st.table methods, which provide more customization options.

Adding Interactive Elements

Making the application interactive is key to engaging users. Streamlit provides various ways to add interactive elements, such as buttons, sliders, drop-down menus, and more.

Using a Slider to Select Age Range

The following example shows how to use a slider to let users choose an age range and filter data based on that range.

age_range = st.slider('Select Age Range:', 0, 100, (25, 75))
filtered_df = df[(df['Age'] >= age_range[0]) & (df['Age'] <= age_range[1])]
st.write(filtered_df)

This code first creates a slider that allows users to select an age range, then filters and displays the corresponding data based on the selected range.

Tip: Want to learn more about interactive components? Try functions like st.selectbox, st.checkbox, and st.text_input.

Drawing Charts

Streamlit also has built-in support for various chart libraries, such as Matplotlib, Plotly, and Altair. Here, we will demonstrate how to draw a simple line chart.

import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [10, 20, 25])
plt.title("Simple Line Chart")
st.pyplot()

This code first imports Matplotlib and draws a line, then displays it in the Streamlit application using the st.pyplot() function.

Note: If you want to use Plotly or Altair, make sure these libraries are correctly installed and follow the official documentation.

Conclusion

Friends, today’s Python learning journey ends here! We introduced the basic concepts of Streamlit and learned how to create our first application, display data tables, add interactive elements, and draw charts. Streamlit allows us to build powerful data applications in a minimal way. Remember to type the code yourself, and feel free to ask me in the comments if you have any questions. I wish you all a happy learning experience and continuous improvement in Python!

Leave a Comment