I’m encountering a peculiar issue when developing my Python shiny app. My app currently has the functionality to dynamically generate new tabs with the press of a navset tab called “+”. However, after pressing “+”, the state (including input and output values) of the previous tabs reset back to empty. Is there a way to preserve the state of any previously existing tabs?
My code is outlined below:
from shiny import App, Inputs, Outputs, Session, module, reactive, render, ui
# Create a Module UI
@module.ui
def textbox_ui(panelNum):
ui.nav_panel(
f"Tab {panelNum}",
ui.input_text_area(id=f"test_text",
label = "Enter some text"),
ui.output_ui(f"display_text_{panelNum}"),
value = f"Tab_{panelNum}"
)
@module.server
def textbox_server(input, output, session, panelNum):
@output(id=f"display_text_{panelNum}")
@render.text
def return_text():
return input[f"test_text"]()
# Set up app UI
app_ui = ui.page_fluid(
ui.tags.head(
ui.tags.style(
"""
body {
height: 100vh;
overflow-y: auto !important;
}
"""
)
)
ui.h2('Test App', align='center', fillable=True),
ui.output_ui("tab_UI")
title = "Test App"
)
# Set up server
def server(input, output, session):
navs = reactive.value(0)
@reactive.effect
@reactive.event(input.shiny_tabs)
def add_tabs():
if input.shiny_tabs() == "+":
navs.set(navs.get() + 1)
@output
@render.ui
def tab_UI():
[textbox_server(str(x), panelNum=x+1) for x in range(navs.get())]
ui.update_navs("shiny_tabs", selected = f"Tab_{navs.get()}")
return ui.navset_tab(
ui.nav_panel("Home",
ui.card(
ui.card_header("Overview"),
ui.p("An example of the outputs clearing")
)
value = "panel0"
),
*[textbox_ui(str(x), panelNum=x+1) for x in range(navs.get())],
ui.nav_panel("+"),
id = "shiny_tabs"
)
app = App(app_ui, server)