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

python

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

  • Automate Everything with Python: A Complete Guide

    08/12/2024 | Python

  • Python Basics: Comprehensive Guide

    21/09/2024 | Python

  • FastAPI Mastery: From Zero to Hero

    15/10/2024 | Python

  • Mastering NumPy: From Basics to Advanced

    25/09/2024 | Python

Related Articles

  • Mastering Data Visualization with Streamlit Charts in Python

    15/11/2024 | Python

  • Integrating APIs with Streamlit Applications

    15/11/2024 | Python

  • Optimizing Matplotlib for Large Datasets

    05/10/2024 | Python

  • Unleashing the Power of Custom Tools and Function Calling in LangChain

    26/10/2024 | Python

  • Enhancing API Documentation with Swagger UI and ReDoc in FastAPI

    15/10/2024 | Python

  • Seaborn for Big Data

    06/10/2024 | Python

  • Unlocking the Power of Django Templates and Template Language

    26/10/2024 | Python

Popular Category

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