This app allows users to interact with data, visualize it using different charts, and upload files. It includes several interactive elements and dynamically updates based on user input.
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# App Title
st.title("Interactive Data Dashboard")
# Sidebar for User Input
st.sidebar.header("Filter Data")
# Text Input in Sidebar
name = st.sidebar.text_input("Enter your name:")
# Dropdown selection (selectbox)
category = st.sidebar.selectbox(
"Select a category:",
options=["A", "B", "C", "D"]
)
# Slider to filter values
value_filter = st.sidebar.slider("Filter by value:", min_value=-10.0, max_value=10.0, value=(-5.0, 5.0))
# Upload a CSV File
uploaded_file = st.sidebar.file_uploader("Upload a CSV file", type=["csv"])
# Display simple text
st.text(f"Hello {name}, welcome to the dashboard!")
# Markdown formatted text
st.markdown("**Here's an interactive data display**")
# Show a sample table if no file is uploaded
if uploaded_file is None:
st.subheader("Default Dataset")
# Generate random data for demonstration
data = pd.DataFrame({
'Category': np.random.choice(['A', 'B', 'C'], size=100),
'Value': np.random.randn(100),
'Date': pd.date_range(start='1/1/2023', periods=100)
})
else:
# Use uploaded file if available
data = pd.read_csv(uploaded_file)
# Filter data based on the slider and category selection
filtered_data = data[
(data['Category'] == category) &
(data['Value'] >= value_filter[0]) &
(data['Value'] <= value_filter[1])
]
# Display data using st.dataframe and st.table
st.dataframe(filtered_data, use_container_width=True)
st.subheader("Static Table View")
st.table(filtered_data.head())
# Line chart
st.subheader("Line Chart of Values Over Time")
st.line_chart(filtered_data.set_index('Date')['Value'])
# Bar chart
st.subheader("Bar Chart of Categories")
st.bar_chart(filtered_data['Category'].value_counts())
# Area chart
st.subheader("Area Chart of Value Distribution")
st.area_chart(filtered_data.set_index('Date')['Value'])
# Display code sample
st.code('''
# Example code snippet
import pandas as pd
import numpy as np
data = pd.DataFrame({
'Category': np.random.choice(['A', 'B', 'C'], size=100),
'Value': np.random.randn(100),
'Date': pd.date_range(start='1/1/2023', periods=100)
})
''')
# Button to show additional insights
if st.button("Show Summary"):
st.write(filtered_data.describe())
# Image and audio demonstration
st.subheader("Image and Audio Demo")
st.image("https://placekitten.com/400/300", caption="Random Kitten Image")
st.audio("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3")
# Simple text
st.text("Thank you for exploring the dashboard!")
st.write() – Used to display text and Pandas summaries.st.title() – Displays the main title of the app.st.header() – Adds headers to structure the app sections.st.subheader() – Used for smaller section headers.st.text() – Simple text display for welcome and thank-you messages.st.markdown() – Displays Markdown-formatted text for emphasis.st.code() – Displays a block of formatted code.st.dataframe() – Displays a Pandas DataFrame interactively.st.table() – Displays a static version of the data.st.line_chart() – Renders a line chart to visualize data.st.bar_chart() – Renders a bar chart to show categorical counts.st.area_chart() – Displays an area chart for value trends.st.selectbox() – Dropdown in the sidebar for category filtering.st.slider() – Slider widget for selecting a range of values.st.text_input() – Accepts user input in the sidebar.st.file_uploader() – Allows file upload (CSV) for data analysis.st.button() – Button to trigger summary statistics.st.sidebar() – Adds a sidebar for user inputs and controls.st.image() – Displays an image with a caption.st.audio() – Plays an audio file.This example combines data interactivity, visualization, and media handling using the core Streamlit functions.