# Streamlit: A Python Library for Rapid Application Development
Hello everyone! Today we are going to learn about Streamlit, a Python library that makes building data applications fast and fun. Whether you are a data scientist, analyst, or a beginner, Streamlit can help you easily create interactive web applications. With just a few lines of Python code, you can visualize data, display model results, and even share your application. Next, let’s dive into the basic usage of Streamlit and some practical examples. Let’s get started!
1. What is Streamlit?
Streamlit is an open-source Python library designed to help users quickly create and share data applications. It is particularly suitable for data visualization and showcasing machine learning models. With Streamlit, you only need to write a few lines of code to generate a fully functional web application interface.
Tip: To install Streamlit, you can use the following command:
pip install streamlit
2. Creating Your First Streamlit Application
Let’s start by creating a simple Streamlit application. In a new Python file, write the following code:
# app.py
import streamlit as st
st.title("My First Streamlit App")
st.write("Welcome to my app!")
In this example, we import Streamlit and use st.title
and st.write
methods to set the title and content of the application.
Run the Application: Run streamlit run app.py
in the terminal, then open the browser to visit the provided address, and you will see the application you just created.
3. Adding User Input
Streamlit allows us to easily add user input components. Let’s add some input boxes for users to enter data:
# app.py
import streamlit as st
st.title("User Input Example")
name = st.text_input("Please enter your name:")
age = st.number_input("Please enter your age:", min_value=0)
if st.button("Submit"):
st.write(f"Hello, {name}, you are {age} years old!")
In this example, we use st.text_input
and st.number_input
methods to get user input. When the user clicks the “Submit” button, the application will display the information they entered.
Tip: Streamlit refreshes the page in real-time, so every input from the user will be immediately reflected in the application.
4. Data Visualization
Streamlit supports various data visualization libraries, such as Matplotlib and Plotly. Let’s create a simple chart using Matplotlib:
# app.py
import streamlit as st
import matplotlib.pyplot as plt
import numpy as np
st.title("Data Visualization Example")
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.title("Sine Wave")
plt.xlabel("x")
plt.ylabel("sin(x)")
st.pyplot(plt)
In this example, we generate a sine wave graph and display it in the application using st.pyplot
. With just a few lines of code, we can present data visualization to the users.
Note: Make sure to install the Matplotlib library using pip install matplotlib
.
5. Using the Sidebar
Streamlit also provides a sidebar feature, allowing us to better organize input components. We can place user inputs in the sidebar:
# app.py
import streamlit as st
st.sidebar.title("Settings")
name = st.sidebar.text_input("Please enter your name:")
age = st.sidebar.number_input("Please enter your age:", min_value=0)
if st.sidebar.button("Submit"):
st.write(f"Hello, {name}, you are {age} years old!")
In this example, we moved the input components to the sidebar, making it easier for users to make settings.
Tip: Using the sidebar can make the interface cleaner, especially when there are multiple input components.
6. Displaying Data Tables
Streamlit allows us to display data in table format. Suppose we have some data to display, we can use the st.dataframe
method:
# app.py
import streamlit as st
import pandas as pd
st.title("Data Table Example")
data = {
'Name': ['Apple', 'Banana', 'Orange'],
'Quantity': [10, 20, 15],
'Price': [1.2, 0.5, 0.8]
}
df = pd.DataFrame(data)
st.dataframe(df)
In this example, we created a simple DataFrame and displayed it in the application using st.dataframe
.
Note: Make sure to install the Pandas library using pip install pandas
.
7. Sharing Your Application with Others
Once you have created an amazing application, you might want to share it with others. Streamlit provides various ways to share applications. You can use Streamlit Sharing or deploy your application to cloud platforms (such as Heroku, AWS, Google Cloud, etc.).
Exercise: Try adding more features to your application, such as chart selection, file upload, etc., to further enhance interactivity.
Conclusion
Today, we learned the basic usage of Streamlit, including how to create applications, add user input, visualize data, use sidebars, and display data tables. With this knowledge, you will be able to quickly build interesting and practical data applications.
Friends, that’s all for today’s Python learning journey! Remember to start coding, and feel free to ask questions in the comment section. Wish everyone happy learning and continuous improvement in Python!