I have an app written in Shiny for Python with several nav panels. On the front page, which the user gets sent to first, I have one button for each other nav panel. Is there any way for me to add functionality so that clicking a button sends you to the appropriate nav panel?
I've found ways to do this in R Shiny, but none in Shiny for Python.
Example code:
from shiny import App, Inputs, Outputs, Session, reactive, render, req, ui
from shiny.express import input, ui
with ui.nav_panel('Start'):
ui.input_action_button("move_to_panel_1", "Move to panel 1") # clicking this moves you to panel 1
ui.input_action_button("move_to_panel_2", "Move to panel 2") # to panel 2
with ui.nav_panel('Panel 1'):
with ui.card():
@render.text
def text1():
return 'Text 1'
with ui.nav_panel('Panel 2'):
with ui.card():
@render.text
def text2():
return 'Text 2'
I have an app written in Shiny for Python with several nav panels. On the front page, which the user gets sent to first, I have one button for each other nav panel. Is there any way for me to add functionality so that clicking a button sends you to the appropriate nav panel?
I've found ways to do this in R Shiny, but none in Shiny for Python.
Example code:
from shiny import App, Inputs, Outputs, Session, reactive, render, req, ui
from shiny.express import input, ui
with ui.nav_panel('Start'):
ui.input_action_button("move_to_panel_1", "Move to panel 1") # clicking this moves you to panel 1
ui.input_action_button("move_to_panel_2", "Move to panel 2") # to panel 2
with ui.nav_panel('Panel 1'):
with ui.card():
@render.text
def text1():
return 'Text 1'
with ui.nav_panel('Panel 2'):
with ui.card():
@render.text
def text2():
return 'Text 2'
Share
Improve this question
edited Jan 19 at 7:00
Jan
8,9016 gold badges19 silver badges33 bronze badges
asked Jan 19 at 4:18
RedzRedz
5621 gold badge6 silver badges24 bronze badges
1 Answer
Reset to default 1It is possible:
- Locate the
nav_panel()
s inside a navigation container. You can choose one of Shiny'sui.navset_*
functions, e.g.ui.navset_tab()
:with ui.navset_tab(id="selected_navset_tab"): with ui.nav_panel('Start'): ... with ui.nav_panel(...): ...
- Provide a
value
argument to each of thenav_panel()
which you want to make accessible via buttons:with ui.nav_panel('Panel 1', value="panel1"): ...
- Define an event which is triggered when a button gets clicked, inside it use
ui.update_navs()
and provide theselected
argument with the needed value, e.g.@reactive.effect @reactive.event(input.move_to_panel_1) def _(): ui.update_navs("selected_navset_tab", selected="panel1")
from shiny import render, ui, reactive
from shiny.express import input, ui
with ui.navset_tab(id="selected_navset_tab"):
with ui.nav_panel('Start'):
ui.input_action_button("move_to_panel_1", "Move to panel 1")
ui.input_action_button("move_to_panel_2", "Move to panel 2")
with ui.nav_panel('Panel 1', value="panel1"):
with ui.card():
@render.text
def text1():
return 'Text 1'
with ui.nav_panel('Panel 2', value="panel2"):
with ui.card():
@render.text
def text2():
return 'Text 2'
@reactive.effect
@reactive.event(input.move_to_panel_1)
def _():
ui.update_navs("selected_navset_tab", selected="panel1")
@reactive.effect
@reactive.event(input.move_to_panel_2)
def _():
ui.update_navs("selected_navset_tab", selected="panel2")