GuglielmoTor commited on
Commit
6e6119d
·
verified ·
1 Parent(s): 7ec7117

Update ui_generators.py

Browse files
Files changed (1) hide show
  1. ui_generators.py +23 -19
ui_generators.py CHANGED
@@ -147,23 +147,25 @@ def run_mentions_tab_display(token_state):
147
  html_parts.append(mentions_df_display[display_columns].head(20).to_html(escape=False, index=False, classes="table table-sm"))
148
 
149
  mentions_html_output = "\n".join(html_parts)
150
- fig = None # Initialize fig to None
151
- fig_plot_local = None # Use a local variable for the figure object
152
  if not mentions_df.empty and "sentiment_label" in mentions_df.columns:
153
  try:
154
  fig_plot_local, ax = plt.subplots(figsize=(6,4))
155
  sentiment_counts = mentions_df["sentiment_label"].value_counts()
156
  sentiment_counts.plot(kind='bar', ax=ax, color=['#4CAF50', '#FFC107', '#F44336', '#9E9E9E', '#2196F3'])
157
- ax.set_title("Mention Sentiment Distribution", y=1.08) # MODIFIED: Increased y parameter
158
  ax.set_ylabel("Count")
159
  plt.xticks(rotation=45, ha='right')
160
- # fig_plot_local.subplots_adjust(top=0.88) # Alternative: Adjust top margin if y isn't enough
161
- plt.tight_layout() # Ensure tight_layout is called
162
- fig = fig_plot_local # Assign to fig for returning
 
 
163
  logging.info("Mentions tab: Sentiment distribution plot generated.")
164
  except Exception as e:
165
  logging.error(f"Error generating mentions plot: {e}", exc_info=True)
166
- fig = None # Ensure fig is None on error
167
  finally:
168
  if fig_plot_local and fig_plot_local is not plt:
169
  plt.close(fig_plot_local)
@@ -221,14 +223,15 @@ def run_follower_stats_tab_display(token_state):
221
  fig_gains_local, ax_gains = plt.subplots(figsize=(10,5))
222
  ax_gains.plot(plot_data['_plot_month'], plot_data['organic'], marker='o', linestyle='-', label='Organic Gain')
223
  ax_gains.plot(plot_data['_plot_month'], plot_data['paid'], marker='x', linestyle='--', label='Paid Gain')
224
- ax_gains.set_title("Monthly Follower Gains Over Time", y=1.08) # MODIFIED
225
  ax_gains.set_ylabel("Follower Count")
226
  ax_gains.set_xlabel("Month (YYYY-MM)")
227
  plt.xticks(rotation=45, ha='right')
228
  ax_gains.legend()
229
  plt.grid(True, linestyle='--', alpha=0.7)
230
- # fig_gains_local.subplots_adjust(top=0.88) # Alternative adjustment
231
  plt.tight_layout()
 
232
  plot_monthly_gains = fig_gains_local
233
  logging.info("Follower stats tab: Monthly gains plot generated.")
234
  else:
@@ -259,12 +262,13 @@ def run_follower_stats_tab_display(token_state):
259
  fig_seniority_local, ax_seniority = plt.subplots(figsize=(8,5))
260
  top_n_seniority = seniority_df_sorted.nlargest(10, FOLLOWER_STATS_ORGANIC_COLUMN)
261
  ax_seniority.bar(top_n_seniority[FOLLOWER_STATS_CATEGORY_COLUMN], top_n_seniority[FOLLOWER_STATS_ORGANIC_COLUMN], color='skyblue')
262
- ax_seniority.set_title("Follower Distribution by Seniority (Top 10 Organic)", y=1.08) # MODIFIED
263
  ax_seniority.set_ylabel("Organic Follower Count")
264
  plt.xticks(rotation=45, ha='right')
265
  plt.grid(axis='y', linestyle='--', alpha=0.7)
266
- # fig_seniority_local.subplots_adjust(top=0.85) # Alternative adjustment
267
  plt.tight_layout()
 
268
  plot_seniority_dist = fig_seniority_local
269
  logging.info("Follower stats tab: Seniority distribution plot generated.")
270
  else:
@@ -294,12 +298,13 @@ def run_follower_stats_tab_display(token_state):
294
  fig_industry_local, ax_industry = plt.subplots(figsize=(8,5))
295
  top_n_industry = industry_df_sorted.nlargest(10, FOLLOWER_STATS_ORGANIC_COLUMN)
296
  ax_industry.bar(top_n_industry[FOLLOWER_STATS_CATEGORY_COLUMN], top_n_industry[FOLLOWER_STATS_ORGANIC_COLUMN], color='lightcoral')
297
- ax_industry.set_title("Follower Distribution by Industry (Top 10 Organic)", y=1.08) # MODIFIED
298
  ax_industry.set_ylabel("Organic Follower Count")
299
  plt.xticks(rotation=45, ha='right')
300
  plt.grid(axis='y', linestyle='--', alpha=0.7)
301
- # fig_industry_local.subplots_adjust(top=0.85) # Alternative adjustment
302
  plt.tight_layout()
 
303
  plot_industry_dist = fig_industry_local
304
  logging.info("Follower stats tab: Industry distribution plot generated.")
305
  else:
@@ -330,9 +335,11 @@ def create_analytics_plot_panel(label, plot_id_str):
330
  explore_button (gr.Button): Explore/Zoom button.
331
  formula_button (gr.Button): Formula button.
332
  """
333
- with gr.Column() as panel_col: # This will be the component that gets hidden/shown by explore
334
  with gr.Row(equal_height=False, variant="panel"):
335
- plot_component = gr.Plot(label=label, scale=8) # The label here is for Gradio, not the Matplotlib title
 
 
336
  with gr.Column(scale=2, min_width=100):
337
  bomb_button = gr.Button(BOMB_ICON, variant="secondary", size="sm", elem_id=f"bomb_{plot_id_str}")
338
  explore_button = gr.Button(EXPLORE_ICON, variant="secondary", size="sm", elem_id=f"explore_{plot_id_str}")
@@ -355,11 +362,9 @@ def build_analytics_tab_plot_area(plot_configs):
355
  # Start a new section if necessary
356
  if config1["section"] != current_section_title:
357
  current_section_title = config1["section"]
358
- gr.Markdown(f"### {current_section_title}") # Section title
359
 
360
- # Create a new row for each pair of plots or a single plot
361
  with gr.Row(equal_height=False):
362
- # Process the first plot of the potential pair
363
  panel_col1, plot_comp1, bomb_btn1, explore_btn1, formula_btn1 = \
364
  create_analytics_plot_panel(config1["label"], config1["id"])
365
  plot_ui_objects[config1["id"]] = {
@@ -370,7 +375,6 @@ def build_analytics_tab_plot_area(plot_configs):
370
  logging.debug(f"Created UI panel for plot_id: {config1['id']}")
371
  i += 1
372
 
373
- # Check if there's a second plot for this row and if it's in the same section
374
  if i < len(plot_configs):
375
  config2 = plot_configs[i]
376
  if config2["section"] == current_section_title:
 
147
  html_parts.append(mentions_df_display[display_columns].head(20).to_html(escape=False, index=False, classes="table table-sm"))
148
 
149
  mentions_html_output = "\n".join(html_parts)
150
+ fig = None
151
+ fig_plot_local = None
152
  if not mentions_df.empty and "sentiment_label" in mentions_df.columns:
153
  try:
154
  fig_plot_local, ax = plt.subplots(figsize=(6,4))
155
  sentiment_counts = mentions_df["sentiment_label"].value_counts()
156
  sentiment_counts.plot(kind='bar', ax=ax, color=['#4CAF50', '#FFC107', '#F44336', '#9E9E9E', '#2196F3'])
157
+ ax.set_title("Mention Sentiment Distribution", y=1.03) # Adjusted y for Matplotlib title
158
  ax.set_ylabel("Count")
159
  plt.xticks(rotation=45, ha='right')
160
+
161
+ plt.tight_layout()
162
+ fig_plot_local.subplots_adjust(top=0.90) # MODIFIED: Add space at the top of the figure
163
+ # This pushes plot content down from Gradio's label
164
+ fig = fig_plot_local
165
  logging.info("Mentions tab: Sentiment distribution plot generated.")
166
  except Exception as e:
167
  logging.error(f"Error generating mentions plot: {e}", exc_info=True)
168
+ fig = None
169
  finally:
170
  if fig_plot_local and fig_plot_local is not plt:
171
  plt.close(fig_plot_local)
 
223
  fig_gains_local, ax_gains = plt.subplots(figsize=(10,5))
224
  ax_gains.plot(plot_data['_plot_month'], plot_data['organic'], marker='o', linestyle='-', label='Organic Gain')
225
  ax_gains.plot(plot_data['_plot_month'], plot_data['paid'], marker='x', linestyle='--', label='Paid Gain')
226
+ ax_gains.set_title("Monthly Follower Gains Over Time", y=1.03) # Adjusted y for Matplotlib title
227
  ax_gains.set_ylabel("Follower Count")
228
  ax_gains.set_xlabel("Month (YYYY-MM)")
229
  plt.xticks(rotation=45, ha='right')
230
  ax_gains.legend()
231
  plt.grid(True, linestyle='--', alpha=0.7)
232
+
233
  plt.tight_layout()
234
+ fig_gains_local.subplots_adjust(top=0.90) # MODIFIED: Add space at the top of the figure
235
  plot_monthly_gains = fig_gains_local
236
  logging.info("Follower stats tab: Monthly gains plot generated.")
237
  else:
 
262
  fig_seniority_local, ax_seniority = plt.subplots(figsize=(8,5))
263
  top_n_seniority = seniority_df_sorted.nlargest(10, FOLLOWER_STATS_ORGANIC_COLUMN)
264
  ax_seniority.bar(top_n_seniority[FOLLOWER_STATS_CATEGORY_COLUMN], top_n_seniority[FOLLOWER_STATS_ORGANIC_COLUMN], color='skyblue')
265
+ ax_seniority.set_title("Follower Distribution by Seniority (Top 10 Organic)", y=1.03) # Adjusted y
266
  ax_seniority.set_ylabel("Organic Follower Count")
267
  plt.xticks(rotation=45, ha='right')
268
  plt.grid(axis='y', linestyle='--', alpha=0.7)
269
+
270
  plt.tight_layout()
271
+ fig_seniority_local.subplots_adjust(top=0.88) # MODIFIED: Add space. Experiment with this value.
272
  plot_seniority_dist = fig_seniority_local
273
  logging.info("Follower stats tab: Seniority distribution plot generated.")
274
  else:
 
298
  fig_industry_local, ax_industry = plt.subplots(figsize=(8,5))
299
  top_n_industry = industry_df_sorted.nlargest(10, FOLLOWER_STATS_ORGANIC_COLUMN)
300
  ax_industry.bar(top_n_industry[FOLLOWER_STATS_CATEGORY_COLUMN], top_n_industry[FOLLOWER_STATS_ORGANIC_COLUMN], color='lightcoral')
301
+ ax_industry.set_title("Follower Distribution by Industry (Top 10 Organic)", y=1.03) # Adjusted y
302
  ax_industry.set_ylabel("Organic Follower Count")
303
  plt.xticks(rotation=45, ha='right')
304
  plt.grid(axis='y', linestyle='--', alpha=0.7)
305
+
306
  plt.tight_layout()
307
+ fig_industry_local.subplots_adjust(top=0.88) # MODIFIED: Add space. Experiment with this value.
308
  plot_industry_dist = fig_industry_local
309
  logging.info("Follower stats tab: Industry distribution plot generated.")
310
  else:
 
335
  explore_button (gr.Button): Explore/Zoom button.
336
  formula_button (gr.Button): Formula button.
337
  """
