JaviA commited on
Commit
2fd0c51
ยท
verified ยท
1 Parent(s): 8c92a2c

Upload src/streamlit_app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +109 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,111 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
1
  import streamlit as st
2
+ import pandas as pd
3
+ import plotly.express as px
4
+ import plotly.graph_objects as go
5
+ from plotly.subplots import make_subplots
6
+ import numpy as np
7
+
8
+ # Page config
9
+ st.set_page_config(
10
+ page_title="Uber NYC Pickups",
11
+ page_icon="๐Ÿš•",
12
+ layout="wide",
13
+ initial_sidebar_state="expanded"
14
+ )
15
+
16
+ # Header with required link
17
+ st.title("๐Ÿš• Uber NYC Pickups")
18
+ st.markdown("[Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)")
19
+
20
+ # Load data (simulate as if from the repo; in practice, download or use cached)
21
+ @st.cache_data
22
+ def load_data():
23
+ # For demo purposes, generate sample data similar to uber-raw-data-sep14.csv
24
+ # In a real app, load from 'https://github.com/streamlit/demo-uber-nyc-pickups/raw/master/uber-raw-data-sep14.csv'
25
+ np.random.seed(42)
26
+ n_points = 10000
27
+ dates = pd.date_range('2014-09-01', periods=n_points, freq='min')
28
+ lats = np.random.normal(40.75, 0.1, n_points)
29
+ lons = np.random.normal(-73.97, 0.1, n_points)
30
+ df = pd.DataFrame({
31
+ 'Date/Time': dates,
32
+ 'Lat': lats,
33
+ 'Lon': lons,
34
+ 'Base': np.random.choice(['B02512', 'B02598'], n_points)
35
+ })
36
+ return df
37
+
38
+ df = load_data()
39
+
40
+ # Sidebar filters
41
+ st.sidebar.header("Filters")
42
+ date_range = st.sidebar.slider(
43
+ "Select Date Range",
44
+ min_value=df['Date/Time'].min(),
45
+ max_value=df['Date/Time'].max(),
46
+ value=(df['Date/Time'].min(), df['Date/Time'].max()),
47
+ format="YYYY-MM-DD HH:MM"
48
+ )
49
+ hour_filter = st.sidebar.slider("Select Hour of Day", 0, 23, (0, 23))
50
+
51
+ base_filter = st.sidebar.multiselect(
52
+ "Select Base",
53
+ options=df['Base'].unique(),
54
+ default=df['Base'].unique()
55
+ )
56
+
57
+ # Filter data
58
+ filtered_df = df[
59
+ (df['Date/Time'] >= date_range[0]) &
60
+ (df['Date/Time'] <= date_range[1]) &
61
+ (df['Date/Time'].dt.hour.between(hour_filter[0], hour_filter[1])) &
62
+ (df['Base'].isin(base_filter))
63
+ ].copy()
64
+
65
+ # Metrics
66
+ col1, col2, col3 = st.columns(3)
67
+ with col1:
68
+ st.metric("Total Pickups", len(filtered_df))
69
+ with col2:
70
+ avg_hour = filtered_df['Date/Time'].dt.hour.mean()
71
+ st.metric("Average Hour", f"{avg_hour:.0f}")
72
+ with col3:
73
+ unique_bases = filtered_df['Base'].nunique()
74
+ st.metric("Unique Bases", unique_bases)
75
+
76
+ # Visualizations
77
+ if len(filtered_df) > 0:
78
+ # Map
79
+ st.subheader("Pickups Map")
80
+ fig_map = px.scatter_mapbox(
81
+ filtered_df,
82
+ lat="Lat",
83
+ lon="Lon",
84
+ color="Base",
85
+ hover_data=["Date/Time"],
86
+ zoom=10,
87
+ height=500,
88
+ mapbox_style="carto-positron"
89
+ )
90
+ st.plotly_chart(fig_map, use_container_width=True)
91
+
92
+ # Time series
93
+ st.subheader("Pickups Over Time")
94
+ filtered_df['Date'] = filtered_df['Date/Time'].dt.date
95
+ hourly_data = filtered_df.groupby(filtered_df['Date/Time'].dt.hour).size().reset_index(name='Count')
96
+ fig_time = px.line(hourly_data, x='Date/Time', y='Count', title="Pickups by Hour")
97
+ st.plotly_chart(fig_time, use_container_width=True)
98
 
99
+ # Hourly heatmap
100
+ st.subheader("Hourly Distribution")
101
+ hourly_dist = filtered_df.groupby('Date/Time').size().reset_index(name='Count')
102
+ fig_heatmap = px.density_heatmap(
103
+ filtered_df,
104
+ x=filtered_df['Date/Time'].dt.hour,
105
+ y=filtered_df['Date/Time'].dt.date,
106
+ z=filtered_df.groupby([filtered_df['Date/Time'].dt.date, filtered_df['Date/Time'].dt.hour]).size().values.reshape(-1, 24),
107
+ color_continuous_scale="Viridis"
108
+ )
109
+ st.plotly_chart(fig_heatmap, use_container_width=True)
110
+ else:
111
+ st.info("No data matches the selected filters.")