最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to switch to a certain navigation panel when a button is clicked? - Stack Overflow

programmeradmin4浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

It is possible:

  • Locate the nav_panel()s inside a navigation container. You can choose one of Shiny's ui.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 the nav_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 the selected 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")
发布评论

评论列表(0)

  1. 暂无评论