A dark mode switch input toggles the app between dark and light modes.
To add a dark mode switch to your app:
Add ui.input_dark_mode() to the UI of your app to create a dark mode switch. Where you call this function will determine where the dark mode switch will appear within the app’s layout.
Optionally, specify the id parameter of ui.input_dark_mode() to define the identifier of the switch. When id is specified, you can use it to access the current color mode.
By default, the user’s system settings for the preferred color scheme will be used for the initial mode of the app. To force the initial mode, set the mode parameter to "light" or "dark".
The value of an input component is accessible as a reactive value within the server() function. To access the value of a dark mode switch:
Use input.<dark_mode_switch_id>() (e.g., input.mode()) to access the value of the dark mode switch. The server value of a dark mode switch is a string: either "light" or "dark".
Variations
Dark mode switch in a navbar
To add a dark mode switch to a navbar, use ui.page_navbar() to create a page with a navbar, and ui.nav_spacer() to push the dark mode switch to the right. Place the ui.input_dark_mode() element within a ui.nav_control() element, to add the control to the navbar without creating a corresponding panel.
from shiny.express import uiui.page_opts(title="Dark mode switch in navbar", fillable=True, id="page")ui.nav_spacer() with ui.nav_control(): ui.input_dark_mode()
from shiny import App, uiapp_ui = ui.page_navbar( ui.nav_spacer(), ui.nav_control(ui.input_dark_mode()), title="Dark mode switch in navbar")def server(input, output, session):passapp = App(app_ui, server)