logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume BuilderLearning Path GeneratorCheatsheet GeneratorAgentic Prompt GeneratorCompany ResearchCover Letter Generator
  • XpertoAI
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCollectionsArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche collections.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Building Interactive Dashboards with Streamlit

author
Generated by
ProCodebase AI

15/11/2024

AI Generatedpython

Sign in to read full article

Introduction to Streamlit

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.

Getting Started with Streamlit

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.

Key Components of a Streamlit Dashboard

1. Data Input and Display

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)

2. Interactive Widgets

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)

3. Data Visualization

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)

Building a Complete Dashboard

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:

  • A sidebar with filters
  • Two charts displaying department statistics and salary distribution
  • A table showing detailed employee information

Enhancing Your Dashboard

To take your dashboard to the next level, consider these advanced features:

  1. Caching: Use @st.cache_data to speed up data loading and processing.
  2. Custom CSS: Improve the look of your dashboard with custom styles.
  3. Session State: Maintain user interactions across reruns.
  4. Metrics and KPIs: Use st.metric() to display important numbers prominently.

Deployment

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.

Popular Tags

pythonstreamlitdata visualization

Share now!

Like & Bookmark!

Related Collections

  • Matplotlib Mastery: From Plots to Pro Visualizations

    05/10/2024 | Python

  • Seaborn: Data Visualization from Basics to Advanced

    06/10/2024 | Python

  • PyTorch Mastery: From Basics to Advanced

    14/11/2024 | Python

  • Mastering LangGraph: Stateful, Orchestration Framework

    17/11/2024 | Python

  • TensorFlow Mastery: From Foundations to Frontiers

    06/10/2024 | Python

Related Articles

  • Unveiling the Power of Unsupervised Learning in Python with Scikit-learn

    15/11/2024 | Python

  • Creating Stunning Scatter Plots with Seaborn

    06/10/2024 | Python

  • Mastering NumPy Array Creation

    25/09/2024 | Python

  • Mastering Django Models and Database Management

    26/10/2024 | Python

  • Supercharging Python with Retrieval Augmented Generation (RAG) using LangChain

    26/10/2024 | Python

  • Elevating Data Visualization

    05/10/2024 | Python

  • Mastering PyTorch Model Persistence

    14/11/2024 | Python

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design