Spaces:
Sleeping
Sleeping
Milind Kamat
commited on
Commit
Β·
1584dfa
1
Parent(s):
65ac6c0
2024 DEC 30 : Ver 20 More exmaples
Browse filesSigned-off-by: Milind Kamat <36366961+milindkamat0507@users.noreply.github.com>
app.py
CHANGED
|
@@ -1,169 +1,267 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
st.set_page_config(
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
page = st.radio(
|
| 13 |
-
"Choose your learning path:",
|
| 14 |
-
["Text Elements", "Input Widgets", "Layout & Containers", "Data Display", "Interactive Charts"]
|
| 15 |
-
)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
st.
|
| 27 |
-
st.
|
| 28 |
-
st.header('Header')
|
| 29 |
-
st.subheader('Subheader')
|
| 30 |
-
st.code('print("Hello World")')
|
| 31 |
-
""")
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
st.write("Try these text elements yourself!")
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
exec(code_input)
|
| 48 |
-
except Exception as e:
|
| 49 |
-
st.error(f"Error: {str(e)}")
|
| 50 |
-
|
| 51 |
-
elif page == "Input Widgets":
|
| 52 |
-
st.header("2. Input Widgets Laboratory")
|
| 53 |
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
st.subheader("Widget Examples")
|
| 58 |
-
st.write("Try these interactive widgets:")
|
| 59 |
-
|
| 60 |
-
# Text input
|
| 61 |
-
name = st.text_input("Enter your name:")
|
| 62 |
-
if name:
|
| 63 |
-
st.write(f"Hello, {name}!")
|
| 64 |
-
|
| 65 |
-
# Number input
|
| 66 |
-
number = st.number_input("Pick a number", 0, 100)
|
| 67 |
-
st.write(f"Your number squared is: {number**2}")
|
| 68 |
-
|
| 69 |
-
# Slider
|
| 70 |
-
age = st.slider("Select your age", 0, 100, 25)
|
| 71 |
-
st.write(f"You selected: {age} years")
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
elif
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
st.button("Column 2 Button")
|
| 104 |
-
|
| 105 |
-
with tab2:
|
| 106 |
-
with st.expander("Click to expand"):
|
| 107 |
-
st.write("This content is hidden until expanded!")
|
| 108 |
-
st.slider("Hidden slider", 0, 100)
|
| 109 |
-
|
| 110 |
-
with tab3:
|
| 111 |
-
with st.container():
|
| 112 |
-
st.write("This is a container")
|
| 113 |
-
st.metric(label="Temperature", value="24 Β°C", delta="1.2 Β°C")
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
|
|
|
| 117 |
|
| 118 |
-
#
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
|
|
|
|
| 126 |
|
| 127 |
-
#
|
| 128 |
-
st.
|
| 129 |
-
st.
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
if city_filter != 'All':
|
| 134 |
-
filtered_df = df[df['City'] == city_filter]
|
| 135 |
-
st.write("Filtered DataFrame:")
|
| 136 |
-
st.dataframe(filtered_df)
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
|
|
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
columns=['A', 'B', 'C']
|
| 145 |
)
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
else:
|
| 158 |
-
st.area_chart(chart_data)
|
| 159 |
-
|
| 160 |
-
st.code("""
|
| 161 |
-
# Create charts
|
| 162 |
-
st.line_chart(chart_data)
|
| 163 |
-
st.bar_chart(chart_data)
|
| 164 |
-
st.area_chart(chart_data)
|
| 165 |
""")
|
| 166 |
|
| 167 |
-
# Footer
|
| 168 |
st.markdown("---")
|
| 169 |
-
st.markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
import plotly.graph_objects as go
|
| 6 |
+
from datetime import datetime, timedelta
|
| 7 |
|
| 8 |
+
st.set_page_config(layout="wide", page_title="Business Analytics Dashboard Tutorial")
|
| 9 |
|
| 10 |
+
# Sample business data generation
|
| 11 |
+
def generate_sales_data():
|
| 12 |
+
dates = pd.date_range(start='2024-01-01', end='2024-12-31', freq='D')
|
| 13 |
+
np.random.seed(42)
|
| 14 |
+
|
| 15 |
+
df = pd.DataFrame({
|
| 16 |
+
'Date': dates,
|
| 17 |
+
'Sales': np.random.normal(1000, 200, len(dates)),
|
| 18 |
+
'Region': np.random.choice(['North', 'South', 'East', 'West'], len(dates)),
|
| 19 |
+
'Product': np.random.choice(['Electronics', 'Clothing', 'Food', 'Books'], len(dates)),
|
| 20 |
+
'Customer_Type': np.random.choice(['Retail', 'Wholesale'], len(dates)),
|
| 21 |
+
})
|
| 22 |
+
df['Profit'] = df['Sales'] * np.random.uniform(0.15, 0.25, len(df))
|
| 23 |
+
return df
|
| 24 |
|
| 25 |
+
# Main Navigation
|
| 26 |
+
st.title("π Business Analytics & Data Analysis Tutorial")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
tabs = st.tabs([
|
| 29 |
+
"Business Dashboard",
|
| 30 |
+
"Data Analysis",
|
| 31 |
+
"Financial Metrics",
|
| 32 |
+
"Practice Zone"
|
| 33 |
+
])
|
| 34 |
+
|
| 35 |
+
# Tab 1: Business Dashboard
|
| 36 |
+
with tabs[0]:
|
| 37 |
+
st.header("Creating Business Dashboards")
|
| 38 |
+
st.write("Learn how to build interactive business dashboards with Streamlit")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Generate sample data
|
| 41 |
+
df_sales = generate_sales_data()
|
|
|
|
| 42 |
|
| 43 |
+
# Dashboard filters
|
| 44 |
+
col1, col2, col3 = st.columns(3)
|
| 45 |
+
with col1:
|
| 46 |
+
selected_region = st.multiselect(
|
| 47 |
+
"Select Region",
|
| 48 |
+
df_sales['Region'].unique(),
|
| 49 |
+
default=df_sales['Region'].unique()[0]
|
| 50 |
+
)
|
| 51 |
+
with col2:
|
| 52 |
+
date_range = st.date_input(
|
| 53 |
+
"Select Date Range",
|
| 54 |
+
value=(df_sales['Date'].min(), df_sales['Date'].max())
|
| 55 |
+
)
|
| 56 |
+
with col3:
|
| 57 |
+
product_type = st.selectbox(
|
| 58 |
+
"Select Product",
|
| 59 |
+
['All'] + list(df_sales['Product'].unique())
|
| 60 |
+
)
|
| 61 |
|
| 62 |
+
# Filter data based on selections
|
| 63 |
+
mask = (df_sales['Region'].isin(selected_region)) & \
|
| 64 |
+
(df_sales['Date'] >= pd.Timestamp(date_range[0])) & \
|
| 65 |
+
(df_sales['Date'] <= pd.Timestamp(date_range[1]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
if product_type != 'All':
|
| 68 |
+
mask &= (df_sales['Product'] == product_type)
|
| 69 |
|
| 70 |
+
filtered_df = df_sales[mask]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# KPI Metrics
|
| 73 |
+
st.subheader("Key Performance Indicators")
|
| 74 |
+
kpi1, kpi2, kpi3, kpi4 = st.columns(4)
|
| 75 |
+
|
| 76 |
+
with kpi1:
|
| 77 |
+
st.metric(
|
| 78 |
+
"Total Sales",
|
| 79 |
+
f"${filtered_df['Sales'].sum():,.0f}",
|
| 80 |
+
f"{((filtered_df['Sales'].sum() / df_sales['Sales'].sum()) - 1) * 100:.1f}%"
|
| 81 |
+
)
|
| 82 |
+
with kpi2:
|
| 83 |
+
st.metric(
|
| 84 |
+
"Average Daily Sales",
|
| 85 |
+
f"${filtered_df['Sales'].mean():,.0f}",
|
| 86 |
+
f"{((filtered_df['Sales'].mean() / df_sales['Sales'].mean()) - 1) * 100:.1f}%"
|
| 87 |
+
)
|
| 88 |
+
with kpi3:
|
| 89 |
+
st.metric(
|
| 90 |
+
"Total Profit",
|
| 91 |
+
f"${filtered_df['Profit'].sum():,.0f}",
|
| 92 |
+
f"{((filtered_df['Profit'].sum() / df_sales['Profit'].sum()) - 1) * 100:.1f}%"
|
| 93 |
+
)
|
| 94 |
+
with kpi4:
|
| 95 |
+
st.metric(
|
| 96 |
+
"Profit Margin",
|
| 97 |
+
f"{(filtered_df['Profit'].sum() / filtered_df['Sales'].sum() * 100):.1f}%"
|
| 98 |
+
)
|
| 99 |
|
| 100 |
+
# Sales Trends
|
| 101 |
+
st.subheader("Sales Trends Analysis")
|
| 102 |
+
daily_sales = filtered_df.groupby('Date')[['Sales', 'Profit']].sum().reset_index()
|
| 103 |
+
|
| 104 |
+
fig = go.Figure()
|
| 105 |
+
fig.add_trace(go.Scatter(
|
| 106 |
+
x=daily_sales['Date'],
|
| 107 |
+
y=daily_sales['Sales'],
|
| 108 |
+
name='Sales',
|
| 109 |
+
line=dict(color='blue')
|
| 110 |
+
))
|
| 111 |
+
fig.add_trace(go.Scatter(
|
| 112 |
+
x=daily_sales['Date'],
|
| 113 |
+
y=daily_sales['Profit'],
|
| 114 |
+
name='Profit',
|
| 115 |
+
line=dict(color='green')
|
| 116 |
+
))
|
| 117 |
+
fig.update_layout(title='Daily Sales and Profit Trends')
|
| 118 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 119 |
|
| 120 |
+
# Tab 2: Data Analysis
|
| 121 |
+
with tabs[1]:
|
| 122 |
+
st.header("Advanced Data Analysis")
|
| 123 |
+
|
| 124 |
+
analysis_type = st.selectbox(
|
| 125 |
+
"Choose Analysis Type",
|
| 126 |
+
["Time Series Analysis", "Regional Performance", "Product Analysis"]
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
if analysis_type == "Time Series Analysis":
|
| 130 |
+
st.subheader("Time Series Decomposition")
|
| 131 |
+
# Monthly aggregation
|
| 132 |
+
monthly_sales = df_sales.groupby(df_sales['Date'].dt.to_period('M')).agg({
|
| 133 |
+
'Sales': 'sum',
|
| 134 |
+
'Profit': 'sum'
|
| 135 |
+
}).reset_index()
|
| 136 |
+
|
| 137 |
+
# Rolling averages
|
| 138 |
+
window_size = st.slider("Moving Average Window (months)", 1, 12, 3)
|
| 139 |
+
monthly_sales['Sales_MA'] = monthly_sales['Sales'].rolling(window=window_size).mean()
|
| 140 |
+
|
| 141 |
+
fig = px.line(monthly_sales,
|
| 142 |
+
x='Date',
|
| 143 |
+
y=['Sales', 'Sales_MA'],
|
| 144 |
+
title=f'Monthly Sales with {window_size}-Month Moving Average')
|
| 145 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 146 |
|
| 147 |
+
elif analysis_type == "Regional Performance":
|
| 148 |
+
st.subheader("Regional Sales Distribution")
|
| 149 |
+
|
| 150 |
+
regional_sales = df_sales.groupby('Region').agg({
|
| 151 |
+
'Sales': 'sum',
|
| 152 |
+
'Profit': 'sum'
|
| 153 |
+
}).reset_index()
|
| 154 |
+
|
| 155 |
+
fig = px.scatter(regional_sales,
|
| 156 |
+
x='Sales',
|
| 157 |
+
y='Profit',
|
| 158 |
+
size='Sales',
|
| 159 |
+
color='Region',
|
| 160 |
+
title='Regional Sales vs Profit')
|
| 161 |
+
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
+
# Tab 3: Financial Metrics
|
| 164 |
+
with tabs[2]:
|
| 165 |
+
st.header("Financial Analysis Tools")
|
| 166 |
|
| 167 |
+
# ROI Calculator
|
| 168 |
+
st.subheader("ROI Calculator")
|
| 169 |
+
col1, col2 = st.columns(2)
|
| 170 |
+
with col1:
|
| 171 |
+
investment = st.number_input("Initial Investment ($)", min_value=0, value=10000)
|
| 172 |
+
revenue = st.number_input("Expected Revenue ($)", min_value=0, value=15000)
|
| 173 |
+
with col2:
|
| 174 |
+
costs = st.number_input("Operating Costs ($)", min_value=0, value=5000)
|
| 175 |
+
time_period = st.number_input("Time Period (years)", min_value=1, value=1)
|
| 176 |
|
| 177 |
+
roi = ((revenue - costs - investment) / investment) * 100
|
| 178 |
+
st.metric("ROI (%)", f"{roi:.1f}%")
|
| 179 |
|
| 180 |
+
# Break-even Analysis
|
| 181 |
+
st.subheader("Break-even Analysis")
|
| 182 |
+
fixed_costs = st.number_input("Fixed Costs ($)", min_value=0, value=50000)
|
| 183 |
+
unit_price = st.number_input("Unit Price ($)", min_value=0, value=100)
|
| 184 |
+
unit_cost = st.number_input("Unit Variable Cost ($)", min_value=0, value=60)
|
| 185 |
|
| 186 |
+
break_even_units = fixed_costs / (unit_price - unit_cost)
|
| 187 |
+
st.metric("Break-even Point (units)", f"{break_even_units:,.0f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
+
# Tab 4: Practice Zone
|
| 190 |
+
with tabs[3]:
|
| 191 |
+
st.header("Practice Exercises")
|
| 192 |
|
| 193 |
+
exercise = st.selectbox(
|
| 194 |
+
"Select Exercise",
|
| 195 |
+
["Sales Dashboard", "Financial Analysis", "Customer Segmentation"]
|
|
|
|
| 196 |
)
|
| 197 |
|
| 198 |
+
if exercise == "Sales Dashboard":
|
| 199 |
+
st.markdown("""
|
| 200 |
+
### Exercise: Create a Sales Dashboard
|
| 201 |
+
|
| 202 |
+
Create a dashboard that shows:
|
| 203 |
+
1. Total sales by region
|
| 204 |
+
2. Monthly sales trend
|
| 205 |
+
3. Top-selling products
|
| 206 |
+
|
| 207 |
+
```python
|
| 208 |
+
# Sample code to get started
|
| 209 |
+
import streamlit as st
|
| 210 |
+
import pandas as pd
|
| 211 |
+
|
| 212 |
+
# Load your data
|
| 213 |
+
df = pd.DataFrame({
|
| 214 |
+
'Date': dates,
|
| 215 |
+
'Sales': values,
|
| 216 |
+
'Region': regions
|
| 217 |
+
})
|
| 218 |
+
|
| 219 |
+
# Create visualizations
|
| 220 |
+
st.line_chart(df.set_index('Date')['Sales'])
|
| 221 |
+
```
|
| 222 |
+
""")
|
| 223 |
+
|
| 224 |
+
# Code input and execution area
|
| 225 |
+
st.subheader("Your Code")
|
| 226 |
+
user_code = st.text_area("Write your code here:", height=200)
|
| 227 |
+
if st.button("Run Analysis"):
|
| 228 |
+
try:
|
| 229 |
+
with st.container(border=True):
|
| 230 |
+
exec(user_code)
|
| 231 |
+
except Exception as e:
|
| 232 |
+
st.error(f"Error: {str(e)}")
|
| 233 |
+
|
| 234 |
+
# Documentation
|
| 235 |
+
with st.expander("π Documentation & Tips"):
|
| 236 |
+
st.markdown("""
|
| 237 |
+
### Business Analytics Best Practices
|
| 238 |
+
|
| 239 |
+
1. **Data Preparation**
|
| 240 |
+
- Always clean and validate your data
|
| 241 |
+
- Handle missing values appropriately
|
| 242 |
+
- Consider seasonality in time series data
|
| 243 |
+
|
| 244 |
+
2. **Visualization Guidelines**
|
| 245 |
+
- Choose appropriate chart types
|
| 246 |
+
- Use consistent color schemes
|
| 247 |
+
- Include clear labels and titles
|
| 248 |
+
|
| 249 |
+
3. **Financial Analysis**
|
| 250 |
+
- Document all assumptions
|
| 251 |
+
- Include sensitivity analysis
|
| 252 |
+
- Provide context for metrics
|
| 253 |
|
| 254 |
+
4. **Dashboard Design**
|
| 255 |
+
- Keep it simple and focused
|
| 256 |
+
- Provide interactive filters
|
| 257 |
+
- Update in real-time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
""")
|
| 259 |
|
| 260 |
+
# Footer with additional resources
|
| 261 |
st.markdown("---")
|
| 262 |
+
st.markdown("""
|
| 263 |
+
<div style='text-align: center'>
|
| 264 |
+
<p>π For more business analytics resources, visit our documentation</p>
|
| 265 |
+
<p>Last updated: {}</p>
|
| 266 |
+
</div>
|
| 267 |
+
""".format(datetime.now().strftime("%Y-%m-%d")), unsafe_allow_html=True)
|
app01.py
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="Interactive Streamlit Tutorial", layout="wide")
|
| 6 |
+
|
| 7 |
+
st.title("Welcome to Streamlit Development Journey π")
|
| 8 |
+
st.markdown("*An interactive guide for MBA students - Practice as you learn!*")
|
| 9 |
+
|
| 10 |
+
with st.sidebar:
|
| 11 |
+
st.header("Navigation Panel")
|
| 12 |
+
page = st.radio(
|
| 13 |
+
"Choose your learning path:",
|
| 14 |
+
["Text Elements", "Input Widgets", "Layout & Containers", "Data Display", "Interactive Charts"]
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
if page == "Text Elements":
|
| 18 |
+
st.header("1. Text Elements Playground")
|
| 19 |
+
|
| 20 |
+
# Learning Section
|
| 21 |
+
st.subheader("π Learn")
|
| 22 |
+
with st.expander("See text formatting examples"):
|
| 23 |
+
st.code("""
|
| 24 |
+
# Different ways to display text:
|
| 25 |
+
st.write('Regular text')
|
| 26 |
+
st.markdown('**Bold** and *italic*')
|
| 27 |
+
st.title('Main Title')
|
| 28 |
+
st.header('Header')
|
| 29 |
+
st.subheader('Subheader')
|
| 30 |
+
st.code('print("Hello World")')
|
| 31 |
+
""")
|
| 32 |
+
|
| 33 |
+
# Practice Section
|
| 34 |
+
st.subheader("π¨ Practice")
|
| 35 |
+
st.write("Try these text elements yourself!")
|
| 36 |
+
|
| 37 |
+
code_input = st.text_area(
|
| 38 |
+
"Type your Streamlit code here:",
|
| 39 |
+
height=100,
|
| 40 |
+
placeholder="Example: st.write('Hello World')"
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
if st.button("Run Code"):
|
| 44 |
+
try:
|
| 45 |
+
with st.container():
|
| 46 |
+
st.write("Your Output:")
|
| 47 |
+
exec(code_input)
|
| 48 |
+
except Exception as e:
|
| 49 |
+
st.error(f"Error: {str(e)}")
|
| 50 |
+
|
| 51 |
+
elif page == "Input Widgets":
|
| 52 |
+
st.header("2. Input Widgets Laboratory")
|
| 53 |
+
|
| 54 |
+
col1, col2 = st.columns(2)
|
| 55 |
+
|
| 56 |
+
with col1:
|
| 57 |
+
st.subheader("Widget Examples")
|
| 58 |
+
st.write("Try these interactive widgets:")
|
| 59 |
+
|
| 60 |
+
# Text input
|
| 61 |
+
name = st.text_input("Enter your name:")
|
| 62 |
+
if name:
|
| 63 |
+
st.write(f"Hello, {name}!")
|
| 64 |
+
|
| 65 |
+
# Number input
|
| 66 |
+
number = st.number_input("Pick a number", 0, 100)
|
| 67 |
+
st.write(f"Your number squared is: {number**2}")
|
| 68 |
+
|
| 69 |
+
# Slider
|
| 70 |
+
age = st.slider("Select your age", 0, 100, 25)
|
| 71 |
+
st.write(f"You selected: {age} years")
|
| 72 |
+
|
| 73 |
+
with col2:
|
| 74 |
+
st.subheader("Code Reference")
|
| 75 |
+
st.code("""
|
| 76 |
+
# Text input
|
| 77 |
+
name = st.text_input("Enter your name:")
|
| 78 |
+
if name:
|
| 79 |
+
st.write(f"Hello, {name}!")
|
| 80 |
+
|
| 81 |
+
# Number input
|
| 82 |
+
number = st.number_input("Pick a number", 0, 100)
|
| 83 |
+
|
| 84 |
+
# Slider
|
| 85 |
+
age = st.slider("Select your age", 0, 100, 25)
|
| 86 |
+
""")
|
| 87 |
+
|
| 88 |
+
elif page == "Layout & Containers":
|
| 89 |
+
st.header("3. Layout Workshop")
|
| 90 |
+
|
| 91 |
+
st.write("Experiment with different layouts:")
|
| 92 |
+
|
| 93 |
+
tab1, tab2, tab3 = st.tabs(["Columns", "Expander", "Container"])
|
| 94 |
+
|
| 95 |
+
with tab1:
|
| 96 |
+
st.write("Create two columns:")
|
| 97 |
+
col1, col2 = st.columns(2)
|
| 98 |
+
with col1:
|
| 99 |
+
st.write("This is column 1")
|
| 100 |
+
st.button("Column 1 Button")
|
| 101 |
+
with col2:
|
| 102 |
+
st.write("This is column 2")
|
| 103 |
+
st.button("Column 2 Button")
|
| 104 |
+
|
| 105 |
+
with tab2:
|
| 106 |
+
with st.expander("Click to expand"):
|
| 107 |
+
st.write("This content is hidden until expanded!")
|
| 108 |
+
st.slider("Hidden slider", 0, 100)
|
| 109 |
+
|
| 110 |
+
with tab3:
|
| 111 |
+
with st.container():
|
| 112 |
+
st.write("This is a container")
|
| 113 |
+
st.metric(label="Temperature", value="24 Β°C", delta="1.2 Β°C")
|
| 114 |
+
|
| 115 |
+
elif page == "Data Display":
|
| 116 |
+
st.header("4. Data Display Workshop")
|
| 117 |
+
|
| 118 |
+
# Create sample data
|
| 119 |
+
df = pd.DataFrame({
|
| 120 |
+
'Name': ['John', 'Emma', 'Alex', 'Sarah'],
|
| 121 |
+
'Age': [28, 24, 32, 27],
|
| 122 |
+
'City': ['New York', 'London', 'Paris', 'Tokyo']
|
| 123 |
+
})
|
| 124 |
+
|
| 125 |
+
st.subheader("Practice with DataFrames")
|
| 126 |
+
|
| 127 |
+
# Show data manipulation options
|
| 128 |
+
st.write("Original DataFrame:")
|
| 129 |
+
st.dataframe(df)
|
| 130 |
+
|
| 131 |
+
# Let users try filtering
|
| 132 |
+
city_filter = st.selectbox("Filter by city:", ['All'] + list(df['City'].unique()))
|
| 133 |
+
if city_filter != 'All':
|
| 134 |
+
filtered_df = df[df['City'] == city_filter]
|
| 135 |
+
st.write("Filtered DataFrame:")
|
| 136 |
+
st.dataframe(filtered_df)
|
| 137 |
+
|
| 138 |
+
elif page == "Interactive Charts":
|
| 139 |
+
st.header("5. Visualization Lab")
|
| 140 |
+
|
| 141 |
+
# Generate random data
|
| 142 |
+
chart_data = pd.DataFrame(
|
| 143 |
+
np.random.randn(20, 3),
|
| 144 |
+
columns=['A', 'B', 'C']
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
# Let users choose chart type
|
| 148 |
+
chart_type = st.selectbox(
|
| 149 |
+
"Select chart type:",
|
| 150 |
+
["Line Chart", "Bar Chart", "Area Chart"]
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
if chart_type == "Line Chart":
|
| 154 |
+
st.line_chart(chart_data)
|
| 155 |
+
elif chart_type == "Bar Chart":
|
| 156 |
+
st.bar_chart(chart_data)
|
| 157 |
+
else:
|
| 158 |
+
st.area_chart(chart_data)
|
| 159 |
+
|
| 160 |
+
st.code("""
|
| 161 |
+
# Create charts
|
| 162 |
+
st.line_chart(chart_data)
|
| 163 |
+
st.bar_chart(chart_data)
|
| 164 |
+
st.area_chart(chart_data)
|
| 165 |
+
""")
|
| 166 |
+
|
| 167 |
+
# Footer
|
| 168 |
+
st.markdown("---")
|
| 169 |
+
st.markdown("π‘ **Pro Tip:** Try modifying the code examples to see how they change the output!")
|