posit-dev / py-shiny

Shiny for Python
https://shiny.posit.co/py/
MIT License
1.22k stars 69 forks source link

[Express] `page_auto()` doesn't let you give `page_navbar()` an `id` #1036

Open gadenbuie opened 8 months ago

gadenbuie commented 8 months ago

I wasn't able to find a user-friendly way of passing id from ui.page_opts() to page_navbar(). This is useful when server side logic needs to know which nav_panel() is currently active.

Here's the solution I came up with in Express:

from shiny import reactive
from shiny.express import input, ui
from shiny.ui import page_navbar

ui.page_opts(
    title="App with navbar",
    page_fn=lambda *args, **kwargs: page_navbar(*args, id="page", **kwargs),
)

with ui.nav_panel("A"):
    "Page A content"

with ui.nav_panel("B"):
    "Page B content"

with ui.nav_panel("C"):
    "Page C content"

@reactive.effect
def _():
    print(f"Page: {input.page()}")
gadenbuie commented 8 months ago

Similarly, shiny.ui.page_navbar() takes position and a few other useful options. Notably, both id and position are highlighted in https://shiny.posit.co/py/layouts/navbars.html and are hard to reach in Express.