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.

fetch index.html from --asset-url as a fallback (#7453)

Myles Scolnickc6d0c5646a3f
diff --git a/marimo/_server/api/endpoints/assets.py b/marimo/_server/api/endpoints/assets.py
index fa8574fa36..09bdd03ec8 100644
--- a/marimo/_server/api/endpoints/assets.py
+++ b/marimo/_server/api/endpoints/assets.py
@@ -72,6 +72,33 @@ except RuntimeError:
FILE_QUERY_PARAM_KEY = "file"
+async def _fetch_index_html_from_url(asset_url: str) -> str:
+ """Fetch index.html from the given asset URL."""
+ import marimo._utils.requests as requests
+ from marimo._version import __version__
+
+ # Replace {version} placeholder if present
+ if "{version}" in asset_url:
+ asset_url = asset_url.replace("{version}", __version__)
+
+ # Construct the full URL to index.html
+ # Remove trailing slash if present
+ asset_url = asset_url.rstrip("/")
+ index_url = f"{asset_url}/index.html"
+
+ try:
+ LOGGER.debug("Fetching index.html from: %s", index_url)
+ response = requests.get(index_url)
+ response.raise_for_status()
+ return response.text()
+ except Exception as e:
+ LOGGER.error("Failed to fetch index.html from %s: %s", index_url, e)
+ raise HTTPException(
+ status_code=500,
+ detail=f"Failed to fetch index.html from asset_url: {e}",
+ ) from e
+
+
@router.get("/")
@requires("read", redirect="auth:login_page")
async def index(request: Request) -> HTMLResponse:
@@ -83,7 +110,20 @@ async def index(request: Request) -> HTMLResponse:
or app_state.session_manager.file_router.get_unique_file_key()
)
- html = index_html.read_text()
+ # Try local index.html first, fallback to asset_url if local file doesn't exist
+ if index_html.exists():
+ html = index_html.read_text()
+ elif app_state.asset_url:
+ LOGGER.info(
+ "Local index.html not found, fetching from asset_url: %s",
+ app_state.asset_url,
+ )
+ html = await _fetch_index_html_from_url(app_state.asset_url)
+ else:
+ raise HTTPException(
+ status_code=500,
+ detail="index.html not found and no asset_url configured",
+ )
if not file_key:
# We don't know which file to use, so we need to render a homepage
diff --git a/tests/_server/api/endpoints/test_assets.py b/tests/_server/api/endpoints/test_assets.py
index 9d1277bd2e..809658796e 100644
--- a/tests/_server/api/endpoints/test_assets.py
+++ b/tests/_server/api/endpoints/test_assets.py
@@ -6,6 +6,7 @@ import shutil
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING, Any, cast
+from unittest.mock import Mock, patch
from marimo._server.api.deps import AppState
from marimo._server.api.endpoints.assets import _inject_service_worker
@@ -217,3 +218,80 @@ def test_inject_service_worker() -> None:
"const notebookId = 'c%3A%5Cpath%5Cto%5Cnotebook.py';"
in _inject_service_worker("<body></body>", r"c:\path\to\notebook.py")
)
+
+
+def test_index_with_missing_local_file_and_asset_url(
+ client: TestClient,
+) -> None:
+ """Test that index.html is fetched from asset_url when local file doesn't exist."""
+ app_state = AppState.from_app(cast(Any, client.app))
+
+ # Mock HTML content that would come from the CDN
+ mock_html = """<!DOCTYPE html>
+<html>
+<head><title>{{ title }}</title></head>
+<body>
+<marimo-filename hidden>{{ filename }}</marimo-filename>
+'{{ mount_config }}'
+</body>
+</html>"""
+
+ # Mock the requests.get to return our mock HTML
+ mock_response = Mock()
+ mock_response.text.return_value = mock_html
+ mock_response.raise_for_status.return_value = None
+
+ with (
+ patch("marimo._server.api.endpoints.assets.root") as mock_root,
+ patch("marimo._utils.requests.get", return_value=mock_response),
+ ):
+ # Make local index.html appear to not exist
+ mock_index_html = Mock()
+ mock_index_html.exists.return_value = False
+ mock_root.__truediv__.return_value = mock_index_html
+
+ # Set asset_url on the app state
+ client.app.state.asset_url = "https://cdn.example.com/assets/0.1.0"
+
+ response = client.get("/", headers=token_header())
+ assert response.status_code == 200, response.text
+ # The response should contain processed HTML
+ assert "<title>" in response.text
+
+
+def test_index_with_missing_local_file_no_asset_url(
+ client: TestClient,
+) -> None:
+ """Test that error is raised when local file doesn't exist and no asset_url."""
+ with patch("marimo._server.api.endpoints.assets.root") as mock_root:
+ # Make local index.html appear to not exist
+ mock_index_html = Mock()
+ mock_index_html.exists.return_value = False
+ mock_root.__truediv__.return_value = mock_index_html
+
+ # Ensure asset_url is None
+ client.app.state.asset_url = None
+
+ response = client.get("/", headers=token_header())
+ assert response.status_code == 500
+ assert "index.html not found" in response.json()["detail"]
+
+
+def test_index_prefers_local_file_over_asset_url(client: TestClient) -> None:
+ """Test that local index.html is preferred even when asset_url is set."""
+ # Set asset_url on the app state
+ client.app.state.asset_url = "https://cdn.example.com/assets/0.1.0"
+
+ # Mock requests.get to track if it's called
+ mock_response = Mock()
+ with patch(
+ "marimo._utils.requests.get", return_value=mock_response
+ ) as mock_get:
+ response = client.get("/", headers=token_header())
+ assert response.status_code == 200, response.text
+
+ # requests.get should NOT be called since local file exists
+ mock_get.assert_not_called()
+
+ # Reset asset_url
+ client.app.state.asset_url = None