Skip to content

Commit

Permalink
fix: load theme earlier when using SSG
Browse files Browse the repository at this point in the history
  • Loading branch information
iisakkirotko authored and maartenbreddels committed Mar 18, 2024
1 parent e3ea88a commit a7ab230
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 48 deletions.
2 changes: 2 additions & 0 deletions packages/solara-enterprise/solara_enterprise/ssg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions solara/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import ipywidgets
import jinja2
import requests

import solara
import solara.routing
import solara.settings
Expand Down Expand Up @@ -302,9 +301,9 @@ def include_css(path: str) -> Markup:
code = content_utf8
elif embed:
content_utf8 = content.decode("utf-8")
code = f"<style>/*\npath={path}\n*/\n{content_utf8}</style>"
code = f'<style class="solara-template-css">/*\npath={path}\n*/\n{content_utf8}</style>'
else:
code = f'<link rel="stylesheet" type="text/css" href="{url}">'
code = f'<link rel="stylesheet" type="text/css" href="{url}" class="solara-template-css">'
return Markup(code)

def include_js(path: str, module=False) -> Markup:
Expand Down
107 changes: 62 additions & 45 deletions solara/server/templates/solara.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@

{% block header %}


{{ pre_rendered_css | safe }}

{% if vue3 == True %}
<link href="{{cdn}}/@widgetti/[email protected]/dist/main{{ipywidget_major_version}}.css" rel="stylesheet"></link>
<link href="{{cdn}}/@widgetti/[email protected]/dist/main{{ipywidget_major_version}}.css" rel="stylesheet" class="solara-template-css"></link>
{% else %}
<link href="{{cdn}}/@widgetti/[email protected]/dist/main{{ipywidget_major_version}}.css" rel="stylesheet"></link>
<link href="{{cdn}}/@widgetti/[email protected]/dist/main{{ipywidget_major_version}}.css" rel="stylesheet" class="solara-template-css"></link>
{% endif %}


Expand All @@ -27,7 +25,7 @@
{{ resources.include_css("/static/highlight.css") }}
{{ resources.include_css("/static/highlight-dark.css") }}
{{ resources.include_css("/static/assets/style.css") }}
<style id="jupyter-theme-css">
<style id="jupyter-theme-css" class="solara-template-css">
{{ resources.include_css("/static/assets/theme-"~theme.variant~".css") }}
</style>
{{ resources.include_css("/static/assets/custom.css") }}
Expand All @@ -38,9 +36,22 @@
"kernelId": "1234"
}
</script>
<style>
<style class="solara-template-css">
{% include "loader-"~theme.loader~".css" %}
</style>
<style id="pre-render-theme">
.theme--light .v-sheet {
background-color: #fff;
border-color: #fff;
color: rgba(0,0,0,.87)
}
.theme--dark .v-sheet {
background-color: #1e1e1e;
border-color: #1e1e1e;
color: #fff
}
</style>
{% endblock header %}
</head>
{% raw -%}
Expand Down Expand Up @@ -175,6 +186,46 @@
{# next div is used in ssg code to see if vue took over rendering #}
<div id="pre-rendered-html-present" style="display: none"></div>
</div>
<script>
solara.rootPath = {{ root_path | tojson | safe}};
console.log("rootPath", solara.rootPath);
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';
}
}
// Init theme
let app_container = document.getElementById('app');
if (inDarkMode()) {
app_container.classList.remove('theme--light');
app_container.classList.add('theme--dark');
} else {
app_container.classList.remove('theme--dark');
app_container.classList.add('theme--light');
}
async function changeThemeCSS(theme) {
let css = await fetch(`${solara.rootPath}/static/assets/theme-${theme}.css`).then(r => r.text());
document.getElementById('jupyter-theme-css').innerHTML = css;
}
changeThemeCSS(inDarkMode() ? 'dark' : 'light');
</script>
{% block after_pre_rendered_html %}{% endblock %}
{% if vue3 == True %}
<link href="{{cdn}}/@widgetti/[email protected]/dist/fonts.css" rel="stylesheet"></link>
Expand All @@ -191,10 +242,6 @@
<script src="{{cdn}}/@widgetti/[email protected]/dist/solara-vuetify-app{{ipywidget_major_version}}.js"></script>
{% endif %}
{% endif %}
<script>
solara.rootPath = {{ root_path | tojson | safe}};
console.log("rootPath", solara.rootPath);
</script>

{{ resources.include_js("/static/assets/custom.js") }}
{{ resources.include_js("/static/assets/theme.js") }}
Expand All @@ -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';
}
}
</script>

<script>
Expand Down Expand Up @@ -256,11 +282,7 @@
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
this.$vuetify.theme.dark = inDarkMode();
});
if (this.$vuetify.theme.dark) {
this.changeThemeCSS('dark');
} else {
this.changeThemeCSS('light');
}
document.getElementById('pre-render-theme').remove();
},
methods: {
stateReset() {
Expand All @@ -275,10 +297,6 @@
reload() {
location.reload();
},
async changeThemeCSS(theme) {
let css = await fetch(`${solara.rootPath}/static/assets/theme-${theme}.css`).then(r => r.text());
document.getElementById('jupyter-theme-css').innerHTML = css;
}
},
watch: {
kernelBusy: function (value) {
Expand Down Expand Up @@ -325,15 +343,14 @@
}
},
'$vuetify.theme.dark': function (value) {
let app = document.getElementById('app');
if ( value ) {
this.changeThemeCSS('dark');
app.classList.remove('theme--light');
app.classList.add('theme--dark');
app_container.classList.remove('theme--light');
app_container.classList.add('theme--dark');
} else {
this.changeThemeCSS('light');
app.classList.remove('theme--dark');
app.classList.add('theme--light');
app_container.classList.remove('theme--dark');
app_container.classList.add('theme--light');
}
}
},
Expand Down

0 comments on commit a7ab230

Please sign in to comment.