From 82e45dcc991a86f6889bc6bbf83143a46c6b6323 Mon Sep 17 00:00:00 2001 From: Iisakki Rotko Date: Fri, 23 Feb 2024 15:42:52 +0100 Subject: [PATCH] incorporate suggestions from code review Co-authored-by: Maarten Breddels --- solara/__main__.py | 21 ++++++++++++------ solara/lab/components/theming.py | 10 ++++----- solara/lab/components/theming.vue | 2 +- solara/server/server.py | 4 +++- solara/server/settings.py | 1 - solara/server/templates/solara.html.j2 | 6 ++---- solara/tasks.py | 30 +++++++++++++++++--------- solara/website/pages/api/theming.py | 13 ++++++----- 8 files changed, 54 insertions(+), 33 deletions(-) diff --git a/solara/__main__.py b/solara/__main__.py index cea8074f3..172f6d47e 100644 --- a/solara/__main__.py +++ b/solara/__main__.py @@ -11,12 +11,13 @@ import rich import rich_click as click -import solara import uvicorn from rich import print as rprint -from solara.server import settings from uvicorn.main import LEVEL_CHOICES, LOOP_CHOICES +import solara +from solara.server import settings + from .server import telemetry try: @@ -216,11 +217,17 @@ def cli(): default=settings.theme.variant.name, help=f"Use light or dark variant, or auto detect (auto). [default: {settings.theme.variant.name}", ) +@click.option( + "--dark", + type=bool, + default=settings.theme.variant == settings.ThemeVariant.dark, + help="Use dark theme. Shorthand for --theme-variant=dark", +) @click.option( "--theme-variant-user-selectable/--no-theme-variant-user-selectable", type=bool, - default=settings.theme.variant_user_selectable, - help=f"Can the user select the theme variant from the UI. [default: {settings.theme.variant_user_selectable}", + hidden=True, + help="Deprecated.", ) @click.option("--pdb/--no-pdb", "use_pdb", default=False, help="Enter debugger on error") @click.argument("app") @@ -273,6 +280,7 @@ def run( use_pdb: bool, theme_loader: str, theme_variant: settings.ThemeVariant, + dark: bool, theme_variant_user_selectable: bool, ssg: bool, search: bool, @@ -375,12 +383,13 @@ def open_browser(): kwargs["loop"] = loop settings.main.use_pdb = use_pdb settings.theme.loader = theme_loader + if dark: + theme_variant = settings.ThemeVariant.dark settings.theme.variant = theme_variant - settings.theme.variant_user_selectable = theme_variant_user_selectable settings.main.tracer = tracer settings.main.timing = timing items = ( - "theme_variant_user_selectable theme_variant theme_loader use_pdb server open_browser open url failed dev tracer" + "theme_variant_user_selectable dark theme_variant theme_loader use_pdb server open_browser open url failed dev tracer" " timing ssg search check_version production".split() ) for item in items: diff --git a/solara/lab/components/theming.py b/solara/lab/components/theming.py index cf509819c..22c1208a0 100644 --- a/solara/lab/components/theming.py +++ b/solara/lab/components/theming.py @@ -4,12 +4,11 @@ from ipyvuetify.Themes import Theme import solara -import solara.server.settings as settings from solara.components.component_vue import component_vue from solara.tasks import Proxy theme = Proxy(Theme) -ipyvuetify.Themes.theme = theme +ipyvuetify.Themes.theme = theme # type: ignore @component_vue("theming.vue") @@ -30,6 +29,7 @@ def ThemeToggle( on_icon: str = "mdi-weather-night", off_icon: str = "mdi-weather-sunny", auto_icon: str = "mdi-brightness-auto", + enable_auto: bool = True, ): """ Insert a toggle switch for user to switch between light and dark themes. @@ -45,8 +45,8 @@ def Page(): ## Arguments - `on_icon`: The icon to display when the dark theme is enabled. - `off_icon`: The icon to display when the dark theme is disabled. - - `auto_icon`: The icon to display when the theme is set to auto - (**note**: auto mode is only available if the server settings `theme.variant_user_selectable` is enabled). + - `auto_icon`: The icon to display when the theme is set to auto. Only visible if `enable_auto` is `True`. + - `enable_auto`: Whether to enable the auto detection of dark mode. """ def sync_themes(selected_theme: str): @@ -55,7 +55,7 @@ def sync_themes(selected_theme: str): return _ThemeToggle( theme_dark=theme.dark, event_sync_themes=sync_themes, - enable_auto=settings.theme.variant_user_selectable, + enable_auto=enable_auto, on_icon="mdi-weather-night", off_icon="mdi-weather-sunny", auto_icon="mdi-brightness-auto", diff --git a/solara/lab/components/theming.vue b/solara/lab/components/theming.vue index c29abdf31..c33f79395 100644 --- a/solara/lab/components/theming.vue +++ b/solara/lab/components/theming.vue @@ -12,7 +12,7 @@