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.

improvement: change the filename in static html export to be just the file basename (#2518)

Myles Scolnick14bb4d6f13a1
diff --git a/marimo/_server/api/utils.py b/marimo/_server/api/utils.py
index 1c6cbf93d2..2a38966e91 100644
--- a/marimo/_server/api/utils.py
+++ b/marimo/_server/api/utils.py
@@ -39,16 +39,16 @@ async def parse_request(
)
-def parse_title(filename: Optional[str]) -> str:
+def parse_title(filepath: Optional[str]) -> str:
"""
- Parse a filename into a (name, extension) tuple.
+ Create a title from a filename.
"""
- if filename is None:
+ if filepath is None:
return "marimo"
# filename is used as title, except basename and suffix are
# stripped and underscores are replaced with spaces
- return os.path.splitext(os.path.basename(filename))[0].replace("_", " ")
+ return os.path.splitext(os.path.basename(filepath))[0].replace("_", " ")
def open_url_in_browser(browser: str, url: str) -> None:
diff --git a/marimo/_server/export/exporter.py b/marimo/_server/export/exporter.py
index 68241a4d06..e3847f07b7 100644
--- a/marimo/_server/export/exporter.py
+++ b/marimo/_server/export/exporter.py
@@ -95,7 +95,7 @@ class Exporter:
user_config=config,
server_token=SkewProtectionToken("static"),
app_config=file_manager.app.config,
- filename=file_manager.filename,
+ filepath=file_manager.filename,
code=code,
code_hash=code_hash,
cell_ids=cell_ids,
diff --git a/marimo/_server/templates/templates.py b/marimo/_server/templates/templates.py
index e90e07fe67..685b791a99 100644
--- a/marimo/_server/templates/templates.py
+++ b/marimo/_server/templates/templates.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import base64
import json
+import os
from textwrap import dedent
from typing import Any, List, Optional, cast
@@ -81,7 +82,7 @@ def static_notebook_template(
user_config: MarimoConfig,
server_token: SkewProtectionToken,
app_config: _AppConfig,
- filename: Optional[str],
+ filepath: Optional[str],
code: str,
code_hash: str,
cell_ids: list[str],
@@ -109,7 +110,7 @@ def static_notebook_template(
html = html.replace(
"{{ title }}",
- parse_title(filename)
+ parse_title(filepath)
if app_config.app_title is None
else app_config.app_title,
)
@@ -117,7 +118,7 @@ def static_notebook_template(
"{{ app_config }}",
json.dumps(_del_none_or_empty(app_config.asdict()), sort_keys=True),
)
- html = html.replace("{{ filename }}", filename or "")
+ html = html.replace("{{ filename }}", os.path.basename(filepath or ""))
html = html.replace("{{ mode }}", "read")
serialized_cell_outputs = {
@@ -152,7 +153,7 @@ def static_notebook_template(
# If has custom css, inline the css and add to the head
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)
if css_contents:
static_block += dedent(f"""
<style>
diff --git a/tests/_server/templates/test_templates.py b/tests/_server/templates/test_templates.py
index ae9c8bca73..7b748e4b0f 100644
--- a/tests/_server/templates/test_templates.py
+++ b/tests/_server/templates/test_templates.py
@@ -149,6 +149,7 @@ class TestStaticNotebookTemplate(unittest.TestCase):
self.server_token = SkewProtectionToken("token")
self.app_config = _AppConfig()
self.filename = "notebook.py"
+ self.filepath = "path/to/notebook.py"
self.code = "print('Hello, World!')"
self.cell_ids = ["cell1", "cell2"]
self.cell_names = ["Cell 1", "Cell 2"]
@@ -187,7 +188,7 @@ class TestStaticNotebookTemplate(unittest.TestCase):
self.user_config,
self.server_token,
self.app_config,
- self.filename,
+ self.filepath,
self.code,
hash_code(self.code),
self.cell_ids,
@@ -227,7 +228,7 @@ class TestStaticNotebookTemplate(unittest.TestCase):
self.user_config,
self.server_token,
self.app_config,
- self.filename,
+ self.filepath,
"",
hash_code(self.code),
[],