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: add --host to CLI (#674)

Myles Scolnick0aa624a4d720
diff --git a/marimo/_cli/cli.py b/marimo/_cli/cli.py
index aab98054e3..de94c8b46b 100644
--- a/marimo/_cli/cli.py
+++ b/marimo/_cli/cli.py
@@ -170,6 +170,13 @@ edit_help_msg = "\n".join(
type=int,
help="Port to attach to.",
)
+ "--host",
+ default="",
+ show_default=True,
+ type=str,
+ help="Host to attach to.",
+)
@click.option(
"--headless",
is_flag=True,
@@ -181,6 +188,7 @@ edit_help_msg = "\n".join(
@click.argument("name", required=False)
def edit(
port: Optional[int],
+ host: str,
headless: bool,
name: Optional[str] = None,
) -> None:
@@ -208,6 +216,7 @@ def edit(
start(
development_mode=DEVELOPMENT_MODE,
quiet=QUIET,
+ host=host,
port=port,
headless=headless,
filename=name,
@@ -235,6 +244,13 @@ Example:
type=int,
help="Port to attach to.",
)
+ "--host",
+ default="",
+ show_default=True,
+ type=str,
+ help="Host to attach to.",
+)
@click.option(
"--headless",
is_flag=True,
@@ -253,7 +269,11 @@ Example:
)
@click.argument("name", required=True)
def run(
- port: Optional[int], headless: bool, include_code: bool, name: str
+ port: Optional[int],
+ host: str,
+ headless: bool,
+ include_code: bool,
+ name: str,
) -> None:
# Validate name, or download from URL
# The second return value is an optional temporary directory. It is unused,
@@ -267,6 +287,7 @@ def run(
start(
development_mode=DEVELOPMENT_MODE,
quiet=QUIET,
+ host=host,
port=port,
headless=headless,
filename=name,
@@ -318,6 +339,13 @@ Recommended sequence:
type=int,
help="Port to attach to.",
)
+ "--host",
+ default="",
+ show_default=True,
+ type=str,
+ help="Host to attach to.",
+)
@click.option(
"--headless",
is_flag=True,
@@ -343,6 +371,7 @@ Recommended sequence:
)
def tutorial(
port: Optional[int],
+ host: str,
headless: bool,
name: Literal[
"intro", "dataflow", "ui", "markdown", "plots", "layout", "fileformat"
@@ -376,6 +405,7 @@ def tutorial(
start(
development_mode=DEVELOPMENT_MODE,
quiet=QUIET,
+ host=host,
port=port,
mode=SessionMode.EDIT,
filename=fname,
diff --git a/marimo/_server/api/lifespans.py b/marimo/_server/api/lifespans.py
index c0df84ed99..29ad62bd84 100644
--- a/marimo/_server/api/lifespans.py
+++ b/marimo/_server/api/lifespans.py
@@ -102,8 +102,9 @@ async def lsp(app: Starlette) -> AsyncIterator[None]:
@contextlib.asynccontextmanager
async def open_browser(app: Starlette) -> AsyncIterator[None]:
- session_mgr = get_manager()
- url = f"http://localhost:{session_mgr.port}"
+ host = app.state.host
+ port = app.state.port
+ url = f"http://{host}:{port}"
user_config = get_configuration()
headless = app.state.headless
if not headless:
@@ -118,14 +119,15 @@ async def open_browser(app: Starlette) -> AsyncIterator[None]:
@contextlib.asynccontextmanager
async def logging(app: Starlette) -> AsyncIterator[None]:
- del app
manager = get_manager()
+ host = app.state.host
+ port = app.state.port
# Startup message
if not manager.quiet:
print_startup(
manager.filename,
- f"http://localhost:{manager.port}",
+ f"http://{host}:{port}",
manager.mode == SessionMode.RUN,
)
diff --git a/marimo/_server/start.py b/marimo/_server/start.py
index 479546c3b0..c1100ac284 100644
--- a/marimo/_server/start.py
+++ b/marimo/_server/start.py
@@ -25,6 +25,7 @@ def start(
include_code: bool,
headless: bool,
port: Optional[int],
+ host: str,
) -> None:
"""
Start the server.
@@ -46,6 +47,8 @@ def start(
log_level = "info" if development_mode else "error"
app.state.headless = headless
+ app.state.port = port
+ app.state.host = host or "localhost"
app.state.session_manager = session_manager
app.state.user_config = get_configuration()
@@ -53,6 +56,7 @@ def start(
uvicorn.Config(
app,
port=port,
+ host=host,
# TODO: cannot use reload unless the app is an import string
# although cannot use import string because it breaks the
# session manager
diff --git a/tests/_cli/test_cli.py b/tests/_cli/test_cli.py
index da6e9d6b14..573ec876de 100644
--- a/tests/_cli/test_cli.py
+++ b/tests/_cli/test_cli.py
@@ -17,13 +17,11 @@ from marimo._ast import codegen
from marimo._ast.cell import CellConfig
-def _try_fetch(port: int) -> Optional[bytes]:
+def _try_fetch(port: int, host: str = "localhost") -> Optional[bytes]:
contents = None
for _ in range(10):
try:
- contents = urllib.request.urlopen(
- f"http://localhost:{port}"
- ).read()
+ contents = urllib.request.urlopen(f"http://{host}:{port}").read()
break
except Exception:
time.sleep(0.5)
@@ -138,3 +136,22 @@ def test_cli_tutorial() -> None:
p, f"marimo-version data-version='{__version__}'".encode(), contents
)
_check_contents(p, b"intro.py", contents)
+
+
+def test_cli_custom_host() -> None:
+ port = _get_port()
+ host = "localhost"
+ p = subprocess.Popen(
+ [
+ "marimo",
+ "tutorial",
+ "intro",
+ "-p",
+ str(port),
+ "--headless",
+ "--host",
+ host,
+ ]
+ )
+ contents = _try_fetch(port, host)
+ _check_contents(p, b"marimo-mode data-mode='edit'", contents)