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: relative css paths (#8654)

Dylan Madisetti5cde332aed42
diff --git a/marimo/_server/templates/templates.py b/marimo/_server/templates/templates.py
index 8d7ba2545c..c2e403a33a 100644
--- a/marimo/_server/templates/templates.py
+++ b/marimo/_server/templates/templates.py
@@ -291,16 +291,24 @@ def notebook_page_template(
html = html.replace("</head>", f"{opengraph_tags}\n</head>")
# If has custom css, inline the css and add to the head
+ # Prefer the absolute path for IO, since `filename` can be a display
+ # path (workspace-relative) in gallery/workspace mode.
if app_config.css_file:
- css_contents = read_css_file(app_config.css_file, filename=filename)
+ css_contents = read_css_file(
+ app_config.css_file, filename=filepath or filename
+ )
if css_contents:
css_contents = _custom_css_block(css_contents)
# Append to head
html = html.replace("</head>", f"{css_contents}</head>")
# Add custom CSS from display config
- html = _inject_custom_css_for_config(html, user_config, filename)
- html = _inject_custom_css_for_config(html, config_overrides, filename)
+ html = _inject_custom_css_for_config(
+ html, user_config, filepath or filename
+ )
+ html = _inject_custom_css_for_config(
+ html, config_overrides, filepath or filename
+ )
# Add global HTML head contents if specified (from create_asgi_app)
if html_head:
@@ -309,7 +317,7 @@ def notebook_page_template(
# Add per-notebook HTML head file contents if specified
if app_config.html_head_file:
head_contents = read_html_head_file(
- app_config.html_head_file, filename=filename
+ app_config.html_head_file, filename=filepath or filename
)
if head_contents:
# Append to head
diff --git a/tests/_server/templates/test_templates.py b/tests/_server/templates/test_templates.py
index ef6db064e6..a66cd156a9 100644
--- a/tests/_server/templates/test_templates.py
+++ b/tests/_server/templates/test_templates.py
@@ -159,6 +159,60 @@ class TestNotebookPageTemplate(unittest.TestCase):
assert css in result
_assert_no_leftover_replacements(result)
+ def test_notebook_page_template_custom_css_workspace_relative(
+ self,
+ ) -> None:
+ # Simulate workspace mode: filename is workspace-relative display path,
+ # filepath is the absolute path used for I/O.
+ subdir = self.tmp_path / "data"
+ subdir.mkdir()
+ notebook = subdir / "notebook.py"
+ css = "/* workspace relative css */"
+ css_file = subdir / "custom.css"
+ css_file.write_text(css)
+
+ result = templates.notebook_page_template(
+ html=self.html,
+ base_url=self.base_url,
+ user_config=self.user_config,
+ config_overrides=self.config_overrides,
+ server_token=self.server_token,
+ app_config=_AppConfig(css_file="custom.css"),
+ filename="data/notebook.py", # workspace-relative display name
+ filepath=str(notebook.resolve()), # absolute path for I/O
+ mode=self.mode,
+ )
+
+ assert css in result
+ _assert_no_leftover_replacements(result)
+
+ def test_notebook_page_template_custom_head_workspace_relative(
+ self,
+ ) -> None:
+ # Simulate workspace mode: filename is workspace-relative display path,
+ # filepath is the absolute path used for I/O.
+ subdir = self.tmp_path / "data"
+ subdir.mkdir()
+ notebook = subdir / "notebook.py"
+ head = "<style>.workspace-specific { color: blue; }</style>"
+ head_file = subdir / "head.html"
+ head_file.write_text(head)
+
+ result = templates.notebook_page_template(
+ html=self.html,
+ base_url=self.base_url,
+ user_config=self.user_config,
+ config_overrides=self.config_overrides,
+ server_token=self.server_token,
+ app_config=_AppConfig(html_head_file="head.html"),
+ filename="data/notebook.py", # workspace-relative display name
+ filepath=str(notebook.resolve()), # absolute path for I/O
+ mode=self.mode,
+ )
+
+ assert head in result.split("</head>", 1)[0]
+ _assert_no_leftover_replacements(result)
+
def test_notebook_page_template_custom_head(self) -> None:
# Create html head file
head = """