Skip to content

Commit 01c3aad

Browse files
committed
Updating nav implementation and examples
1 parent 1902796 commit 01c3aad

File tree

6 files changed

+160
-61
lines changed

6 files changed

+160
-61
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from shiny import reactive
2+
from shiny.express import input, ui
3+
4+
with ui.layout_sidebar():
5+
with ui.sidebar():
6+
ui.input_action_button("add", "Add 'Dynamic' tab")
7+
ui.input_action_button("removeFoo", "Remove 'Foo' tabs")
8+
ui.input_action_button("addFoo", "Add New 'Foo' tab")
9+
10+
@reactive.effect()
11+
@reactive.event(input.removeFoo)
12+
def _():
13+
ui.nav_remove("tabs", target="Foo")
14+
15+
@reactive.effect()
16+
@reactive.event(input.addFoo)
17+
def _():
18+
n = str(input.addFoo())
19+
ui.nav_insert(
20+
"tabs",
21+
ui.nav_panel(
22+
"Foo-" + n, "This is the new Foo-" + n + " tab", value="Foo"
23+
),
24+
target="Menu",
25+
position="before",
26+
select=True,
27+
)
28+
29+
with ui.navset_tab(id="tabs"):
30+
with ui.nav_panel("Hello"):
31+
"This is the hello tab"
32+
with ui.nav_panel("Foo", value="Foo"):
33+
"This is the Foo tab"
34+
with ui.nav_menu("Static", value="Menu"):
35+
with ui.nav_panel("Static 1", value="s1"):
36+
"Static 1"
37+
with ui.nav_panel("Static 2", value="s2"):
38+
"Static 2",
39+
40+
@reactive.effect()
41+
@reactive.event(input.add)
42+
def _():
43+
id = "Dynamic-" + str(input.add())
44+
ui.nav_insert(
45+
"tabs",
46+
ui.nav_panel(id, id),
47+
target="s2",
48+
position="before",
49+
)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from shiny import App, Inputs, Outputs, Session, reactive, ui
2+
3+
app_ui = ui.page_sidebar(
4+
ui.sidebar(
5+
"Home",
6+
ui.input_action_button("hideTab", "Hide 'Foo' tab"),
7+
ui.input_action_button("showTab", "Show 'Foo' tab"),
8+
ui.input_action_button("hideMenu", "Hide 'More' nav_menu"),
9+
ui.input_action_button("showMenu", "Show 'More' nav_menu"),
10+
),
11+
ui.navset_tab(
12+
ui.nav_panel("Foo", "This is the foo tab"),
13+
ui.nav_panel("Bar", "This is the bar tab"),
14+
ui.nav_menu(
15+
"More",
16+
ui.nav_panel("Table", "Table page"),
17+
ui.nav_panel("About", "About page"),
18+
"------",
19+
"Even more!",
20+
ui.nav_panel("Email", "Email page"),
21+
value="More",
22+
),
23+
id="tabs",
24+
),
25+
title="Navbar page",
26+
id="sidebar",
27+
)
28+
29+
30+
def server(input: Inputs, output: Outputs, session: Session):
31+
@reactive.effect()
32+
@reactive.event(input.hideTab)
33+
def _():
34+
ui.nav_hide("tabs", target="Foo")
35+
36+
@reactive.effect()
37+
@reactive.event(input.showTab)
38+
def _():
39+
ui.nav_show("tabs", target="Foo")
40+
41+
@reactive.effect()
42+
@reactive.event(input.hideMenu)
43+
def _():
44+
ui.nav_hide("tabs", target="More")
45+
46+
@reactive.effect()
47+
@reactive.event(input.showMenu)
48+
def _():
49+
ui.nav_show("tabs", target="More")
50+
51+
52+
app = App(app_ui, server)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from shiny import reactive
2+
from shiny.express import input, ui, expressify, render
3+
4+
with ui.layout_sidebar():
5+
with ui.sidebar():
6+
ui.input_action_button("add", "Add 'Dynamic' tab")
7+
ui.input_action_button("removeFoo", "Remove 'Foo' tabs")
8+
ui.input_action_button("addFoo", "Add New 'Foo' tab")
9+
10+
with ui.navset_tab(id="tabs"):
11+
with ui.nav_panel("Hello"):
12+
"This is the hello tab"
13+
with ui.nav_panel("Foo", value="Foo"):
14+
"This is the Foo tab"
15+
with ui.nav_menu("Static", value="Menu"):
16+
with ui.nav_panel("Static 1", value="s1"):
17+
"Static 1"
18+
with ui.nav_panel("Static 2", value="s2"):
19+
"Static 2"
20+
21+
@expressify()
22+
@reactive.event(input.add)
23+
def _():
24+
id = "Dynamic-" + str(input.add())
25+
ui.nav_insert(
26+
"tabs",
27+
ui.nav_panel(id, id),
28+
target="s2",
29+
position="before",
30+
)
31+
32+
@reactive.effect()
33+
@reactive.event(input.removeFoo)
34+
def _():
35+
ui.nav_remove("tabs", target="Foo")
36+
37+
@reactive.effect()
38+
@reactive.event(input.addFoo)
39+
def _():
40+
n = str(input.addFoo())
41+
ui.nav_insert(
42+
"tabs",
43+
ui.nav_panel("Foo-" + n, "This is the new Foo-" + n + " tab", value="Foo"),
44+
target="Menu",
45+
position="before",
46+
select=True,
47+
)

