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.

feat: Allow specifying asset_url for ASGI app (#6610)

Ahmetdc4cdc48ac88
diff --git a/marimo/_server/asgi.py b/marimo/_server/asgi.py
index 9059e70607..4bf9330c58 100644
--- a/marimo/_server/asgi.py
+++ b/marimo/_server/asgi.py
@@ -298,6 +298,7 @@ def create_asgi_app(
token: Optional[str] = None,
skew_protection: bool = False,
session_ttl: Optional[int] = None,
+ asset_url: Optional[str] = None,
) -> ASGIAppBuilder:
"""Public API to create an ASGI app that can serve multiple notebooks.
This only works for application that are in Run mode.
@@ -310,6 +311,8 @@ def create_asgi_app(
skew_protection (bool, optional): Enable skew protection middleware to prevent version mismatch issues.
e.g. if the server is updated, the client will be prompted to reload.
session_ttl (int, optional): Time-to-live in seconds for sessions. If not provided, uses default TTL (2 minutes).
+ asset_url (str, optional): Custom asset URL for loading static resources. Can include {version} placeholder.
+ e.g. https://cdn.jsdelivr.net/npm/@marimo-team/frontend@{version}/dist
Returns:
ASGIAppBuilder: A builder object to create multiple ASGI apps
@@ -400,6 +403,7 @@ def create_asgi_app(
config_reader = get_default_config_manager(current_path=None)
base_app = Starlette()
+ base_app.state.asset_url = asset_url
# Default to an empty token
# If a user is using the create_asgi_app API,
@@ -487,6 +491,7 @@ def create_asgi_app(
)
app.state.session_manager = session_manager
app.state.base_url = base_url
+ app.state.asset_url = asset_url
app.state.config_manager = config_reader
app.state.enable_auth = enable_auth
return app
diff --git a/tests/_server/test_asgi.py b/tests/_server/test_asgi.py
index 4e24e72ffb..61679c19c2 100644
--- a/tests/_server/test_asgi.py
+++ b/tests/_server/test_asgi.py
@@ -281,6 +281,34 @@ class TestASGIAppBuilder(unittest.TestCase):
session_manager = starlette_app.state.session_manager
assert session_manager.ttl_seconds == 300
+ def test_asset_url_parameter(self):
+ """Test that asset_url parameter is passed to app state."""
+ custom_asset_url = "https://my-cdn.com/assets/{version}/"
+ builder = create_asgi_app(
+ quiet=True, include_code=True, asset_url=custom_asset_url
+ )
+ app = builder.with_app(path="/app1", root=self.app1).build()
+
+ # Check that asset_url is set on the base app
+ from marimo._server.api.deps import AppStateBase
+
+ state = AppStateBase.from_app(app)
+ assert state.asset_url == custom_asset_url
+
+ # Also check that it's set on individual mounted apps
+ starlette_app = builder._app_cache[self.app1]
+ assert starlette_app.state.asset_url == custom_asset_url
+
+ def test_asset_url_none_by_default(self):
+ """Test that asset_url is None when not specified."""
+ builder = create_asgi_app(quiet=True, include_code=True)
+ app = builder.with_app(path="/app1", root=self.app1).build()
+
+ from marimo._server.api.deps import AppStateBase
+
+ state = AppStateBase.from_app(app)
+ assert state.asset_url is None
+
class TestDynamicDirectoryMiddleware(unittest.TestCase):
def setUp(self):