Skip to content

Commit 6de5bde

Browse files
committed
Example app with dynamic tabs
1 parent 019d619 commit 6de5bde

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

examples/dynamic-tabs/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Example of inserting, deleting showing, an hiding tabs
2+
3+
<a href='https://connect.posit.cloud/publish?framework=shiny&sourceRepositoryURL=https%3A%2F%2Fgithub.com%2Fposit-dev%2Fpy-shiny&sourceRef=main&sourceRefType=branch&primaryFile=examples%2Fmodel-score%2Fapp.py&pythonVersion=3.11'><img src='https://cdn.connect.posit.cloud/assets/deploy-to-connect-blue.svg' align="right" /></a>

examples/dynamic-tabs/app.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from shiny import App, Inputs, Outputs, Session, reactive, ui
2+
3+
app_ui = ui.page_sidebar(
4+
ui.sidebar(
5+
ui.input_action_button("add", "Add 'Dynamic' tab"),
6+
ui.input_action_button("removeFoo", "Remove 'Foo' tabs"),
7+
ui.input_action_button("addFoo", "Add New 'Foo' tab"),
8+
ui.input_action_button("hideTab", "Hide 'Foo' tab"),
9+
ui.input_action_button("showTab", "Show 'Foo' tab"),
10+
ui.input_action_button("hideMenu", "Hide 'Static' nav_menu"),
11+
ui.input_action_button("showMenu", "Show 'Static' nav_menu"),
12+
),
13+
ui.navset_tab(
14+
ui.nav_panel("Hello", "This is the hello tab"),
15+
ui.nav_panel("Foo", "This is the Foo tab", value="Foo"),
16+
ui.nav_menu(
17+
"Static",
18+
ui.nav_panel("Static 1", "Static 1", value="s1"),
19+
ui.nav_panel("Static 2", "Static 2", value="s2"),
20+
value="Menu",
21+
),
22+
id="tabs",
23+
),
24+
)
25+
26+
27+
def server(input: Inputs, output: Outputs, session: Session):
28+
@reactive.effect()
29+
@reactive.event(input.add)
30+
def _():
31+
id = "Dynamic-" + str(input.add())
32+
ui.nav_insert(
33+
"tabs",
34+
ui.nav_panel(id, id),
35+
target="s2",
36+
position="before",
37+
)
38+
39+
@reactive.effect()
40+
@reactive.event(input.removeFoo)
41+
def _():
42+
ui.nav_remove("tabs", target="Foo")
43+
44+
@reactive.effect()
45+
@reactive.event(input.addFoo)
46+
def _():
47+
n = str(input.addFoo())
48+
ui.nav_insert(
49+
"tabs",
50+
ui.nav_panel("Foo-" + n, "This is the new Foo-" + n + " tab", value="Foo"),
51+
target="Menu",
52+
position="before",
53+
select=True,
54+
)
55+
56+
@reactive.effect()
57+
@reactive.event(input.hideTab)
58+
def _():
59+
ui.nav_hide("tabs", target="Foo")
60+
61+
@reactive.effect()
62+
@reactive.event(input.showTab)
63+
def _():
64+
ui.nav_show("tabs", target="Foo")
65+
66+
@reactive.effect()
67+
@reactive.event(input.hideMenu)
68+
def _():
69+
ui.nav_hide("tabs", target="Menu")
70+
71+
@reactive.effect()
72+
@reactive.event(input.showMenu)
73+
def _():
74+
ui.nav_show("tabs", target="Menu")
75+
76+
77+
app = App(app_ui, server)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
shiny

0 commit comments

Comments
 (0)