|
| 1 | +from typing import Any, Dict |
| 2 | + |
| 3 | +from shiny import Inputs, reactive |
| 4 | +from shiny.express import app_opts, expressify, input, module, session, ui |
| 5 | + |
| 6 | +app_opts(bookmark_store="url") |
| 7 | + |
| 8 | + |
| 9 | +navset_configs: Dict[str, Dict[str, Dict[str, Any]]] = { |
| 10 | + "navset_hidden": { |
| 11 | + "default": {}, |
| 12 | + }, |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +@expressify |
| 17 | +def create_navset(navset_name: str, input: Inputs) -> None: |
| 18 | + navset_function = getattr(ui, navset_name) |
| 19 | + |
| 20 | + @expressify |
| 21 | + def make_navset(navset_variant: str, **navset_kwargs): |
| 22 | + letters = ["a", "b", "c"] |
| 23 | + navset_fn_id = f"{navset_name}_{navset_variant}" |
| 24 | + ui.h3(f"Variant: {navset_variant}") |
| 25 | + with navset_function(id=navset_fn_id, **navset_kwargs): |
| 26 | + for suffix in letters: |
| 27 | + id = f"{navset_fn_id}_{suffix}" |
| 28 | + with ui.nav_panel(id, value=id): |
| 29 | + ui.markdown(f"{id} content") |
| 30 | + |
| 31 | + btn_id = f"{navset_fn_id}_button" |
| 32 | + ui.input_action_button( |
| 33 | + id=btn_id, |
| 34 | + label=f"Cycle content for {navset_fn_id}", |
| 35 | + ) |
| 36 | + |
| 37 | + @reactive.effect |
| 38 | + @reactive.event(input[btn_id], ignore_init=True) |
| 39 | + def _(): |
| 40 | + # Cycle the content by changing the nav panel value |
| 41 | + current_idx = input[btn_id]() |
| 42 | + next_letter = letters[current_idx % len(letters)] |
| 43 | + next_id = f"{navset_fn_id}_{next_letter}" |
| 44 | + |
| 45 | + ui.update_navs(navset_fn_id, selected=next_id) |
| 46 | + |
| 47 | + for navset_variant, params in navset_configs[navset_name].items(): |
| 48 | + make_navset(navset_variant, **params.copy()) |
| 49 | + |
| 50 | + |
| 51 | +with ui.card(): |
| 52 | + ui.card_header("Hidden Bookmark Navset Demo") |
| 53 | + |
| 54 | + # Non-modular section |
| 55 | + ui.h3("Non-Module Section") |
| 56 | + |
| 57 | + with ui.navset_tab(id="navsets_collection"): |
| 58 | + for navset_name in navset_configs.keys(): |
| 59 | + with ui.nav_panel(navset_name): |
| 60 | + create_navset(navset_name, input=input) |
| 61 | + |
| 62 | + @module |
| 63 | + def navset_module(input, output, session): |
| 64 | + ui.h3("Navset Module") |
| 65 | + |
| 66 | + with ui.navset_tab(id="navsets_collection"): |
| 67 | + for navset_name in navset_configs.keys(): |
| 68 | + with ui.nav_panel(navset_name): |
| 69 | + create_navset(navset_name, input=input) |
| 70 | + |
| 71 | + navset_module("first") |
| 72 | + |
| 73 | + ui.input_bookmark_button() |
| 74 | + |
| 75 | + |
| 76 | +@session.bookmark.on_bookmarked |
| 77 | +async def _(url: str): |
| 78 | + await session.bookmark.update_query_string(url) |
0 commit comments