Skip to content

fix: support mairmo edit on marimo.app #4840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions marimo/_cli/file_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from typing import Optional
from urllib.error import HTTPError

from marimo import _loggers
from marimo import __version__, _loggers
from marimo._cli.print import green
from marimo._utils.marimo_path import MarimoPath
from marimo._utils.url import is_url
Expand Down Expand Up @@ -105,7 +105,7 @@ def download(url: str) -> tuple[bool, str]:
request = urllib.request.Request(
url,
# User agent to avoid 403 Forbidden some bot protection
headers={"User-Agent": "Mozilla/5.0"},
headers={"User-Agent": f"marimo/{__version__}"},
)
file_contents = (
urllib.request.urlopen(request).read().decode("utf-8")
Expand All @@ -123,27 +123,38 @@ def download(url: str) -> tuple[bool, str]:
return download(url)

# Starts with https://static.marimo.app/, append /download
if url.startswith("https://static.marimo.app/"):
if url.startswith("https://static.marimo.app/static"):
return download(os.path.join(url, "download"))

# Starts with https://marimo.app/
if url.startswith("https://marimo.app/"):
return download(url)

# Otherwise, not a static marimo notebook
return False, ""

@staticmethod
def _extract_code_from_static_notebook(file_contents: str) -> str:
assert StaticNotebookReader.CODE_SUFFIX in file_contents, (
"<marimo-code> not found in file contents"
)
# normalize hidden attribute
file_contents = file_contents.replace("hidden=''", 'hidden=""')
return file_contents.split(StaticNotebookReader.CODE_PREFIX)[1].split(
StaticNotebookReader.CODE_SUFFIX
)[0]
prefix = StaticNotebookReader.CODE_PREFIX
suffix = StaticNotebookReader.CODE_SUFFIX
encoded_code = file_contents.split(prefix)[1].split(suffix)[0]
return urllib.parse.unquote(encoded_code)

@staticmethod
def _extract_filename_from_static_notebook(file_contents: str) -> str:
if StaticNotebookReader.FILENAME_SUFFIX not in file_contents:
return "notebook.py"
# normalize hidden attribute
file_contents = file_contents.replace("hidden=''", 'hidden=""')
return file_contents.split(StaticNotebookReader.FILENAME_PREFIX)[
1
].split(StaticNotebookReader.FILENAME_SUFFIX)[0]
prefix = StaticNotebookReader.FILENAME_PREFIX
suffix = StaticNotebookReader.FILENAME_SUFFIX
encoded_filename = file_contents.split(prefix)[1].split(suffix)[0]
return urllib.parse.unquote(encoded_filename)


class GitHubSourceReader(FileReader):
Expand Down
8 changes: 4 additions & 4 deletions tests/_cli/test_file_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def test_local_file_reader(tmp_path: Path) -> None:

def test_static_notebook_reader() -> None:
reader = StaticNotebookReader()
valid_url = "https://static.marimo.app/example"
valid_url = "https://static.marimo.app/static/example"
invalid_url = "https://example.com/file.py"

with patch.object(
Expand Down Expand Up @@ -274,9 +274,9 @@ def test_generic_url_reader_with_query_params():
def test_static_notebook_reader_url_formats():
reader = StaticNotebookReader()
urls = [
"https://static.marimo.app/example",
"https://static.marimo.app/example/",
"https://static.marimo.app/example.html",
"https://static.marimo.app/static/example",
"https://static.marimo.app/static/example/",
"https://static.marimo.app/static/example.html",
]
for url in urls:
with patch.object(
Expand Down
Loading