GuglielmoTor commited on
Commit
c47a4ee
·
verified ·
1 Parent(s): 4e82b79

Update ui_generators.py

Browse files
Files changed (1) hide show
  1. ui_generators.py +63 -40
ui_generators.py CHANGED
@@ -308,51 +308,74 @@ def run_follower_stats_tab_display(token_state):
308
 
309
 
310
  # --- NEW UI GENERATION LOGIC FOR ANALYTICS TAB ---
311
- def create_analytics_plot_ui_row(label, plot_id_str):
312
  """
313
- Creates a Gradio Row with a Plot component, a Bomb (insights) button,
314
- and a hidden Column for insights.
 
315
  """
316
- # This function will be called within a gr.Blocks() context in app.py
317
- with gr.Row(equal_height=False):
318
- with gr.Column(scale=7): # Main column for the plot
319
- plot_component = gr.Plot(label=label)
320
- with gr.Column(scale=1, min_width=60): # Smaller column for the bomb button
321
- bomb_button = gr.Button("💣", variant="secondary", size="sm", elem_id=f"bomb_{plot_id_str}")
322
- # Insights column, initially hidden, appears to the right
323
- with gr.Column(scale=4, visible=False, elem_id=f"insights_col_{plot_id_str}") as insights_col_ui:
324
- gr.Markdown(f"##### Insights: {label}") # Title for the insights panel
325
- insights_markdown_ui = gr.Markdown(f"Click 💣 for insights on {label}...") # Placeholder text
326
- return plot_component, bomb_button, insights_col_ui, insights_markdown_ui
327
-
328
- def build_analytics_tab_ui_components(plot_configs):
329
  """
330
- Builds and returns all UI components for the analytics plots based on plot_configs.
331
- This function should be called within the "Analytics" TabItem context in app.py.
332
- It will create the section headers and the plot rows.
 
333
  """
334
- logging.info(f"Building UI components for {len(plot_configs)} analytics plots.")
335
- plot_ui_objects = {} # Stores {"plot_id": {"plot": gr.Plot, "bomb": gr.Button, "insights_col": gr.Column, "insights_md": gr.Markdown, "label": str}}
336
 
337
  current_section_title = ""
338
- for config in plot_configs:
339
- if config["section"] != current_section_title:
340
- gr.Markdown(f"### {config['section']}") # Create section header
341
- current_section_title = config["section"]
342
-
343
- # Create the plot row using the helper
344
- plot_component, bomb_button, insights_col_ui, insights_markdown_ui = \
345
- create_analytics_plot_ui_row(config["label"], config["id"])
346
-
347
- # Store the created components in the dictionary
348
- plot_ui_objects[config["id"]] = {
349
- "plot": plot_component,
350
- "bomb": bomb_button,
351
- "insights_col": insights_col_ui,
352
- "insights_md": insights_markdown_ui,
353
- "label": config["label"] # Store label for easy access, e.g., for insights text
354
- }
355
- logging.debug(f"Created UI for plot_id: {config['id']} in section: {config['section']}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356
 
357
- logging.info(f"Finished building UI components for analytics tab. Total objects: {len(plot_ui_objects)}")
358
  return plot_ui_objects
 
308
 
309
 
310
  # --- NEW UI GENERATION LOGIC FOR ANALYTICS TAB ---
311
+ def create_analytics_plot_panel(label, plot_id_str):
312
  """
313
+ Creates a Gradio Column representing a single plot panel.
314
+ This panel contains the plot and its associated "bomb" button to the right.
315
+ Returns the panel (gr.Column), the plot component, and the bomb button.
316
  """
317
+ with gr.Column() as panel_col: # This is the component that will go into the two-column rows
318
+ with gr.Row(equal_height=False, variant="panel"): # A sub-row for plot and button
319
+ with gr.Column(scale=8): # Adjust scale as needed for plot vs button
320
+ plot_component = gr.Plot(label=label)
321
+ with gr.Column(scale=2, min_width=70, vertical_alignment="center"): # min_width for button, try to center vertically
322
+ bomb_button = gr.Button("💣", variant="secondary", size="sm", elem_id=f"bomb_{plot_id_str}")
323
+ return panel_col, plot_component, bomb_button
324
+
325
+ def build_analytics_tab_plot_area(plot_configs):
 
 
 
 
326
  """
327
+ Builds the main plot area for the Analytics tab, arranging plot panels into rows of two.
328
+ This function should be called within the "Analytics" TabItem context in app.py,
329
+ specifically within the main column designated for plots.
330
+ Returns a dictionary of plot UI objects.
331
  """
332
+ logging.info(f"Building plot area for {len(plot_configs)} analytics plots.")
333
+ plot_ui_objects = {} # Stores {"plot_id": {"plot_component": gr.Plot, "bomb_button": gr.Button, "label": str}}
334
 
335
  current_section_title = ""
336
+
337
+ # Iterate through plot_configs to create sections and then rows of plots
338
+ for i in range(0, len(plot_configs), 2): # Process in pairs for two plots per row
339
+ # Handle section header
340
+ # Check if the section of the first plot in the potential pair is new
341
+ if plot_configs[i]["section"] != current_section_title:
342
+ current_section_title = plot_configs[i]["section"]
343
+ gr.Markdown(f"### {current_section_title}")
344
+
345
+ with gr.Row(): # Create a new row for up to two plot panels
346
+ # First plot in the pair
347
+ config1 = plot_configs[i]
348
+ panel_col1, plot_component1, bomb_button1 = \
349
+ create_analytics_plot_panel(config1["label"], config1["id"])
350
+ plot_ui_objects[config1["id"]] = {
351
+ "plot_component": plot_component1,
352
+ "bomb_button": bomb_button1,
353
+ "label": config1["label"]
354
+ }
355
+ logging.debug(f"Created UI panel for plot_id: {config1['id']}")
356
+
357
+ # Second plot in the pair, if it exists
358
+ if i + 1 < len(plot_configs):
359
+ config2 = plot_configs[i+1]
360
+ # Check if the second plot starts a new section (edge case if sections aren't aligned with pairs)
361
+ if config2["section"] != current_section_title:
362
+ # This row is finished, start a new section header for the next plot
363
+ # This might leave the current row with only one plot if sections change mid-pair
364
+ # For simplicity, we assume sections align well or the header appears before the row with the new section's first plot
365
+ # The current loop structure will create a new header before the next gr.Row if section changes
366
+ pass # The next iteration's section check will handle this.
367
+
368
+ panel_col2, plot_component2, bomb_button2 = \
369
+ create_analytics_plot_panel(config2["label"], config2["id"])
370
+ plot_ui_objects[config2["id"]] = {
371
+ "plot_component": plot_component2,
372
+ "bomb_button": bomb_button2,
373
+ "label": config2["label"]
374
+ }
375
+ logging.debug(f"Created UI panel for plot_id: {config2['id']}")
376
+ else:
377
+ # If there's no second plot, this row will just have one panel
378
+ pass
379
 
380
+ logging.info(f"Finished building plot area. Total plot objects: {len(plot_ui_objects)}")
381
  return plot_ui_objects