From ca06b0060cb5e264f2c31e5de267373195b4dbf8 Mon Sep 17 00:00:00 2001 From: Iisakki Rotko Date: Thu, 14 Mar 2024 14:12:16 +0100 Subject: [PATCH] fix: load theme earlier when using SSG --- .../solara_enterprise/ssg.py | 2 + solara/server/server.py | 5 +- solara/server/templates/solara.html.j2 | 107 ++++++++++-------- 3 files changed, 66 insertions(+), 48 deletions(-) diff --git a/packages/solara-enterprise/solara_enterprise/ssg.py b/packages/solara-enterprise/solara_enterprise/ssg.py index 89f54c7df..da6071f14 100644 --- a/packages/solara-enterprise/solara_enterprise/ssg.py +++ b/packages/solara-enterprise/solara_enterprise/ssg.py @@ -205,6 +205,8 @@ def _ssg_data(html: str) -> Optional[SSGData]: rendered_styles = soup.find_all("style") for style in rendered_styles: style_html = str(style) + if 'class="solara-template-css"' in style_html: + continue # in case we want to skip the mathjax css # if "MJXZERO" in style_html: # continue diff --git a/solara/server/server.py b/solara/server/server.py index e5706178b..021be9dc2 100644 --- a/solara/server/server.py +++ b/solara/server/server.py @@ -13,7 +13,6 @@ import ipywidgets import jinja2 import requests - import solara import solara.routing import solara.settings @@ -302,9 +301,9 @@ def include_css(path: str) -> Markup: code = content_utf8 elif embed: content_utf8 = content.decode("utf-8") - code = f"" + code = f'' else: - code = f'' + code = f'' return Markup(code) def include_js(path: str, module=False) -> Markup: diff --git a/solara/server/templates/solara.html.j2 b/solara/server/templates/solara.html.j2 index 329800c7c..0e91abff1 100644 --- a/solara/server/templates/solara.html.j2 +++ b/solara/server/templates/solara.html.j2 @@ -11,13 +11,11 @@ {% block header %} - {{ pre_rendered_css | safe }} - {% if vue3 == True %} - + {% else %} - + {% endif %} @@ -27,7 +25,7 @@ {{ resources.include_css("/static/highlight.css") }} {{ resources.include_css("/static/highlight-dark.css") }} {{ resources.include_css("/static/assets/style.css") }} - {{ resources.include_css("/static/assets/custom.css") }} @@ -38,9 +36,22 @@ "kernelId": "1234" } - + {% endblock header %} {% raw -%} @@ -175,6 +186,46 @@ {# next div is used in ssg code to see if vue took over rendering #} + {% block after_pre_rendered_html %}{% endblock %} {% if vue3 == True %} @@ -191,10 +242,6 @@ {% endif %} {% endif %} - {{ resources.include_js("/static/assets/custom.js") }} {{ resources.include_js("/static/assets/theme.js") }} @@ -208,27 +255,6 @@ solara.production = {{ production | tojson | safe }}; const themeVariants = ['light', 'dark', 'auto'] solara.preRendered = {{ pre_rendered_html | safe | length | tojson }} > 0 - var theme = {{ theme | tojson | safe }} - function getThemeVariant() { - if (localStorage.getItem(':solara:theme.variant')) { - return JSON.parse(localStorage.getItem(':solara:theme.variant')) - } - return theme.variant; - } - if (localStorage.getItem(':solara:theme.variant')) { - theme.variant = JSON.parse(localStorage.getItem(':solara:theme.variant')) - } - function prefersDarkScheme() { - return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches - } - function inDarkMode() { - if (getThemeVariant() == 'auto') { - return prefersDarkScheme(); - } - else { - return getThemeVariant() == 'dark'; - } - }