Update src/streamlit_app.py

#3
by musk12 - opened
Files changed (1) hide show
  1. src/streamlit_app.py +50 -13
src/streamlit_app.py CHANGED
@@ -35,13 +35,22 @@ def render_chart_section(title, func, key_id):
35
  col1, col2 = st.columns([2, 1])
36
  with col1:
37
  fig, ax = plt.subplots()
38
- func() # Ye plotting function execute karega
 
 
 
 
 
 
 
 
39
  if any(x in title for x in ["Venue", "Season", "City"]):
40
  plt.xticks(rotation=45)
41
- st.pyplot(fig, use_container_width=True) # Container width se jumping rukegi
 
 
42
  with col2:
43
  ai_explainer_ui(fig, key_id)
44
- plt.close(fig)
45
 
46
 
47
  # Now, use this variable throughout your app
@@ -143,7 +152,8 @@ def ai_explainer_ui(fig, key_id):
143
  )
144
 
145
  if response.status_code == 200:
146
- st.session_state[state_key] = response.json()['explanation']
 
147
 
148
  st.session_state[state_key] = explanation_text
149
  info_slot.info(explanation_text)
@@ -153,6 +163,28 @@ def ai_explainer_ui(fig, key_id):
153
  st.error(f"Connection Error: {e}")
154
 
155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
 
157
  st.title(f"🏏 {analysis_mode}")
158
  if analysis_mode == "Overview":
@@ -208,11 +240,11 @@ if analysis_mode == "Overview":
208
  # fig1.tight_layout() # ← instance method, not plt.tight_layout()
209
  # st.pyplot(fig1)
210
 
211
- render_chart_section("Matches per Season", fig1, "overview_season")
212
 
213
- plt.close(fig1) # ← free memory immediately
214
- ai_explainer_ui(fig1, "overview_season")
215
- st.divider()
216
 
217
  with st.container(border=True):
218
  st.subheader("Top 10 Run Scoring Teams")
@@ -238,11 +270,11 @@ if analysis_mode == "Overview":
238
  # fig2.tight_layout()
239
  # st.pyplot(fig2)
240
 
241
- render_chart_section("Top 10 Run Scoring Teams", fig2, "overview_teams")
242
- plt.close(fig2)
243
- ai_explainer_ui(fig2, "overview_teams")
244
 
245
- st.divider()
246
 
247
  # ── Run distribution ─────────────────────────────────────────────────────
248
  st.subheader("Run Distribution per Ball")
@@ -384,6 +416,11 @@ elif analysis_mode == "Ball-by-Ball Analysis":
384
  ]
385
 
386
  for i, (title, func) in enumerate(bb_plots):
 
 
 
 
 
387
  # st.subheader(f"{i+1}. {title}")
388
  # col1, col2 = st.columns([2, 1])
389
  # with col1:
@@ -394,5 +431,5 @@ elif analysis_mode == "Ball-by-Ball Analysis":
394
  # with col2:
395
  # ai_explainer_ui(fig, f"bb_btn_{i}")
396
 
397
- render_chart_section(f"{i+1}. {title}", func, f"bb_btn_{i}")
398
  st.divider()
 
35
  col1, col2 = st.columns([2, 1])
36
  with col1:
37
  fig, ax = plt.subplots()
38
+
39
+ # Check if function takes an 'ax' argument or not
40
+ try:
41
+ # Matches per Season / Top Teams logic
42
+ func(ax)
43
+ except TypeError:
44
+ # Lambda plots logic (Seaborn/Pie)
45
+ func()
46
+
47
  if any(x in title for x in ["Venue", "Season", "City"]):
48
  plt.xticks(rotation=45)
49
+
50
+ st.pyplot(fig, use_container_width=True)
51
+ plt.close(fig)
52
  with col2:
53
  ai_explainer_ui(fig, key_id)
 
54
 
55
 
56
  # Now, use this variable throughout your app
 
152
  )
153
 
154
  if response.status_code == 200:
155
+ explanation_text = response.json().get('explanation', "No explanation found.")
156
+ # st.session_state[state_key] = response.json()['explanation']
157
 
158
  st.session_state[state_key] = explanation_text
159
  info_slot.info(explanation_text)
 
163
  st.error(f"Connection Error: {e}")
164
 
165
 
166
+ def plot_matches_per_season(ax):
167
+ season_counts = (
168
+ df2_filtered['season']
169
+ .value_counts()
170
+ .sort_index(key=lambda s: s.map(lambda x: int(x) if str(x).isdigit() else x))
171
+ )
172
+ ax.plot(season_counts.index.astype(str), season_counts.values,
173
+ marker='o', color='#1f77b4', linewidth=2)
174
+ ax.set_xlabel("Season")
175
+ ax.set_ylabel("Matches Played")
176
+
177
+ def plot_top_teams(ax):
178
+ team_runs = (
179
+ df_filtered.groupby('batting_team')['runs_total']
180
+ .sum()
181
+ .sort_values(ascending=False)
182
+ .head(10)
183
+ )
184
+ colors = plt.cm.viridis_r([i/9 for i in range(10)])
185
+ ax.barh(team_runs.index[::-1], team_runs.values[::-1], color=colors)
186
+
187
+
188
 
189
  st.title(f"🏏 {analysis_mode}")
190
  if analysis_mode == "Overview":
 
240
  # fig1.tight_layout() # ← instance method, not plt.tight_layout()
241
  # st.pyplot(fig1)
242
 
243
+ render_chart_section("Matches per Season", plot_matches_per_season, "overview_season")
244
 
245
+ # plt.close(fig1) # ← free memory immediately
246
+ # ai_explainer_ui(fig1, "overview_season")
247
+ st.divider()
248
 
249
  with st.container(border=True):
250
  st.subheader("Top 10 Run Scoring Teams")
 
270
  # fig2.tight_layout()
271
  # st.pyplot(fig2)
272
 
273
+ render_chart_section("Top 10 Run Scoring Teams", plot_top_teams, "overview_teams")
274
+ # plt.close(fig2)
275
+ # ai_explainer_ui(fig2, "overview_teams")
276
 
277
+ st.divider()
278
 
279
  # ── Run distribution ─────────────────────────────────────────────────────
280
  st.subheader("Run Distribution per Ball")
 
416
  ]
417
 
418
  for i, (title, func) in enumerate(bb_plots):
419
+
420
+ def plot_dist(ax):
421
+ run_counts = df_filtered['runs_total'].value_counts().sort_index()
422
+ ax.bar(run_counts.index.astype(str), run_counts.values, color='pink', alpha=0.8)
423
+ ax.set_xlabel("Runs per Ball")
424
  # st.subheader(f"{i+1}. {title}")
425
  # col1, col2 = st.columns([2, 1])
426
  # with col1:
 
431
  # with col2:
432
  # ai_explainer_ui(fig, f"bb_btn_{i}")
433
 
434
+ render_chart_section("Run Distribution per Ball", plot_dist, "overview_distribution")
435
  st.divider()