Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update theme picker UI to be used as dynamic UI #50

Open
gadenbuie opened this issue Jan 13, 2025 · 0 comments
Open

Update theme picker UI to be used as dynamic UI #50

gadenbuie opened this issue Jan 13, 2025 · 0 comments

Comments

@gadenbuie
Copy link
Contributor

When shinyswatch.theme_picker_ui() is used in dynamic UI, i.e. @render.ui, it's only initialized once. On subsequent UI updates, the select input is not initialized.

from shiny import App, ui, Inputs, Outputs, Session, reactive, render
import shinyswatch

app_ui = ui.page_auto(
    ui.output_ui("main_page"),
    ui.input_action_button("change_nav", "Change Nav"),
    theme=shinyswatch.theme.darkly,
)


def server(input: Inputs, output: Outputs, session: Session):
    shinyswatch.theme_picker_server()
    nav_state = reactive.value(1)

    @reactive.calc
    def nav_items():
        return [
            ui.nav_panel(f"Page{i}", ui.markdown(f"# Page{i}")) for i in range(nav_state.get())
        ]

    @render.ui
    def main_page():
        return ui.navset_pill_list(
            *nav_items(),
            ui.nav_control(shinyswatch.theme_picker_ui())
        )
    
    @reactive.effect
    @reactive.event(input.change_nav)
    def change_state():
        nav_state.set(1 if nav_state.get() == 2 else 2)


app = App(app_ui, server)

Reported by @sebovzeoueb on Discord.

A workaround is to call shinyswatch.theme_picker_server() in the @render.ui call, which is generally going to be okay but does create more observers (reactive effects) than we want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant