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.

improvement: add redirect_console_to_browser to marimo.create_asgi_app (#7249)

Myles Scolnick08c5569a4ba2
diff --git a/marimo/_server/asgi.py b/marimo/_server/asgi.py
index 408952f6e7..c92f170d89 100644
--- a/marimo/_server/asgi.py
+++ b/marimo/_server/asgi.py
@@ -294,6 +294,7 @@ def create_asgi_app(
skew_protection: bool = False,
session_ttl: Optional[int] = None,
asset_url: Optional[str] = None,
+ redirect_console_to_browser: bool = False,
) -> ASGIAppBuilder:
"""Public API to create an ASGI app that can serve multiple notebooks.
This only works for application that are in Run mode.
@@ -308,6 +309,8 @@ def create_asgi_app(
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
+ redirect_console_to_browser (bool, optional): Whether to redirect console output (stdout/stderr) to the browser.
+ When True, console output will be displayed in the browser. Defaults to False.
Returns:
ASGIAppBuilder: A builder object to create multiple ASGI apps
@@ -466,7 +469,7 @@ def create_asgi_app(
cli_args={},
argv=None,
auth_token=auth_token,
- redirect_console_to_browser=False,
+ redirect_console_to_browser=redirect_console_to_browser,
ttl_seconds=session_ttl,
)
enable_auth = not AuthToken.is_empty(auth_token)
diff --git a/tests/_server/test_asgi.py b/tests/_server/test_asgi.py
index 61679c19c2..fb59893f3b 100644
--- a/tests/_server/test_asgi.py
+++ b/tests/_server/test_asgi.py
@@ -309,6 +309,30 @@ class TestASGIAppBuilder(unittest.TestCase):
state = AppStateBase.from_app(app)
assert state.asset_url is None
+ def test_redirect_console_to_browser_parameter(self):
+ """Test that redirect_console_to_browser parameter is passed to SessionManager."""
+ builder = create_asgi_app(
+ quiet=True, include_code=True, redirect_console_to_browser=True
+ )
+ builder = builder.with_app(path="/app1", root=self.app1)
+ builder.build()
+
+ # Access the app state to verify redirect_console_to_browser was set
+ starlette_app = builder._app_cache[self.app1]
+ session_manager = starlette_app.state.session_manager
+ assert session_manager.redirect_console_to_browser is True
+
+ def test_redirect_console_to_browser_defaults_to_false(self):
+ """Test that redirect_console_to_browser defaults to False when not specified."""
+ builder = create_asgi_app(quiet=True, include_code=True)
+ builder = builder.with_app(path="/app1", root=self.app1)
+ builder.build()
+
+ # Access the app state to verify default value
+ starlette_app = builder._app_cache[self.app1]
+ session_manager = starlette_app.state.session_manager
+ assert session_manager.redirect_console_to_browser is False
+
class TestDynamicDirectoryMiddleware(unittest.TestCase):
def setUp(self):