Spaces:
Running
Running
Update ui_generators.py
Browse files- ui_generators.py +74 -35
ui_generators.py
CHANGED
@@ -24,9 +24,9 @@ from config import (
|
|
24 |
# logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(module)s - %(message)s')
|
25 |
|
26 |
# --- Constants for Button Icons/Text ---
|
27 |
-
BOMB_ICON = "π£
|
28 |
-
EXPLORE_ICON = "
|
29 |
-
FORMULA_ICON = "Ζ
|
30 |
ACTIVE_ICON = "β Close"
|
31 |
|
32 |
|
@@ -324,71 +324,110 @@ def run_follower_stats_tab_display(token_state):
|
|
324 |
|
325 |
|
326 |
# --- UI GENERATION LOGIC FOR ANALYTICS TAB ---
|
327 |
-
def create_analytics_plot_panel(
|
328 |
"""
|
329 |
-
Creates
|
330 |
-
|
331 |
-
Returns:
|
332 |
-
panel_col (gr.Column): The main column for this plot panel.
|
333 |
-
plot_component (gr.Plot): The plot display area.
|
334 |
-
bomb_button (gr.Button): Insights button.
|
335 |
-
explore_button (gr.Button): Explore/Zoom button.
|
336 |
-
formula_button (gr.Button): Formula button.
|
337 |
"""
|
338 |
-
with gr.Column() as
|
339 |
-
with gr.Row(
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
bomb_button = gr.Button(
|
345 |
-
|
346 |
-
|
347 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
348 |
|
349 |
def build_analytics_tab_plot_area(plot_configs):
|
350 |
"""
|
351 |
Builds the main plot area for the Analytics tab, arranging plot panels into rows of two.
|
352 |
-
|
|
|
|
|
|
|
353 |
"""
|
354 |
logging.info(f"Building plot area for {len(plot_configs)} analytics plots.")
|
355 |
plot_ui_objects = {}
|
356 |
-
|
|
|
357 |
current_section_title = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
358 |
i = 0
|
|
|
|
|
359 |
while i < len(plot_configs):
|
360 |
config1 = plot_configs[i]
|
361 |
-
|
362 |
-
#
|
363 |
-
if config1["section"] !=
|
364 |
current_section_title = config1["section"]
|
365 |
-
|
|
|
|
|
366 |
|
367 |
-
|
|
|
|
|
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"]] = {
|
371 |
"plot_component": plot_comp1, "bomb_button": bomb_btn1,
|
372 |
"explore_button": explore_btn1, "formula_button": formula_btn1,
|
373 |
-
"label": config1["label"], "panel_component": panel_col1
|
|
|
374 |
}
|
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:
|
381 |
panel_col2, plot_comp2, bomb_btn2, explore_btn2, formula_btn2 = \
|
382 |
create_analytics_plot_panel(config2["label"], config2["id"])
|
383 |
plot_ui_objects[config2["id"]] = {
|
384 |
"plot_component": plot_comp2, "bomb_button": bomb_btn2,
|
385 |
"explore_button": explore_btn2, "formula_button": formula_btn2,
|
386 |
-
"label": config2["label"], "panel_component": panel_col2
|
|
|
387 |
}
|
388 |
logging.debug(f"Created UI panel for plot_id: {config2['id']} in same row")
|
389 |
i += 1
|
390 |
-
|
391 |
-
|
|
|
|
|
392 |
if len(plot_ui_objects) != len(plot_configs):
|
393 |
logging.error(f"MISMATCH: Expected {len(plot_configs)} plot objects, but created {len(plot_ui_objects)}.")
|
394 |
-
|
|
|
|
|
|
24 |
# logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(module)s - %(message)s')
|
25 |
|
26 |
# --- Constants for Button Icons/Text ---
|
27 |
+
BOMB_ICON = "π£"
|
28 |
+
EXPLORE_ICON = "π§"
|
29 |
+
FORMULA_ICON = "Ζ"
|
30 |
ACTIVE_ICON = "β Close"
|
31 |
|
32 |
|
|
|
324 |
|
325 |
|
326 |
# --- UI GENERATION LOGIC FOR ANALYTICS TAB ---
|
327 |
+
def create_analytics_plot_panel(plot_label_str, plot_id_str):
|
328 |
"""
|
329 |
+
Creates an individual plot panel with its plot component and action buttons.
|
330 |
+
Returns the panel (Column), plot component, and button components.
|
|
|
|
|
|
|
|
|
|
|
|
|
331 |
"""
|
332 |
+
with gr.Column(visible=True) as panel_component: # Main container for this plot
|
333 |
+
with gr.Row(variant="compact"):
|
334 |
+
gr.Markdown(f"#### {plot_label_str}") # Plot title
|
335 |
+
with gr.Row(elem_classes="plot-actions", scale=0): # Action buttons container
|
336 |
+
# Ensure icons are defined (e.g., BOMB_ICON, EXPLORE_ICON, FORMULA_ICON)
|
337 |
+
# If not defined globally, pass them or use placeholders
|
338 |
+
bomb_button = gr.Button(value="π£", variant="tool", size="sm", min_width=30, elem_id=f"bomb_btn_{plot_id_str}")
|
339 |
+
formula_button = gr.Button(value="Ζ", variant="tool", size="sm", min_width=30, elem_id=f"formula_btn_{plot_id_str}")
|
340 |
+
explore_button = gr.Button(value="π§", variant="tool", size="sm", min_width=30, elem_id=f"explore_btn_{plot_id_str}")
|
341 |
+
|
342 |
+
# Placeholder for the actual plot - this will be updated by the analytics generation logic
|
343 |
+
plot_component = gr.Plot(label=plot_label_str, show_label=False)
|
344 |
+
# Example of adding some specific styling or structure if needed
|
345 |
+
# gr.HTML(f"<div class='plot-container' id='plot_container_{plot_id_str}'></div>") # If you were embedding plots differently
|
346 |
+
|
347 |
+
logging.debug(f"Created analytics panel for: {plot_label_str} (ID: {plot_id_str})")
|
348 |
+
return panel_component, plot_component, bomb_button, explore_button, formula_button
|
349 |
+
|
350 |
|
351 |
def build_analytics_tab_plot_area(plot_configs):
|
352 |
"""
|
353 |
Builds the main plot area for the Analytics tab, arranging plot panels into rows of two.
|
354 |
+
|
355 |
+
Returns a tuple:
|
356 |
+
- plot_ui_objects (dict): Dictionary of plot UI objects.
|
357 |
+
- section_titles_map (dict): Dictionary mapping section names to their gr.Markdown title components.
|
358 |
"""
|
359 |
logging.info(f"Building plot area for {len(plot_configs)} analytics plots.")
|
360 |
plot_ui_objects = {}
|
361 |
+
section_titles_map = {} # NEW: To store section title Markdown components
|
362 |
+
|
363 |
current_section_title = ""
|
364 |
+
|
365 |
+
# Determine unique sections in order to correctly create Markdown components for them
|
366 |
+
# This ensures that section_titles_map will have keys for all sections defined in plot_configs
|
367 |
+
# and that they are created in the UI in the correct order.
|
368 |
+
|
369 |
+
# First pass to create all section title Markdown objects and store them
|
370 |
+
# This is to ensure they are Gradio components that can be targeted for visibility updates.
|
371 |
+
# We use a temporary list to hold sections already processed to avoid duplicate Markdown objects for the same section.
|
372 |
+
processed_sections_for_md_creation = []
|
373 |
+
for config in plot_configs:
|
374 |
+
section_name = config["section"]
|
375 |
+
if section_name not in processed_sections_for_md_creation:
|
376 |
+
# Create the Markdown component for the section title
|
377 |
+
# The actual rendering order will be handled in the layout loop below
|
378 |
+
section_md_component = gr.Markdown(f"### {section_name}", visible=True) # Initially visible
|
379 |
+
section_titles_map[section_name] = section_md_component
|
380 |
+
processed_sections_for_md_creation.append(section_name)
|
381 |
+
logging.debug(f"Created Markdown component for section title: {section_name}")
|
382 |
+
|
383 |
+
# Layout loop: Iterate through plot_configs to place section titles and plot panels
|
384 |
i = 0
|
385 |
+
last_rendered_section_title = None # Keep track of the last section title that was actually rendered
|
386 |
+
|
387 |
while i < len(plot_configs):
|
388 |
config1 = plot_configs[i]
|
389 |
+
|
390 |
+
# Render the section title if it's a new section and hasn't been rendered yet in this layout pass
|
391 |
+
if config1["section"] != last_rendered_section_title:
|
392 |
current_section_title = config1["section"]
|
393 |
+
|
394 |
+
last_rendered_section_title = current_section_title
|
395 |
+
logging.info(f"Processing section: {current_section_title}")
|
396 |
|
397 |
+
|
398 |
+
# Create row for one or two plot panels
|
399 |
+
with gr.Row(equal_height=False):
|
400 |
panel_col1, plot_comp1, bomb_btn1, explore_btn1, formula_btn1 = \
|
401 |
create_analytics_plot_panel(config1["label"], config1["id"])
|
402 |
plot_ui_objects[config1["id"]] = {
|
403 |
"plot_component": plot_comp1, "bomb_button": bomb_btn1,
|
404 |
"explore_button": explore_btn1, "formula_button": formula_btn1,
|
405 |
+
"label": config1["label"], "panel_component": panel_col1,
|
406 |
+
"section": config1["section"] # Store section for easier lookup
|
407 |
}
|
408 |
logging.debug(f"Created UI panel for plot_id: {config1['id']}")
|
409 |
i += 1
|
410 |
+
|
411 |
if i < len(plot_configs):
|
412 |
config2 = plot_configs[i]
|
413 |
+
# Check if the next plot is in the SAME section to place it in the same row
|
414 |
if config2["section"] == current_section_title:
|
415 |
panel_col2, plot_comp2, bomb_btn2, explore_btn2, formula_btn2 = \
|
416 |
create_analytics_plot_panel(config2["label"], config2["id"])
|
417 |
plot_ui_objects[config2["id"]] = {
|
418 |
"plot_component": plot_comp2, "bomb_button": bomb_btn2,
|
419 |
"explore_button": explore_btn2, "formula_button": formula_btn2,
|
420 |
+
"label": config2["label"], "panel_component": panel_col2,
|
421 |
+
"section": config2["section"] # Store section
|
422 |
}
|
423 |
logging.debug(f"Created UI panel for plot_id: {config2['id']} in same row")
|
424 |
i += 1
|
425 |
+
# If config2 is in a different section, it will be handled by the next iteration of the outer loop
|
426 |
+
# which will then print its own section title.
|
427 |
+
|
428 |
+
logging.info(f"Finished building plot area. Total plot objects: {len(plot_ui_objects)}. Section titles created: {len(section_titles_map)}")
|
429 |
if len(plot_ui_objects) != len(plot_configs):
|
430 |
logging.error(f"MISMATCH: Expected {len(plot_configs)} plot objects, but created {len(plot_ui_objects)}.")
|
431 |
+
|
432 |
+
# Return both the plot UI objects and the map of section title components
|
433 |
+
return plot_ui_objects, section_titles_map
|