Spaces:
Sleeping
Sleeping
fix temporal visualizations: use container width and constrain x-axis to data range
Browse files- src/streamlit_app.py +26 -21
src/streamlit_app.py
CHANGED
|
@@ -510,10 +510,15 @@ def create_temporal_visualizations(df: pd.DataFrame) -> Dict[str, go.Figure]:
|
|
| 510 |
df['created_utc'] = pd.to_datetime(df['created_utc'])
|
| 511 |
df = df.sort_values('created_utc')
|
| 512 |
|
| 513 |
-
# Get actual date range of collected data
|
| 514 |
date_min = df['created_utc'].min()
|
| 515 |
date_max = df['created_utc'].max()
|
| 516 |
date_range = (date_max - date_min).days
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 517 |
|
| 518 |
# 1. Hourly activity heatmap
|
| 519 |
df['hour'] = df['created_utc'].dt.hour
|
|
@@ -593,17 +598,17 @@ def create_temporal_visualizations(df: pd.DataFrame) -> Dict[str, go.Figure]:
|
|
| 593 |
)
|
| 594 |
|
| 595 |
fig_timeline.update_layout(
|
| 596 |
-
height=600,
|
| 597 |
showlegend=True,
|
| 598 |
title=f'Activity Timeline ({date_min.strftime("%Y-%m-%d")} to {date_max.strftime("%Y-%m-%d")})'
|
| 599 |
)
|
| 600 |
fig_timeline.update_xaxes(
|
| 601 |
-
title_text="Date",
|
| 602 |
row=2, col=1,
|
| 603 |
-
range=[
|
| 604 |
)
|
| 605 |
fig_timeline.update_xaxes(
|
| 606 |
-
range=[
|
| 607 |
row=1, col=1
|
| 608 |
)
|
| 609 |
fig_timeline.update_yaxes(title_text="Count", row=1, col=1)
|
|
@@ -639,7 +644,7 @@ def create_temporal_visualizations(df: pd.DataFrame) -> Dict[str, go.Figure]:
|
|
| 639 |
fig_monthly.update_layout(
|
| 640 |
title=f'Monthly Posting Trends ({date_min.strftime("%Y-%m")} to {date_max.strftime("%Y-%m")})',
|
| 641 |
xaxis_title='Month',
|
| 642 |
-
xaxis=dict(range=[
|
| 643 |
yaxis=dict(title='Post Count', side='left'),
|
| 644 |
yaxis2=dict(title='Average Score', side='right', overlaying='y'),
|
| 645 |
height=400,
|
|
@@ -675,7 +680,7 @@ def create_temporal_visualizations(df: pd.DataFrame) -> Dict[str, go.Figure]:
|
|
| 675 |
fig_weekly.update_layout(
|
| 676 |
title=f'Weekly Posting Trends ({date_min.strftime("%Y-%m-%d")} to {date_max.strftime("%Y-%m-%d")})',
|
| 677 |
xaxis_title='Week',
|
| 678 |
-
xaxis=dict(range=[
|
| 679 |
yaxis=dict(title='Post Count', side='left'),
|
| 680 |
yaxis2=dict(title='Average Score', side='right', overlaying='y'),
|
| 681 |
height=400,
|
|
@@ -690,13 +695,13 @@ def create_temporal_visualizations(df: pd.DataFrame) -> Dict[str, go.Figure]:
|
|
| 690 |
top_flairs = df['flair'].value_counts().head(10).index
|
| 691 |
flair_time_filtered = flair_time[flair_time['flair'].isin(top_flairs)]
|
| 692 |
|
| 693 |
-
fig_flair = px.line(flair_time_filtered, x='created_utc', y='count',
|
| 694 |
-
color='flair',
|
| 695 |
title=f'Weekly Posting Patterns by Flair ({date_min.strftime("%Y-%m-%d")} to {date_max.strftime("%Y-%m-%d")})',
|
| 696 |
labels={'count': 'Number of Posts', 'created_utc': 'Week'})
|
| 697 |
fig_flair.update_layout(
|
| 698 |
height=400,
|
| 699 |
-
xaxis=dict(range=[
|
| 700 |
)
|
| 701 |
figures['flair'] = fig_flair
|
| 702 |
|
|
@@ -1063,31 +1068,31 @@ def main():
|
|
| 1063 |
temporal_figs = create_temporal_visualizations(df)
|
| 1064 |
|
| 1065 |
# Activity heatmap
|
| 1066 |
-
st.plotly_chart(temporal_figs.get('heatmap'),
|
| 1067 |
-
|
| 1068 |
# Time series
|
| 1069 |
-
st.plotly_chart(temporal_figs.get('timeline'),
|
| 1070 |
-
|
| 1071 |
# Monthly trends
|
| 1072 |
col1, col2 = st.columns(2)
|
| 1073 |
with col1:
|
| 1074 |
-
st.plotly_chart(temporal_figs.get('monthly'),
|
| 1075 |
with col2:
|
| 1076 |
if 'flair' in temporal_figs:
|
| 1077 |
-
st.plotly_chart(temporal_figs.get('flair'),
|
| 1078 |
|
| 1079 |
with tab2:
|
| 1080 |
st.header("Engagement Analysis")
|
| 1081 |
|
| 1082 |
engagement_figs = create_engagement_analytics(df)
|
| 1083 |
-
|
| 1084 |
col1, col2 = st.columns(2)
|
| 1085 |
with col1:
|
| 1086 |
-
st.plotly_chart(engagement_figs['score_dist'],
|
| 1087 |
with col2:
|
| 1088 |
-
st.plotly_chart(engagement_figs['correlation'],
|
| 1089 |
-
|
| 1090 |
-
st.plotly_chart(engagement_figs['top_posts'],
|
| 1091 |
|
| 1092 |
with tab3:
|
| 1093 |
st.header("Raw Data View")
|
|
|
|
| 510 |
df['created_utc'] = pd.to_datetime(df['created_utc'])
|
| 511 |
df = df.sort_values('created_utc')
|
| 512 |
|
| 513 |
+
# Get actual date range of collected data with padding
|
| 514 |
date_min = df['created_utc'].min()
|
| 515 |
date_max = df['created_utc'].max()
|
| 516 |
date_range = (date_max - date_min).days
|
| 517 |
+
|
| 518 |
+
# Add 2% padding to prevent edge clipping
|
| 519 |
+
padding = pd.Timedelta(days=max(1, int(date_range * 0.02)))
|
| 520 |
+
date_min_padded = date_min - padding
|
| 521 |
+
date_max_padded = date_max + padding
|
| 522 |
|
| 523 |
# 1. Hourly activity heatmap
|
| 524 |
df['hour'] = df['created_utc'].dt.hour
|
|
|
|
| 598 |
)
|
| 599 |
|
| 600 |
fig_timeline.update_layout(
|
| 601 |
+
height=600,
|
| 602 |
showlegend=True,
|
| 603 |
title=f'Activity Timeline ({date_min.strftime("%Y-%m-%d")} to {date_max.strftime("%Y-%m-%d")})'
|
| 604 |
)
|
| 605 |
fig_timeline.update_xaxes(
|
| 606 |
+
title_text="Date",
|
| 607 |
row=2, col=1,
|
| 608 |
+
range=[date_min_padded, date_max_padded]
|
| 609 |
)
|
| 610 |
fig_timeline.update_xaxes(
|
| 611 |
+
range=[date_min_padded, date_max_padded],
|
| 612 |
row=1, col=1
|
| 613 |
)
|
| 614 |
fig_timeline.update_yaxes(title_text="Count", row=1, col=1)
|
|
|
|
| 644 |
fig_monthly.update_layout(
|
| 645 |
title=f'Monthly Posting Trends ({date_min.strftime("%Y-%m")} to {date_max.strftime("%Y-%m")})',
|
| 646 |
xaxis_title='Month',
|
| 647 |
+
xaxis=dict(range=[date_min_padded, date_max_padded]),
|
| 648 |
yaxis=dict(title='Post Count', side='left'),
|
| 649 |
yaxis2=dict(title='Average Score', side='right', overlaying='y'),
|
| 650 |
height=400,
|
|
|
|
| 680 |
fig_weekly.update_layout(
|
| 681 |
title=f'Weekly Posting Trends ({date_min.strftime("%Y-%m-%d")} to {date_max.strftime("%Y-%m-%d")})',
|
| 682 |
xaxis_title='Week',
|
| 683 |
+
xaxis=dict(range=[date_min_padded, date_max_padded]),
|
| 684 |
yaxis=dict(title='Post Count', side='left'),
|
| 685 |
yaxis2=dict(title='Average Score', side='right', overlaying='y'),
|
| 686 |
height=400,
|
|
|
|
| 695 |
top_flairs = df['flair'].value_counts().head(10).index
|
| 696 |
flair_time_filtered = flair_time[flair_time['flair'].isin(top_flairs)]
|
| 697 |
|
| 698 |
+
fig_flair = px.line(flair_time_filtered, x='created_utc', y='count',
|
| 699 |
+
color='flair',
|
| 700 |
title=f'Weekly Posting Patterns by Flair ({date_min.strftime("%Y-%m-%d")} to {date_max.strftime("%Y-%m-%d")})',
|
| 701 |
labels={'count': 'Number of Posts', 'created_utc': 'Week'})
|
| 702 |
fig_flair.update_layout(
|
| 703 |
height=400,
|
| 704 |
+
xaxis=dict(range=[date_min_padded, date_max_padded])
|
| 705 |
)
|
| 706 |
figures['flair'] = fig_flair
|
| 707 |
|
|
|
|
| 1068 |
temporal_figs = create_temporal_visualizations(df)
|
| 1069 |
|
| 1070 |
# Activity heatmap
|
| 1071 |
+
st.plotly_chart(temporal_figs.get('heatmap'), use_container_width=True)
|
| 1072 |
+
|
| 1073 |
# Time series
|
| 1074 |
+
st.plotly_chart(temporal_figs.get('timeline'), use_container_width=True)
|
| 1075 |
+
|
| 1076 |
# Monthly trends
|
| 1077 |
col1, col2 = st.columns(2)
|
| 1078 |
with col1:
|
| 1079 |
+
st.plotly_chart(temporal_figs.get('monthly'), use_container_width=True)
|
| 1080 |
with col2:
|
| 1081 |
if 'flair' in temporal_figs:
|
| 1082 |
+
st.plotly_chart(temporal_figs.get('flair'), use_container_width=True)
|
| 1083 |
|
| 1084 |
with tab2:
|
| 1085 |
st.header("Engagement Analysis")
|
| 1086 |
|
| 1087 |
engagement_figs = create_engagement_analytics(df)
|
| 1088 |
+
|
| 1089 |
col1, col2 = st.columns(2)
|
| 1090 |
with col1:
|
| 1091 |
+
st.plotly_chart(engagement_figs['score_dist'], use_container_width=True)
|
| 1092 |
with col2:
|
| 1093 |
+
st.plotly_chart(engagement_figs['correlation'], use_container_width=True)
|
| 1094 |
+
|
| 1095 |
+
st.plotly_chart(engagement_figs['top_posts'], use_container_width=True)
|
| 1096 |
|
| 1097 |
with tab3:
|
| 1098 |
st.header("Raw Data View")
|