Streamlit is a game-changer in the world of Python-based web applications. It allows data scientists and developers to quickly turn data scripts into shareable web apps without the need for extensive web development experience. Let's dive into how you can leverage Streamlit to build impressive dashboards.
First, you'll need to install Streamlit. Open your terminal and run:
pip install streamlit
Once installed, you can create your first Streamlit app by creating a new Python file, let's call it dashboard.py
, and adding the following code:
import streamlit as st st.title("My First Streamlit Dashboard") st.write("Welcome to the world of interactive dashboards!")
To run your app, use the command:
streamlit run dashboard.py
This will launch a local server and open your dashboard in a web browser.
Streamlit makes it easy to load and display data. Here's how you can upload a CSV file and display it:
import pandas as pd import streamlit as st uploaded_file = st.file_uploader("Choose a CSV file", type="csv") if uploaded_file is not None: data = pd.read_csv(uploaded_file) st.write(data)
Streamlit offers various widgets to make your dashboard interactive. Let's add a slider to filter our data:
min_value = int(data['Age'].min()) max_value = int(data['Age'].max()) age_filter = st.slider("Filter by Age", min_value, max_value, (min_value, max_value)) filtered_data = data[(data['Age'] >= age_filter[0]) & (data['Age'] <= age_filter[1])] st.write(filtered_data)
Streamlit integrates seamlessly with popular visualization libraries. Here's an example using Plotly:
import plotly.express as px fig = px.scatter(filtered_data, x="Age", y="Salary", color="Department") st.plotly_chart(fig)
Now, let's put it all together to create a more complex dashboard:
import streamlit as st import pandas as pd import plotly.express as px # Page config st.set_page_config(page_title="Employee Dashboard", layout="wide") # Title st.title("Employee Dashboard") # Data loading @st.cache_data def load_data(): return pd.read_csv("employee_data.csv") data = load_data() # Sidebar filters st.sidebar.header("Filters") department = st.sidebar.multiselect("Select Department", options=data["Department"].unique()) age_range = st.sidebar.slider("Age Range", min_value=20, max_value=60, value=(20, 60)) # Apply filters filtered_data = data[ (data["Department"].isin(department) if department else True) & (data["Age"].between(age_range[0], age_range[1])) ] # Main content col1, col2 = st.columns(2) with col1: st.subheader("Employee Count by Department") dept_count = filtered_data["Department"].value_counts() fig1 = px.bar(dept_count, x=dept_count.index, y=dept_count.values) st.plotly_chart(fig1) with col2: st.subheader("Salary Distribution") fig2 = px.box(filtered_data, x="Department", y="Salary") st.plotly_chart(fig2) st.subheader("Employee Details") st.dataframe(filtered_data)
This dashboard includes:
To take your dashboard to the next level, consider these advanced features:
@st.cache_data
to speed up data loading and processing.st.metric()
to display important numbers prominently.Once you're happy with your dashboard, you can deploy it using Streamlit Sharing, Heroku, or any other Python-friendly hosting service.
By following this guide, you've learned the basics of creating interactive dashboards with Streamlit. Keep experimenting with different widgets, layouts, and visualizations to create powerful data applications that can help you and your team make data-driven decisions.
05/10/2024 | Python
08/11/2024 | Python
15/10/2024 | Python
26/10/2024 | Python
25/09/2024 | Python
17/11/2024 | Python
25/09/2024 | Python
15/10/2024 | Python
06/10/2024 | Python
17/11/2024 | Python
14/11/2024 | Python
05/11/2024 | Python