posit-dev / py-shinyswatch

Bootswatch themes for py-shiny

Home Page:https://posit-dev.github.io/py-shinyswatch/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

shinyswatch.theme.xxx() does not work

webscale opened this issue · comments

commented

This server side method does nothing. Calling any of the shinyswatch.theme.cerulean() methods does nothing.

Note : while this code is in a module , this does not work even when placed in core app.py


@module.server
def default_settings_tab_server(input: Inputs, output: Outputs, session: Session ,master : master.shiny_master):
    
    shinyswatch.theme_picker_server()
       
    @reactive.effect
    @reactive.event(input.app_theme)
    def _():
        print(f"Theme Changed = {input.app_theme()}")
        new_theme = input.app_theme()
        shinyswatch.get_theme(new_theme),
        if(new_theme == 'cerulean'):
             shinyswatch.theme.cerulean()
        elif(new_theme == 'cosmo'):
             shinyswatch.theme.cosmo()
        elif(new_theme == 'litera'):
             shinyswatch.theme.litera()

Client Side Code


def default_settings_tab_onstart(master : master.shiny_master):
    pass
@module.ui
def default_settings_tab_ui( master : master.shiny_master) -> str :
    return ui.card(
        ui.card_header("UI Settings"),
    #    shinyswatch.theme_picker_ui(default = "litera"),
        shinyswatch.theme.litera(),

        ui.input_radio_buttons("app_theme", "Theme", 
                               choices = ['cerulean', 'cosmo',  'litera' ],  
                               inline=True),
        ui.input_action_button("save_settings", "Save Settings"),
        
    )

Proof Points : print(f"Theme Changed = {input.app_theme()}") is printed in console but there is no change in UI.

Pip Freeze :
shiny==0.10.2
shinyswatch==0.6.1
shinywidgets==0.3.2

OS - windows

Browser : Chrome

Screenshot 2024-06-01 134821

So a couple things are going on... but it still leads to not a perfect solution.


The shinyswatch theme needs to be returned in the UI somehow. This can either be given in the initial UI definition in default_settings_tab_ui or it can be given in dynamic UI. (I'd strongly recommend the initial UI definition for a clean CSS experience). You've already done this with your litera theme. ✅

However, I'm guessing from the UI Settings that you'd like for the theme to update on the page when the Theme Setting is saved. If this is the case... the end goal should be to get the UI page to reload with the updated theme value. Unfortunately the package methods don't make this easier.

Really... The theme picker is there more for the app author to pick for the app for everyone, not really the app user to pick for themselves.


So my recommendation is to use the theme picker to decide which theme you'd like for your uses to have. Then use that single theme object within your app.

Example app showing dynamic UI won't work as a solution to get the latest theme value: shinylive editor