Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: copy custom stylesheet (#3818)

Myles Scolnickb67437ba180c
diff --git a/frontend/src/plugins/core/registerReactComponent.tsx b/frontend/src/plugins/core/registerReactComponent.tsx
index d92297f438..3397644d05 100644
--- a/frontend/src/plugins/core/registerReactComponent.tsx
+++ b/frontend/src/plugins/core/registerReactComponent.tsx
@@ -380,7 +380,8 @@ export function registerReactComponent<T>(plugin: IPlugin<T, unknown>): void {
sheet.href ??
(sheet.ownerNode instanceof HTMLElement
? sheet.ownerNode.dataset.viteDevId
- : undefined);
+ : undefined) ??
+ sheet.title;
if (!sheetUniqueKey) {
continue;
}
@@ -474,16 +475,25 @@ export function registerReactComponent<T>(plugin: IPlugin<T, unknown>): void {
// Copy the stylesheet to the shadow root if it is local
// or from our assetUrl (in the case of a static notebook)
+//
+// This logic is not for security reasons, but rather to
+// avoid pollution of various 3rd party stylesheets
function shouldCopyStyleSheet(sheet: CSSStyleSheet): boolean {
+ // Copy local stylesheets owned by marimo
+ if (sheet.title?.startsWith("marimo")) {
+ return true;
+ }
+
if (!sheet.href) {
return false;
}
- // Must end with .css
+ // Must stylesheet must end with .css
if (!sheet.href.endsWith(".css")) {
return false;
}
+ // Copy stylesheets from localhost in development mode
if (
sheet.href.includes("127.0.0.1") &&
(process.env.NODE_ENV === "test" || process.env.NODE_ENV === "development")
@@ -491,10 +501,12 @@ function shouldCopyStyleSheet(sheet: CSSStyleSheet): boolean {
return true;
}
+ // Copy stylesheets from our NPM package
if (sheet.href.includes("/@marimo-team/")) {
return true;
}
+ // Copy stylesheets served from the same origin
return sheet.href.startsWith(window.location.origin);
}
diff --git a/frontend/vite.config.mts b/frontend/vite.config.mts
index 59514edeb6..a885616d9f 100644
--- a/frontend/vite.config.mts
+++ b/frontend/vite.config.mts
@@ -165,6 +165,12 @@ If the server is already running, make sure it is using port ${SERVER_PORT} with
devDoc.head.append(element);
});
+ // Copy styles
+ const styles = serverDoc.querySelectorAll("style");
+ styles.forEach((style) => {
+ devDoc.head.append(style);
+ });
+
return `<!DOCTYPE html>\n${devDoc.documentElement.outerHTML}`;
},
};
diff --git a/marimo/_server/templates/templates.py b/marimo/_server/templates/templates.py
index 466e9797ea..2d68d0cd30 100644
--- a/marimo/_server/templates/templates.py
+++ b/marimo/_server/templates/templates.py
@@ -77,7 +77,7 @@ def notebook_page_template(
if app_config.css_file:
css_contents = read_css_file(app_config.css_file, filename=filename)
if css_contents:
- css_contents = f"<style>{css_contents}</style>"
+ css_contents = _custom_css_block(css_contents)
# Append to head
html = html.replace("</head>", f"{css_contents}</head>")
@@ -195,13 +195,7 @@ def static_notebook_template(
if app_config.css_file:
css_contents = read_css_file(app_config.css_file, filename=filepath)
if css_contents:
- static_block += dedent(
- f"""
- <style>
- {css_contents}
- </style>
- """
- )
+ static_block += _custom_css_block(css_contents)
code_block = dedent(
f"""
@@ -297,7 +291,7 @@ def wasm_notebook_template(
if app_config.css_file:
css_contents = read_css_file(app_config.css_file, filename=filename)
if css_contents:
- css_contents = f"<style>{css_contents}</style>"
+ css_contents = _custom_css_block(css_contents)
# Append to head
body = body.replace("</head>", f"{css_contents}</head>")
@@ -352,3 +346,9 @@ def get_version() -> str:
return (
f"{__version__} (editable)" if is_editable("marimo") else __version__
)
+
+
+def _custom_css_block(css_contents: str) -> str:
+ # marimo-custom is used by the frontend to identify this stylesheet
+ # comes from marimo
+ return f"<style title='marimo-custom'>{css_contents}</style>"
diff --git a/marimo/_smoke_tests/theming/custom.css b/marimo/_smoke_tests/theming/custom.css
index 7aaa221b54..2f9fb948c3 100644
--- a/marimo/_smoke_tests/theming/custom.css
+++ b/marimo/_smoke_tests/theming/custom.css
@@ -15,3 +15,8 @@
h1 {
color: light-dark(red, blue) !important;
}
+
+input[inputmode="numeric"] {
+ border: 1px solid red;
+ text-align: right;
+}
diff --git a/marimo/_smoke_tests/theming/custom_css.py b/marimo/_smoke_tests/theming/custom_css.py
index ca311783f6..38fa846f04 100644
--- a/marimo/_smoke_tests/theming/custom_css.py
+++ b/marimo/_smoke_tests/theming/custom_css.py
@@ -5,37 +5,40 @@
# "marimo",
# ]
# ///
+
import marimo
-__generated_with = "0.8.3"
+__generated_with = "0.11.5"
app = marimo.App(width="medium", css_file="custom.css")
@app.cell
-def __():
+def _():
import marimo as mo
from vega_datasets import data
return data, mo
@app.cell
-def __(data):
+def _(data):
data.cars()
return
@app.cell
-def __(mo):
- mo.callout(mo.md("""
+def _(mo):
+ mo.callout(
+ mo.md("""
## Callout
This font should be styled
- """))
+ """)
+ )
return
@app.cell
-def __(mo):
+def _(mo):
mo.md(
r"""
# heading
@@ -46,5 +49,12 @@ def __(mo):
return
+def _(mo):
+ # This text should be aligned-right
+ mo.ui.number(value=50)
+ return
+
+
if __name__ == "__main__":
app.run()
diff --git a/tests/_server/templates/snapshots/export4.txt b/tests/_server/templates/snapshots/export4.txt
index a3044edc5d..06e5f92f0f 100644
--- a/tests/_server/templates/snapshots/export4.txt
+++ b/tests/_server/templates/snapshots/export4.txt
@@ -69,11 +69,7 @@
window.__MARIMO_STATIC__.assetUrl = "https://cdn.jsdelivr.net/npm/@marimo-team/[email protected]/dist";
window.__MARIMO_STATIC__.files = {};
</script>
-
-<style>
- /* custom css */
-</style>
-</head>
+<style title='marimo-custom'>/* custom css */</style></head>
<body>
<div id="root"></div>
<marimo-code hidden=""></marimo-code>