From 1df5c2a44cbaba6dbc6d5ac6eaf8bc61fc0b34d7 Mon Sep 17 00:00:00 2001 From: Iisakki Rotko Date: Tue, 20 Feb 2024 15:01:45 +0100 Subject: [PATCH] refactor: incorporate feedback from code review Co-authored-by: Maarten Breddels --- solara/lab/components/theming.py | 5 ++- solara/lab/components/theming.vue | 53 ++++++++++++++------------ solara/server/app.py | 4 ++ solara/server/server.py | 6 ++- solara/server/templates/solara.html.j2 | 18 +++++---- solara/website/pages/api/theming.py | 7 ++-- 6 files changed, 55 insertions(+), 38 deletions(-) diff --git a/solara/lab/components/theming.py b/solara/lab/components/theming.py index ffb438164..cf509819c 100644 --- a/solara/lab/components/theming.py +++ b/solara/lab/components/theming.py @@ -14,12 +14,13 @@ @component_vue("theming.vue") def _ThemeToggle( - theme_effective: str, + theme_dark: str, event_sync_themes: Callable[[str], None], enable_auto: bool, on_icon: str, off_icon: str, auto_icon: str, + clicks: int = 1, ): pass @@ -52,7 +53,7 @@ def sync_themes(selected_theme: str): theme.dark = selected_theme return _ThemeToggle( - theme_effective=theme.dark, + theme_dark=theme.dark, event_sync_themes=sync_themes, enable_auto=settings.theme.variant_user_selectable, on_icon="mdi-weather-night", diff --git a/solara/lab/components/theming.vue b/solara/lab/components/theming.vue index f3809b839..c29abdf31 100644 --- a/solara/lab/components/theming.vue +++ b/solara/lab/components/theming.vue @@ -4,7 +4,7 @@ @click="countClicks" > + :color="theme_dark ? 'primary' : null"> {{ this.clicks === 1 ? this.on_icon : this.clicks === 2 ? this.off_icon : this.auto_icon }} @@ -12,59 +12,62 @@ diff --git a/solara/server/app.py b/solara/server/app.py index 479a11c36..2dc6e2b18 100644 --- a/solara/server/app.py +++ b/solara/server/app.py @@ -360,7 +360,11 @@ def on_msg(msg): from solara.lab import theme + # While this usually gets set from the frontend, in solara (server) we want to know this directly at the first + # render. Also, using the same trait allows us to write code which works on all widgets platforms, instead + # or using something different when running under solara server theme.dark_effective = dark + container = ipyvuetify.Html(tag="div") context.container = container load_app_widget(None, app, path) diff --git a/solara/server/server.py b/solara/server/server.py index 737b737c6..3f91947c1 100644 --- a/solara/server/server.py +++ b/solara/server/server.py @@ -293,7 +293,11 @@ def include_css(path: str) -> Markup: url = f"{root_path}{path}?v={hash}" # when < 10k we embed, also when we use a url, it can be relative, which can break the url embed = len(content) < 1024 * 10 and b"url" not in content - if embed: + # Include the jupyterlab theme css directly, so we can change it on demand + if path.endswith("theme-dark.css") or path.endswith("theme-light.css"): + content_utf8 = content.decode("utf-8") + code = content_utf8 + elif embed: content_utf8 = content.decode("utf-8") code = f"" else: diff --git a/solara/server/templates/solara.html.j2 b/solara/server/templates/solara.html.j2 index 710c134e9..c858800a9 100644 --- a/solara/server/templates/solara.html.j2 +++ b/solara/server/templates/solara.html.j2 @@ -27,11 +27,9 @@ {{ resources.include_css("/static/highlight.css") }} {{ resources.include_css("/static/highlight-dark.css") }} {{ resources.include_css("/static/assets/style.css") }} - {% if theme.variant == "light" %} - {{ resources.include_css("/static/assets/theme-light.css") }} - {% elif theme.variant == "dark" %} - {{ resources.include_css("/static/assets/theme-dark.css") }} - {% endif %} + {{ resources.include_css("/static/assets/custom.css") }}