338
+ with gr.Column() as panel_col:
339
  with gr.Row(equal_height=False, variant="panel"):
340
+ # The gr.Plot's label is what you're referring to.
341
+ # We will adjust spacing using Matplotlib's subplots_adjust in the plot generation functions.
342
+ plot_component = gr.Plot(label=label, scale=8)
343
  with gr.Column(scale=2, min_width=100):
344
  bomb_button = gr.Button(BOMB_ICON, variant="secondary", size="sm", elem_id=f"bomb_{plot_id_str}")
345
  explore_button = gr.Button(EXPLORE_ICON, variant="secondary", size="sm", elem_id=f"explore_{plot_id_str}")
 
362
  # Start a new section if necessary
363
  if config1["section"] != current_section_title:
364
  current_section_title = config1["section"]
365
+ gr.Markdown(f"### {current_section_title}")
366
 
 
367
  with gr.Row(equal_height=False):
 
368
  panel_col1, plot_comp1, bomb_btn1, explore_btn1, formula_btn1 = \
369
  create_analytics_plot_panel(config1["label"], config1["id"])
370
  plot_ui_objects[config1["id"]] = {
 
375
  logging.debug(f"Created UI panel for plot_id: {config1['id']}")
376
  i += 1
377
 
 
378
  if i < len(plot_configs):
379
  config2 = plot_configs[i]
380
  if config2["section"] == current_section_title: