ui.insert_nav_panel

ui.insert_nav_panel(
    id,
    nav_panel,
    target=None,
    position='after',
    select=False,
    session=None,
)

Insert a new nav item into a navigation container.

Parameters

id : str

The id of the relevant navigation container (i.e., navset_*() object).

nav_panel : Union[NavSetArg, str]

The navigation item to insert (typically a nav_panel or nav_menu). A nav_menu isn’t allowed when the target references an nav_menu (or an item within it). A string is only allowed when the target references a nav_menu.

target : Optional[str] = None

The value of an existing shiny.ui.nav_panel, next to which tab will be added. Can also be None; see position.

position : Literal[‘after’, ‘before’] = 'after'

The position of the new nav item relative to the target nav item. If target=None, then "before" means the new nav item should be inserted at the head of the navlist, and "after" is the end.

select : bool = False

Whether the nav item should be selected upon insertion.

session : Optional[Session] = None

A Session instance. If not provided, it is inferred via get_current_session.

See Also

remove_nav_panel update_nav_panel nav_panel

Examples

#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400

## file: app.py
from shiny import App, Inputs, Outputs, Session, reactive, ui

app_ui = ui.page_sidebar(
    ui.sidebar(
        ui.input_action_button("add", "Add 'Dynamic' tab"),
        ui.input_action_button("update_foo", "Add/Remove 'Foo' tab"),
    ),
    ui.navset_tab(
        ui.nav_panel("Hello", "This is the hello tab", value="Hello"),
        ui.nav_panel("Foo", "This is the Foo tab", value="Foo"),
        ui.nav_menu(
            "Static",
            ui.nav_panel("Static 1", "Static 1", value="s1"),
            ui.nav_panel("Static 2", "Static 2", value="s2"),
            value="Menu",
        ),
        id="tabs",
    ),
)


def server(input: Inputs, output: Outputs, session: Session):

    @reactive.effect
    @reactive.event(input.update_foo)
    def _():
        if input.update_foo() % 2 == 0:
            ui.insert_nav_panel(
                "tabs",
                ui.nav_panel("Foo", "Foo is back now", value="Foo"),
                target="Menu",
                position="before",
                select=True,
            )
        else:
            ui.remove_nav_panel("tabs", target="Foo")

    @reactive.effect
    @reactive.event(input.add)
    def _():
        id = "Dynamic-" + str(input.add())
        ui.insert_nav_panel(
            "tabs",
            ui.nav_panel(id, id, value=id),
            target="s2",
            position="before",
        )

        ui.notification_show(f"Added tab to menu: {id}")


app = App(app_ui, server)