|
| 1 | +from dash import Input, Output, ctx |
| 2 | + |
| 3 | +PERSONA_BUTTONS = [ |
| 4 | + "persona-btn-default", |
| 5 | + "persona-btn-funder", |
| 6 | + "persona-btn-researcher", |
| 7 | + "persona-btn-developer", |
| 8 | + "persona-btn-internal", |
| 9 | +] |
| 10 | + |
| 11 | +BUTTON_TO_PERSONA = { |
| 12 | + "persona-btn-default": "default", |
| 13 | + "persona-btn-funder": "funder", |
| 14 | + "persona-btn-researcher": "researcher", |
| 15 | + "persona-btn-developer": "developer", |
| 16 | + "persona-btn-internal": "internal", |
| 17 | +} |
| 18 | + |
| 19 | +# --------------------------------------------------------------------------- |
| 20 | +# Master registry of every element the persona system controls. |
| 21 | +# |
| 22 | +# To add a new graph/KPI: |
| 23 | +# 1. Add its layout ID to ALL_SECTION_IDS (html.Div) or ALL_COL_IDS (dbc.Col). |
| 24 | +# 2. Add that ID to whichever persona(s) should show it in PERSONA_SHOW below. |
| 25 | +# Everything not listed for a persona is hidden automatically. |
| 26 | +# --------------------------------------------------------------------------- |
| 27 | + |
| 28 | +# html.Div sections — shown with display:block |
| 29 | +ALL_SECTION_IDS = [ |
| 30 | + "servicemap", |
| 31 | + "metrics", |
| 32 | + "epmc", |
| 33 | + "github", |
| 34 | + "pypi", |
| 35 | + "tables", |
| 36 | + # funder-specific chart sections |
| 37 | + "funder-charts", |
| 38 | +] |
| 39 | + |
| 40 | +# dbc.Col KPI cards — shown with {} (let Bootstrap flex handle sizing) |
| 41 | +ALL_COL_IDS = [ |
| 42 | + "funder-kpi-yoy", |
| 43 | + "funder-kpi-avg-citations", |
| 44 | + "funder-kpi-funding-bodies", |
| 45 | +] |
| 46 | + |
| 47 | +ALL_CONTROLLED_IDS = ALL_SECTION_IDS + ALL_COL_IDS |
| 48 | + |
| 49 | +# --------------------------------------------------------------------------- |
| 50 | +# Per-persona whitelist — only list what IS shown. |
| 51 | +# Anything omitted is hidden automatically. |
| 52 | +# --------------------------------------------------------------------------- |
| 53 | +PERSONA_SHOW = { |
| 54 | + "default": { |
| 55 | + "sections": ["servicemap", "metrics", "epmc", "github", "pypi", "tables"], |
| 56 | + "cols": [], |
| 57 | + }, |
| 58 | + "funder": { |
| 59 | + "sections": ["metrics", "epmc", "funder-charts"], |
| 60 | + "cols": ["funder-kpi-yoy", "funder-kpi-avg-citations", "funder-kpi-funding-bodies"], |
| 61 | + }, |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +def register_persona_callbacks(app): |
| 66 | + |
| 67 | + # ------------------------------------------------------------------ |
| 68 | + # 1. Update the active-persona store when any button is clicked |
| 69 | + # ------------------------------------------------------------------ |
| 70 | + @app.callback( |
| 71 | + Output("active-persona", "data"), |
| 72 | + [Input(btn_id, "n_clicks") for btn_id in PERSONA_BUTTONS], |
| 73 | + prevent_initial_call=True, |
| 74 | + ) |
| 75 | + def update_active_persona(*_): |
| 76 | + return BUTTON_TO_PERSONA.get(ctx.triggered_id, "default") |
| 77 | + |
| 78 | + # ------------------------------------------------------------------ |
| 79 | + # 2. Highlight the active button; reset all others |
| 80 | + # ------------------------------------------------------------------ |
| 81 | + @app.callback( |
| 82 | + [Output(btn_id, "className") for btn_id in PERSONA_BUTTONS] |
| 83 | + + [Output(btn_id, "outline") for btn_id in PERSONA_BUTTONS], |
| 84 | + Input("active-persona", "data"), |
| 85 | + ) |
| 86 | + def update_button_styles(active_persona): |
| 87 | + active = active_persona or "default" |
| 88 | + classes = [ |
| 89 | + "persona-btn active-persona" if BUTTON_TO_PERSONA[b] == active else "persona-btn" |
| 90 | + for b in PERSONA_BUTTONS |
| 91 | + ] |
| 92 | + outlines = [BUTTON_TO_PERSONA[b] != active for b in PERSONA_BUTTONS] |
| 93 | + return classes + outlines |
| 94 | + |
| 95 | + # ------------------------------------------------------------------ |
| 96 | + # 3. Show / hide sections based on active persona |
| 97 | + # ------------------------------------------------------------------ |
| 98 | + @app.callback( |
| 99 | + [Output(eid, "style") for eid in ALL_CONTROLLED_IDS], |
| 100 | + Input("active-persona", "data"), |
| 101 | + ) |
| 102 | + def toggle_persona_sections(active_persona): |
| 103 | + persona = active_persona or "default" |
| 104 | + config = PERSONA_SHOW.get(persona, PERSONA_SHOW["default"]) |
| 105 | + shown_sections = set(config["sections"]) |
| 106 | + shown_cols = set(config["cols"]) |
| 107 | + return [ |
| 108 | + {"display": "block"} if eid in shown_sections |
| 109 | + else {} if eid in shown_cols |
| 110 | + else {"display": "none"} |
| 111 | + for eid in ALL_CONTROLLED_IDS |
| 112 | + ] |
0 commit comments