How to dynamically insert new tabs with the press of an input task button?

  Kiến thức lập trình

I am developing a Shiny app in Python and I am currently struggling to find a way to dynamically generate new tabs with the press of either an input task button or with a tab called “New”. I’ve researched into using ui.insert_ui and achieved some level of the intended effect with dynamically adding panels to my ui.navset_tab but when clicking on the new tab, the tab freezes and I can’t switch to the new tab.

Is there a more optimal way to create dynamic tabs and also remove them?

My code is outlined below:

from shiny import App, Inputs, Outputs, Session, reactive, render, ui
from shinywidgets import output_widget, render_widget

app_ui = ui.page_fluid(
    ui.navset_tab(
        ui.nav_panel("Home",
              ui.card(
                          ui.card_header("Overview"),
                          ui.p("This is the landing page"),
                          ui.input_task_button(id = "create_tab",
                                               label = "Create New Tab",
                                               width = "400px",
                                               type = "success"),
                                )
                            )
        id = "shiny_tabs"
    )
)

def server(input, output, session):

    # Set reactive values
    tabs_created = reactive.value(1)

     # Generate tabs
     @reactive.Effect
     @reactive.event(input.create_tab)
     def _():
          tab_title = f"View {tabs_created.get()}"
          ui.insert_ui(
              ui.navset_tab(
                  ui.nav_panel(tab_title,
                               ui.modal("This will include the accordion content")
                               )
                  ),
                  selector = "#shiny_tabs",
                  where = "beforeEnd"
          )
          
          tabs_created.set(tabs_created.get() + 1)
          
app = App(app_ui, server)

New contributor

Devdan Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT