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: mo.notebook_location() with incorrect :// (#3393)

Myles Scolnickb9090cec2fc4
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index f9a0cc7ffa..385d317ecc 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -317,8 +317,17 @@ def notebook_dir() -> pathlib.Path | None:
return None
+class URLPath(pathlib.PurePosixPath):
+ """
+ Wrapper around pathlib.Path that preserves the "://" in the URL protocol.
+ """
+
+ def __str__(self) -> str:
+ return super().__str__().replace(":/", "://")
+
+
@mddoc
-def notebook_location() -> pathlib.Path | None:
+def notebook_location() -> pathlib.PurePath | None:
"""Get the location of the currently executing notebook.
In WASM, this is the URL of webpage, for example, `https://my-site.com`.
@@ -342,7 +351,7 @@ def notebook_location() -> pathlib.Path | None:
```
Returns:
- pathlib.Path | None: A pathlib.Path object representing the URL or directory of the current
+ Path | None: A Path object representing the URL or directory of the current
notebook, or None if the notebook's directory cannot be determined.
"""
if is_pyodide():
@@ -352,11 +361,14 @@ def notebook_location() -> pathlib.Path | None:
# The location looks like https://marimo-team.github.io/marimo-gh-pages-template/notebooks/assets/worker-BxJ8HeOy.js
# We want to crawl out of the assets/ folder
if "assets" in path_location.parts:
- return path_location.parent.parent
- return path_location
+ return URLPath(str(path_location.parent.parent))
+ return URLPath(str(path_location))
else:
- return notebook_dir()
+ nb_dir = notebook_dir()
+ if nb_dir is not None:
+ return nb_dir
+ return None
@dataclasses.dataclass
diff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.py
index df2b3eb5ab..c8db51979b 100644
--- a/tests/_runtime/test_runtime.py
+++ b/tests/_runtime/test_runtime.py
@@ -4,7 +4,6 @@ from __future__ import annotations
import sys
import textwrap
from dataclasses import dataclass
-from pathlib import Path
from typing import TYPE_CHECKING, Sequence
import pytest
@@ -1039,6 +1038,9 @@ class TestExecution:
assert "dir" in k.globals
assert k.globals["dir"] is k.globals["loc"]
+ @pytest.mark.skipif(
+ sys.platform == "win32", reason="Windows paths behave differently"
+ )
async def test_notebook_location_for_pyodide(
self, any_kernel: Kernel, exec_req: ExecReqProvider
) -> None:
@@ -1060,8 +1062,13 @@ class TestExecution:
)
]
)
- assert k.globals["loc"] == Path(
- "https://marimo-team.github.io/marimo-gh-pages-template/notebooks"
+ assert (
+ str(k.globals["loc"])
+ == "https://marimo-team.github.io/marimo-gh-pages-template/notebooks"
+ )
+ assert (
+ str(k.globals["loc"] / "public" / "data.csv")
+ == "https://marimo-team.github.io/marimo-gh-pages-template/notebooks/public/data.csv"
)
finally:
del sys.modules["pyodide"]