Skip to content

Commit

Permalink
improvement: change the filename in static html export to be just the…
Browse files Browse the repository at this point in the history
… file basename (#2518)
  • Loading branch information
mscolnick authored Oct 7, 2024
1 parent f94e48d commit 14bb4d6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
8 changes: 4 additions & 4 deletions marimo/_server/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion marimo/_server/export/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def export_as_html(
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,
Expand Down
9 changes: 5 additions & 4 deletions marimo/_server/templates/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import base64
import json
import os
from textwrap import dedent
from typing import Any, List, Optional, cast

Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -109,15 +110,15 @@ 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,
)
html = html.replace(
"{{ 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 = {
Expand Down Expand Up @@ -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>
Expand Down
5 changes: 3 additions & 2 deletions tests/_server/templates/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def setUp(self) -> None:
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"]
Expand Down Expand Up @@ -187,7 +188,7 @@ def test_static_notebook_template(self) -> None:
self.user_config,
self.server_token,
self.app_config,
self.filename,
self.filepath,
self.code,
hash_code(self.code),
self.cell_ids,
Expand Down Expand Up @@ -227,7 +228,7 @@ def test_static_notebook_template_no_code(self) -> None:
self.user_config,
self.server_token,
self.app_config,
self.filename,
self.filepath,
"",
hash_code(self.code),
[],
Expand Down

0 comments on commit 14bb4d6

Please sign in to comment.