shiny/examples/nav_show/app.py

Lines changed: 0 additions & 48 deletions
This file was deleted.

shiny/ui/_navs_dynamic.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ def nav_insert(
6161
~nav_remove
6262
~nav_show
6363
~nav_hide
64-
~shiny.ui.nav
64+
~shiny.ui.nav_panel
6565
"""
6666

6767
session = require_active_session(session)
6868

6969
# N.B. this is only sensible if the target is a menu, but we don't know that,
7070
# which could cause confusion of we decide to support top-level strings at some
7171
# in the future.
72-
# print("IS INSTANCE: ", isinstance(nav_panel, str))
73-
# if isinstance(nav_panel, str):
74-
# print("isnav")
75-
nav = menu_string_as_nav(nav_panel)
76-
print(nav)
72+
if isinstance(nav_panel, str):
73+
nav = menu_string_as_nav(nav_panel)
74+
else:
75+
nav = nav_panel
76+
7777
# N.B. shiny.js' is smart enough to know how to add active classes and href/id attrs
7878
li_tag, div_tag = nav.resolve(
7979
selected=None, context=dict(tabsetid="tsid", index="id")
@@ -89,7 +89,7 @@ def nav_insert(
8989
"select": select,
9090
}
9191

92-
session._send_message_sync({"custom": {"shiny-insert-tab": msg}})
92+
session._send_message_sync({"shiny-insert-tab": msg})
9393

9494

9595
@add_example()
@@ -102,7 +102,7 @@ def nav_remove(id: str, target: str, session: Optional[Session] = None) -> None:
102102
id
103103
The ``id`` of the relevant navigation container (i.e., ``navset_*()`` object).
104104
target
105-
The ``value`` of an existing :func:`shiny.ui.nav` item to remove.
105+
The ``value`` of an existing :func:`shiny.ui.nav_panel` item to remove.
106106
session
107107
A :class:`~shiny.Session` instance. If not provided, it is inferred via
108108
:func:`~shiny.session.get_current_session`.
@@ -112,17 +112,16 @@ def nav_remove(id: str, target: str, session: Optional[Session] = None) -> None:
112112
~nav_insert
113113
~nav_show
114114
~nav_hide
115-
~shiny.ui.nav
115+
~shiny.ui.nav_panel
116116
"""
117117

118118
session = require_active_session(session)
119-
120119
msg = {
121120
"inputId": resolve_id(id),
122121
"target": target,
123122
}
124123

125-
session._send_message_sync({"custom": {"shiny-remove-tab": msg}})
124+
session._send_message_sync({"shiny-remove-tab": msg})
126125

127126

128127
@add_example()
@@ -169,7 +168,7 @@ def nav_show(
169168
"type": "show",
170169
}
171170

172-
session._send_message_sync({"custom": {"shiny-change-tab-visibility": msg}})
171+
session._send_message_sync({"shiny-change-tab-visibility": msg})
173172

174173

175174
@add_example()
@@ -203,4 +202,4 @@ def nav_hide(id: str, target: str, session: Optional[Session] = None) -> None:
203202
"type": "hide",
204203
}
205204

206-
session._send_message_sync({"custom": {"shiny-change-tab-visibility": msg}})
205+
session._send_message_sync({"shiny-change-tab-visibility": msg})

0 commit comments

Comments
 (0)