import gradio as gr import pandas as pd import os import logging import matplotlib matplotlib.use('Agg') # Set backend for Matplotlib to avoid GUI conflicts with Gradio import matplotlib.pyplot as plt # from functools import partial # No longer needed if gr.State(value=plot_id) is used # --- Module Imports --- from gradio_utils import get_url_user_token # Functions from newly created/refactored modules from config import ( LINKEDIN_CLIENT_ID_ENV_VAR, BUBBLE_APP_NAME_ENV_VAR, BUBBLE_API_KEY_PRIVATE_ENV_VAR, BUBBLE_API_ENDPOINT_ENV_VAR ) from state_manager import process_and_store_bubble_token from sync_logic import sync_all_linkedin_data_orchestrator from ui_generators import ( display_main_dashboard, run_mentions_tab_display, run_follower_stats_tab_display, build_analytics_tab_ui_components # Import the new UI builder function ) # Corrected import for analytics_data_processing from analytics_data_processing import prepare_filtered_analytics_data from analytics_plot_generator import ( generate_posts_activity_plot, generate_engagement_type_plot, generate_mentions_activity_plot, generate_mention_sentiment_plot, generate_followers_count_over_time_plot, generate_followers_growth_rate_plot, generate_followers_by_demographics_plot, generate_engagement_rate_over_time_plot, generate_reach_over_time_plot, generate_impressions_over_time_plot, create_placeholder_plot, # For initializing plots generate_likes_over_time_plot, generate_clicks_over_time_plot, generate_shares_over_time_plot, generate_comments_over_time_plot, generate_comments_sentiment_breakdown_plot, generate_post_frequency_plot, generate_content_format_breakdown_plot, generate_content_topic_breakdown_plot ) # Configure logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(module)s - %(message)s') # --- Analytics Tab: Plot Update Function (Original, generates figures) --- def update_analytics_plots_figures(token_state_value, date_filter_option, custom_start_date, custom_end_date): """ Prepares analytics data using external processing function and then generates plot figures. This function is primarily responsible for returning the Matplotlib figure objects. """ logging.info(f"Updating analytics plot figures. Filter: {date_filter_option}, Custom Start: {custom_start_date}, Custom End: {custom_end_date}") num_expected_plots = 23 # This should match the number of plots defined in plot_configs if not token_state_value or not token_state_value.get("token"): message = "❌ Access denied. No token. Cannot generate analytics." logging.warning(message) placeholder_figs = [create_placeholder_plot(title="Access Denied", message="No token.") for _ in range(num_expected_plots)] return [message] + placeholder_figs try: (filtered_merged_posts_df, filtered_mentions_df, date_filtered_follower_stats_df, raw_follower_stats_df, start_dt_for_msg, end_dt_for_msg) = \ prepare_filtered_analytics_data( token_state_value, date_filter_option, custom_start_date, custom_end_date ) except Exception as e: error_msg = f"❌ Error preparing analytics data: {e}" logging.error(error_msg, exc_info=True) placeholder_figs = [create_placeholder_plot(title="Data Preparation Error", message=str(e)) for _ in range(num_expected_plots)] return [error_msg] + placeholder_figs date_column_posts = token_state_value.get("config_date_col_posts", "published_at") date_column_mentions = token_state_value.get("config_date_col_mentions", "date") media_type_col_name = token_state_value.get("config_media_type_col", "media_type") eb_labels_col_name = token_state_value.get("config_eb_labels_col", "eb_labels") logging.info(f"Data for plotting - Filtered Merged Posts: {len(filtered_merged_posts_df)} rows, Filtered Mentions: {len(filtered_mentions_df)} rows.") logging.info(f"Date-Filtered Follower Stats: {len(date_filtered_follower_stats_df)} rows, Raw Follower Stats: {len(raw_follower_stats_df)} rows.") try: plot_figs = [] plot_figs.append(generate_posts_activity_plot(filtered_merged_posts_df, date_column=date_column_posts)) plot_figs.append(generate_engagement_type_plot(filtered_merged_posts_df)) fig_mentions_activity_shared = generate_mentions_activity_plot(filtered_mentions_df, date_column=date_column_mentions) fig_mention_sentiment_shared = generate_mention_sentiment_plot(filtered_mentions_df) plot_figs.append(fig_mentions_activity_shared) # Original mention plot slot 1 plot_figs.append(fig_mention_sentiment_shared) # Original mention plot slot 2 plot_figs.append(generate_followers_count_over_time_plot(date_filtered_follower_stats_df, type_value='follower_gains_monthly')) plot_figs.append(generate_followers_growth_rate_plot(date_filtered_follower_stats_df, type_value='follower_gains_monthly')) plot_figs.append(generate_followers_by_demographics_plot(raw_follower_stats_df, type_value='follower_geo', plot_title="Followers by Location")) plot_figs.append(generate_followers_by_demographics_plot(raw_follower_stats_df, type_value='follower_function', plot_title="Followers by Role")) plot_figs.append(generate_followers_by_demographics_plot(raw_follower_stats_df, type_value='follower_industry', plot_title="Followers by Industry")) plot_figs.append(generate_followers_by_demographics_plot(raw_follower_stats_df, type_value='follower_seniority', plot_title="Followers by Seniority")) plot_figs.append(generate_engagement_rate_over_time_plot(filtered_merged_posts_df, date_column=date_column_posts)) plot_figs.append(generate_reach_over_time_plot(filtered_merged_posts_df, date_column=date_column_posts)) plot_figs.append(generate_impressions_over_time_plot(filtered_merged_posts_df, date_column=date_column_posts)) plot_figs.append(generate_likes_over_time_plot(filtered_merged_posts_df, date_column=date_column_posts)) plot_figs.append(generate_clicks_over_time_plot(filtered_merged_posts_df, date_column=date_column_posts)) plot_figs.append(generate_shares_over_time_plot(filtered_merged_posts_df, date_column=date_column_posts)) plot_figs.append(generate_comments_over_time_plot(filtered_merged_posts_df, date_column=date_column_posts)) plot_figs.append(generate_comments_sentiment_breakdown_plot(filtered_merged_posts_df, sentiment_column='comment_sentiment')) plot_figs.append(generate_post_frequency_plot(filtered_merged_posts_df, date_column=date_column_posts)) plot_figs.append(generate_content_format_breakdown_plot(filtered_merged_posts_df, format_col=media_type_col_name)) plot_figs.append(generate_content_topic_breakdown_plot(filtered_merged_posts_df, topics_col=eb_labels_col_name)) # For the "Mention Analysis" section, we reuse the figures generated earlier plot_figs.append(fig_mentions_activity_shared) # New UI slot for mention volume, reuses figure plot_figs.append(fig_mention_sentiment_shared) # New UI slot for mention sentiment, reuses figure message = f"📊 Analytics updated for period: {date_filter_option}" if date_filter_option == "Custom Range": s_display = start_dt_for_msg.strftime('%Y-%m-%d') if start_dt_for_msg else "Any" e_display = end_dt_for_msg.strftime('%Y-%m-%d') if end_dt_for_msg else "Any" message += f" (From: {s_display} To: {e_display})" final_plot_figs = [] for i, p_fig in enumerate(plot_figs): if p_fig is not None and not isinstance(p_fig, str): final_plot_figs.append(p_fig) else: logging.warning(f"Plot figure generation failed or returned unexpected type for slot {i}, using placeholder. Figure: {p_fig}") final_plot_figs.append(create_placeholder_plot(title="Plot Error", message="Failed to generate this plot figure.")) while len(final_plot_figs) < num_expected_plots: logging.warning(f"Padding missing plot figure with placeholder. Expected {num_expected_plots}, got {len(final_plot_figs)}.") final_plot_figs.append(create_placeholder_plot(title="Missing Plot", message="Plot figure could not be generated.")) logging.info(f"Successfully generated {len(final_plot_figs)} plot figures for {num_expected_plots} UI slots.") return [message] + final_plot_figs[:num_expected_plots] except Exception as e: error_msg = f"❌ Error generating analytics plot figures: {e}" logging.error(error_msg, exc_info=True) placeholder_figs = [create_placeholder_plot(title="Plot Generation Error", message=str(e)) for _ in range(num_expected_plots)] return [error_msg] + placeholder_figs # --- Gradio UI Blocks --- with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), title="LinkedIn Organization Dashboard") as app: token_state = gr.State(value={ "token": None, "client_id": None, "org_urn": None, "bubble_posts_df": pd.DataFrame(), "bubble_post_stats_df": pd.DataFrame(), "bubble_mentions_df": pd.DataFrame(), "bubble_follower_stats_df": pd.DataFrame(), "fetch_count_for_api": 0, "url_user_token_temp_storage": None, "config_date_col_posts": "published_at", "config_date_col_mentions": "date", "config_date_col_followers": "date", "config_media_type_col": "media_type", "config_eb_labels_col": "eb_labels" }) gr.Markdown("# 🚀 LinkedIn Organization Dashboard") url_user_token_display = gr.Textbox(label="User Token (from URL - Hidden)", interactive=False, visible=False) status_box = gr.Textbox(label="Overall LinkedIn Token Status", interactive=False, value="Initializing...") org_urn_display = gr.Textbox(label="Organization URN (from URL - Hidden)", interactive=False, visible=False) app.load(fn=get_url_user_token, inputs=None, outputs=[url_user_token_display, org_urn_display], api_name="get_url_params", show_progress=False) def initial_load_sequence(url_token, org_urn_val, current_state): logging.info(f"Initial load sequence triggered. Org URN: {org_urn_val}, URL Token: {'Present' if url_token else 'Absent'}") status_msg, new_state, btn_update = process_and_store_bubble_token(url_token, org_urn_val, current_state) dashboard_content = display_main_dashboard(new_state) return status_msg, new_state, btn_update, dashboard_content with gr.Tabs() as tabs: with gr.TabItem("1️⃣ Dashboard & Sync", id="tab_dashboard_sync"): gr.Markdown("System checks for existing data from Bubble. The 'Sync' button activates if new data needs to be fetched from LinkedIn based on the last sync times and data availability.") sync_data_btn = gr.Button("🔄 Sync LinkedIn Data", variant="primary", visible=False, interactive=False) sync_status_html_output = gr.HTML("
Sync status will appear here.
") dashboard_display_html = gr.HTML("Dashboard loading...
") org_urn_display.change( fn=initial_load_sequence, inputs=[url_user_token_display, org_urn_display, token_state], outputs=[status_box, token_state, sync_data_btn, dashboard_display_html], show_progress="full" ) with gr.TabItem("2️⃣ Analytics", id="tab_analytics"): gr.Markdown("## 📈 LinkedIn Performance Analytics") gr.Markdown("Select a date range to filter analytics. Click 💣 for insights.") analytics_status_md = gr.Markdown("Analytics status will appear here...") with gr.Row(): date_filter_selector = gr.Radio( ["All Time", "Last 7 Days", "Last 30 Days", "Custom Range"], label="Select Date Range", value="Last 30 Days" ) custom_start_date_picker = gr.DateTime(label="Start Date", visible=False, include_time=False, type="datetime") custom_end_date_picker = gr.DateTime(label="End Date", visible=False, include_time=False, type="datetime") apply_filter_btn = gr.Button("🔍 Apply Filter & Refresh Analytics", variant="primary") def toggle_custom_date_pickers(selection): is_custom = selection == "Custom Range" return gr.update(visible=is_custom), gr.update(visible=is_custom) date_filter_selector.change( fn=toggle_custom_date_pickers, inputs=[date_filter_selector], outputs=[custom_start_date_picker, custom_end_date_picker] ) # --- Define plot configurations --- # (Order must match the order of figures returned by update_analytics_plots_figures) plot_configs = [ {"label": "Posts Activity Over Time", "id": "posts_activity", "section": "Posts & Engagement Overview"}, {"label": "Post Engagement Types", "id": "engagement_type", "section": "Posts & Engagement Overview"}, {"label": "Mentions Activity Over Time", "id": "mentions_activity", "section": "Mentions Overview"}, {"label": "Mention Sentiment Distribution", "id": "mention_sentiment", "section": "Mentions Overview"}, {"label": "Followers Count Over Time", "id": "followers_count", "section": "Follower Dynamics"}, {"label": "Followers Growth Rate", "id": "followers_growth_rate", "section": "Follower Dynamics"}, {"label": "Followers by Location", "id": "followers_by_location", "section": "Follower Demographics"}, {"label": "Followers by Role (Function)", "id": "followers_by_role", "section": "Follower Demographics"}, {"label": "Followers by Industry", "id": "followers_by_industry", "section": "Follower Demographics"}, {"label": "Followers by Seniority", "id": "followers_by_seniority", "section": "Follower Demographics"}, {"label": "Engagement Rate Over Time", "id": "engagement_rate", "section": "Post Performance Insights"}, {"label": "Reach Over Time (Clicks)", "id": "reach_over_time", "section": "Post Performance Insights"}, {"label": "Impressions Over Time", "id": "impressions_over_time", "section": "Post Performance Insights"}, {"label": "Reactions (Likes) Over Time", "id": "likes_over_time", "section": "Post Performance Insights"}, {"label": "Clicks Over Time", "id": "clicks_over_time", "section": "Detailed Post Engagement Over Time"}, {"label": "Shares Over Time", "id": "shares_over_time", "section": "Detailed Post Engagement Over Time"}, {"label": "Comments Over Time", "id": "comments_over_time", "section": "Detailed Post Engagement Over Time"}, {"label": "Breakdown of Comments by Sentiment", "id": "comments_sentiment", "section": "Detailed Post Engagement Over Time"}, {"label": "Post Frequency", "id": "post_frequency_cs", "section": "Content Strategy Analysis"}, {"label": "Breakdown of Content by Format", "id": "content_format_breakdown_cs", "section": "Content Strategy Analysis"}, {"label": "Breakdown of Content by Topics", "id": "content_topic_breakdown_cs", "section": "Content Strategy Analysis"}, {"label": "Mentions Volume Over Time (Detailed)", "id": "mention_analysis_volume", "section": "Mention Analysis (Detailed)"}, {"label": "Breakdown of Mentions by Sentiment (Detailed)", "id": "mention_analysis_sentiment", "section": "Mention Analysis (Detailed)"} ] assert len(plot_configs) == 23, "Mismatch in number of plot configurations and expected plots." # --- Build Analytics Tab UI using the function from ui_generators --- # This function will create the gr.Markdown for sections and rows for plots. # It needs to be called within this gr.Blocks() context. plot_ui_objects = build_analytics_tab_ui_components(plot_configs) active_insight_plot_id_state = gr.State(None) # Stores the plot_id of the currently open insight panel # --- Bomb Button Click Handler --- def handle_bomb_click(plot_id_clicked, current_active_plot_id, current_token_state): logging.info(f"Bomb clicked for: {plot_id_clicked}. Currently active: {current_active_plot_id}") updates = [] new_active_id = None if plot_id_clicked == current_active_plot_id: new_active_id = None # Toggle off logging.info(f"Closing insights for {plot_id_clicked}") else: new_active_id = plot_id_clicked # Activate new one logging.info(f"Opening insights for {plot_id_clicked}, closing others.") for p_id_iter, ui_obj_dict in plot_ui_objects.items(): is_target_one = (p_id_iter == new_active_id) updates.append(gr.update(visible=is_target_one)) # For insights_col visibility if is_target_one: # TODO: Implement actual insight generation logic here insight_text = f"**Insights for {ui_obj_dict['label']}**\n\n" insight_text += f"Plot ID: `{p_id_iter}`.\n" insight_text += "Detailed analysis would involve examining trends, anomalies, and correlations related to this specific chart.\n" insight_text += "For example, for 'Posts Activity', we might look for days with unusually high or low activity and correlate with external events or content types." updates.append(gr.update(value=insight_text)) else: updates.append(gr.update(value=f"Click 💣 for insights on {ui_obj_dict['label']}...")) # Reset placeholder updates.append(new_active_id) # New value for active_insight_plot_id_state logging.info(f"Returning {len(updates)-1} UI updates. New active ID: {new_active_id}") return updates # --- Connect Bomb Buttons --- bomb_click_dynamic_outputs = [] # The order of items in bomb_click_dynamic_outputs must match the order of iteration # in handle_bomb_click when it creates its `updates` list. # plot_ui_objects is a dictionary, so .keys() gives an arbitrary order if not Python 3.7+ # To be safe, iterate based on plot_configs order for constructing outputs. for config in plot_configs: p_id_key = config["id"] bomb_click_dynamic_outputs.append(plot_ui_objects[p_id_key]["insights_col"]) bomb_click_dynamic_outputs.append(plot_ui_objects[p_id_key]["insights_md"]) bomb_click_dynamic_outputs.append(active_insight_plot_id_state) for config in plot_configs: plot_id = config["id"] components_dict = plot_ui_objects[plot_id] components_dict["bomb"].click( fn=handle_bomb_click, inputs=[gr.State(value=plot_id), active_insight_plot_id_state, token_state], outputs=bomb_click_dynamic_outputs, api_name=f"show_insights_{plot_id}" # Gradio handles None api_name if plot_id is None (though it shouldn't be) ) # --- Function to Refresh All Analytics UI (Plots + Reset Insights) --- def refresh_all_analytics_ui_elements(current_token_state, date_filter_val, custom_start_val, custom_end_val): logging.info("Refreshing all analytics UI elements.") plot_generation_results = update_analytics_plots_figures( current_token_state, date_filter_val, custom_start_val, custom_end_val ) status_message_update = plot_generation_results[0] generated_plot_figures = plot_generation_results[1:] all_updates = [status_message_update] # Plot figure updates - iterate based on plot_configs to ensure order for i, config in enumerate(plot_configs): p_id_key = config["id"] if i < len(generated_plot_figures): all_updates.append(generated_plot_figures[i]) else: logging.error(f"Mismatch: Expected figure for {p_id_key} but not enough figures generated.") all_updates.append(create_placeholder_plot("Figure Error", f"No figure for {p_id_key}")) # Insight column visibility and markdown content reset - iterate based on plot_configs for config in plot_configs: p_id_key = config["id"] ui_obj_dict_val = plot_ui_objects[p_id_key] all_updates.append(gr.update(visible=False)) # Hide insights_col all_updates.append(gr.update(value=f"Click 💣 for insights on {ui_obj_dict_val['label']}...")) # Reset insights_md all_updates.append(None) # Reset active_insight_plot_id_state return all_updates # --- Define outputs for the apply_filter_btn and sync.then() --- apply_filter_and_sync_outputs = [analytics_status_md] # Iterate based on plot_configs to ensure order for config in plot_configs: # Plot components apply_filter_and_sync_outputs.append(plot_ui_objects[config["id"]]["plot"]) for config in plot_configs: # Insight column components apply_filter_and_sync_outputs.append(plot_ui_objects[config["id"]]["insights_col"]) for config in plot_configs: # Insight markdown components apply_filter_and_sync_outputs.append(plot_ui_objects[config["id"]]["insights_md"]) apply_filter_and_sync_outputs.append(active_insight_plot_id_state) # State component # --- Connect Apply Filter Button --- apply_filter_btn.click( fn=refresh_all_analytics_ui_elements, inputs=[token_state, date_filter_selector, custom_start_date_picker, custom_end_date_picker], outputs=apply_filter_and_sync_outputs, show_progress="full" ) with gr.TabItem("3️⃣ Mentions", id="tab_mentions"): refresh_mentions_display_btn = gr.Button("🔄 Refresh Mentions Display (from local data)", variant="secondary") mentions_html = gr.HTML("Mentions data loads from Bubble after sync. Click refresh to view current local data.") mentions_sentiment_dist_plot = gr.Plot(label="Mention Sentiment Distribution") refresh_mentions_display_btn.click( fn=run_mentions_tab_display, inputs=[token_state], outputs=[mentions_html, mentions_sentiment_dist_plot], show_progress="full" ) with gr.TabItem("4️⃣ Follower Stats", id="tab_follower_stats"): refresh_follower_stats_btn = gr.Button("🔄 Refresh Follower Stats Display (from local data)", variant="secondary") follower_stats_html = gr.HTML("Follower statistics load from Bubble after sync. Click refresh to view current local data.") with gr.Row(): fs_plot_monthly_gains = gr.Plot(label="Monthly Follower Gains") with gr.Row(): fs_plot_seniority = gr.Plot(label="Followers by Seniority (Top 10 Organic)") fs_plot_industry = gr.Plot(label="Followers by Industry (Top 10 Organic)") refresh_follower_stats_btn.click( fn=run_follower_stats_tab_display, inputs=[token_state], outputs=[follower_stats_html, fs_plot_monthly_gains, fs_plot_seniority, fs_plot_industry], show_progress="full" ) # --- Define the full sync_click_event chain HERE, now that analytics outputs are known --- sync_event_part1 = sync_data_btn.click( fn=sync_all_linkedin_data_orchestrator, inputs=[token_state], outputs=[sync_status_html_output, token_state], show_progress="full" ) sync_event_part2 = sync_event_part1.then( fn=process_and_store_bubble_token, inputs=[url_user_token_display, org_urn_display, token_state], outputs=[status_box, token_state, sync_data_btn], show_progress=False ) sync_event_part3 = sync_event_part2.then( fn=display_main_dashboard, inputs=[token_state], outputs=[dashboard_display_html], show_progress=False ) sync_event_final = sync_event_part3.then( fn=refresh_all_analytics_ui_elements, inputs=[token_state, date_filter_selector, custom_start_date_picker, custom_end_date_picker], outputs=apply_filter_and_sync_outputs, show_progress="full" ) if __name__ == "__main__": if not os.environ.get(LINKEDIN_CLIENT_ID_ENV_VAR): logging.warning(f"WARNING: '{LINKEDIN_CLIENT_ID_ENV_VAR}' environment variable not set.") if not os.environ.get(BUBBLE_APP_NAME_ENV_VAR) or \ not os.environ.get(BUBBLE_API_KEY_PRIVATE_ENV_VAR) or \ not os.environ.get(BUBBLE_API_ENDPOINT_ENV_VAR): logging.warning("WARNING: Bubble environment variables not fully set.") try: logging.info(f"Matplotlib version: {matplotlib.__version__} found. Backend: {matplotlib.get_backend()}") except ImportError: logging.error("Matplotlib is not installed. Plots will not be generated.") app.launch(server_name="0.0.0.0", server_port=7860, debug=True)