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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
import random
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
# Set page configuration
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="Quick Streamlit Demo",
|
| 11 |
+
page_icon="📊",
|
| 12 |
+
layout="wide",
|
| 13 |
+
initial_sidebar_state="expanded"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Add custom CSS
|
| 17 |
+
st.markdown("""
|
| 18 |
+
<style>
|
| 19 |
+
.main-header {
|
| 20 |
+
font-size: 2.5rem;
|
| 21 |
+
color: #1E88E5;
|
| 22 |
+
}
|
| 23 |
+
.sub-header {
|
| 24 |
+
font-size: 1.5rem;
|
| 25 |
+
color: #424242;
|
| 26 |
+
}
|
| 27 |
+
</style>
|
| 28 |
+
""", unsafe_allow_html=True)
|
| 29 |
+
|
| 30 |
+
# App title
|
| 31 |
+
st.markdown('<p class="main-header">Interactive Streamlit Dashboard</p>', unsafe_allow_html=True)
|
| 32 |
+
st.markdown("This is a quick demo of Streamlit's capabilities.")
|
| 33 |
+
|
| 34 |
+
# Sidebar
|
| 35 |
+
st.sidebar.title("Controls")
|
| 36 |
+
data_size = st.sidebar.slider("Data Points", 10, 500, 100)
|
| 37 |
+
chart_height = st.sidebar.slider("Chart Height", 300, 800, 400)
|
| 38 |
+
|
| 39 |
+
# Define color themes dictionary mapping to actual Plotly color sequences
|
| 40 |
+
color_themes = {
|
| 41 |
+
"blues": px.colors.sequential.Blues,
|
| 42 |
+
"reds": px.colors.sequential.Reds,
|
| 43 |
+
"greens": px.colors.sequential.Greens,
|
| 44 |
+
"purples": px.colors.sequential.Purples,
|
| 45 |
+
"oranges": px.colors.sequential.Oranges
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
color_theme = st.sidebar.selectbox("Color Theme", list(color_themes.keys()))
|
| 49 |
+
|
| 50 |
+
# Generate random data
|
| 51 |
+
@st.cache_data
|
| 52 |
+
def generate_data(n_points):
|
| 53 |
+
np.random.seed(42)
|
| 54 |
+
dates = pd.date_range(start=datetime.now().date(), periods=n_points)
|
| 55 |
+
df = pd.DataFrame({
|
| 56 |
+
'date': dates,
|
| 57 |
+
'value': np.random.randn(n_points).cumsum(),
|
| 58 |
+
'category': np.random.choice(['A', 'B', 'C', 'D'], n_points),
|
| 59 |
+
'size': np.random.randint(10, 100, n_points)
|
| 60 |
+
})
|
| 61 |
+
return df
|
| 62 |
+
|
| 63 |
+
# Create tabs
|
| 64 |
+
tab1, tab2, tab3 = st.tabs(["Chart", "Data Explorer", "About"])
|
| 65 |
+
|
| 66 |
+
with tab1:
|
| 67 |
+
st.markdown('<p class="sub-header">Interactive Data Visualization</p>', unsafe_allow_html=True)
|
| 68 |
+
|
| 69 |
+
# Generate and display data
|
| 70 |
+
df = generate_data(data_size)
|
| 71 |
+
|
| 72 |
+
# Create a line chart with Plotly
|
| 73 |
+
fig = px.line(
|
| 74 |
+
df, x='date', y='value',
|
| 75 |
+
color='category',
|
| 76 |
+
color_discrete_sequence=color_themes[color_theme],
|
| 77 |
+
height=chart_height
|
| 78 |
+
)
|
| 79 |
+
fig.update_layout(
|
| 80 |
+
title="Time Series Data",
|
| 81 |
+
xaxis_title="Date",
|
| 82 |
+
yaxis_title="Value",
|
| 83 |
+
legend_title="Category",
|
| 84 |
+
hovermode="x unified"
|
| 85 |
+
)
|
| 86 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 87 |
+
|
| 88 |
+
# Add some metrics
|
| 89 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 90 |
+
with col1:
|
| 91 |
+
st.metric("Max Value", f"{df['value'].max():.2f}", f"{random.uniform(-10, 10):.2f}%")
|
| 92 |
+
with col2:
|
| 93 |
+
st.metric("Min Value", f"{df['value'].min():.2f}", f"{random.uniform(-10, 10):.2f}%")
|
| 94 |
+
with col3:
|
| 95 |
+
st.metric("Average", f"{df['value'].mean():.2f}", f"{random.uniform(-10, 10):.2f}%")
|
| 96 |
+
with col4:
|
| 97 |
+
st.metric("Data Points", len(df), f"{data_size - 50}")
|
| 98 |
+
|
| 99 |
+
with tab2:
|
| 100 |
+
st.markdown('<p class="sub-header">Data Explorer</p>', unsafe_allow_html=True)
|
| 101 |
+
|
| 102 |
+
# Filter options
|
| 103 |
+
st.subheader("Filter Data")
|
| 104 |
+
|
| 105 |
+
# Create columns for filters
|
| 106 |
+
filter_col1, filter_col2 = st.columns(2)
|
| 107 |
+
|
| 108 |
+
with filter_col1:
|
| 109 |
+
selected_categories = st.multiselect(
|
| 110 |
+
"Select Categories",
|
| 111 |
+
options=df['category'].unique(),
|
| 112 |
+
default=df['category'].unique()
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
with filter_col2:
|
| 116 |
+
min_val, max_val = st.slider(
|
| 117 |
+
"Value Range",
|
| 118 |
+
float(df['value'].min()),
|
| 119 |
+
float(df['value'].max()),
|
| 120 |
+
[float(df['value'].min()), float(df['value'].max())]
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
# Filter the data
|
| 124 |
+
filtered_df = df[
|
| 125 |
+
(df['category'].isin(selected_categories)) &
|
| 126 |
+
(df['value'] >= min_val) &
|
| 127 |
+
(df['value'] <= max_val)
|
| 128 |
+
]
|
| 129 |
+
|
| 130 |
+
# Show the filtered dataframe
|
| 131 |
+
st.subheader(f"Filtered Data ({len(filtered_df)} rows)")
|
| 132 |
+
st.dataframe(filtered_df, use_container_width=True)
|
| 133 |
+
|
| 134 |
+
# Download button
|
| 135 |
+
csv = filtered_df.to_csv(index=False).encode('utf-8')
|
| 136 |
+
st.download_button(
|
| 137 |
+
"Download CSV",
|
| 138 |
+
csv,
|
| 139 |
+
"filtered_data.csv",
|
| 140 |
+
"text/csv",
|
| 141 |
+
key='download-csv'
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
with tab3:
|
| 145 |
+
st.markdown('<p class="sub-header">About This App</p>', unsafe_allow_html=True)
|
| 146 |
+
|
| 147 |
+
st.markdown("""
|
| 148 |
+
This is a quick demo showcasing some of Streamlit's features:
|
| 149 |
+
|
| 150 |
+
- **Interactive Charts**: Visualize data dynamically
|
| 151 |
+
- **Data Filtering**: Filter and explore datasets
|
| 152 |
+
- **Download Capabilities**: Export data as CSV
|
| 153 |
+
- **Metrics & KPIs**: Display key performance indicators
|
| 154 |
+
- **Custom Styling**: Enhance UI with custom CSS
|
| 155 |
+
|
| 156 |
+
Streamlit makes it easy to create data apps with just a few lines of Python code.
|
| 157 |
+
""")
|
| 158 |
+
|
| 159 |
+
st.info("Made with ❤️ using Streamlit")
|
| 160 |
+
|
| 161 |
+
# Footer
|
| 162 |
+
st.markdown("---")
|
| 163 |
+
st.markdown("Created as a quick Streamlit demo")
|