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.

preview: add `--server-startup-command` (#6238)

Myles Scolnick211da1f4200e
diff --git a/frontend/src/components/editor/alerts/startup-logs-alert.tsx b/frontend/src/components/editor/alerts/startup-logs-alert.tsx
new file mode 100644
index 0000000000..e6a703f6de
--- /dev/null
+++ b/frontend/src/components/editor/alerts/startup-logs-alert.tsx
@@ -0,0 +1,71 @@
+import { BoxIcon, XIcon } from "lucide-react";
+import { useEffect, useState } from "react";
+import { useEvent } from "react-use-event-hook";
+import { useAlertActions, useAlerts } from "@/core/alerts/state";
+import { Banner } from "@/plugins/impl/common/error-banner";
+import { Button } from "../../ui/button";
+
+export const StartupLogsAlert: React.FC = () => {
+ const { startupLogsAlert } = useAlerts();
+ const { clearStartupLogsAlert } = useAlertActions();
+ const [hasCleared, setHasCleared] = useState(false);
+ const status = startupLogsAlert?.status;
+
+ const handleClear = useEvent(() => {
+ clearStartupLogsAlert();
+ setHasCleared(true);
+ });
+
+ const isDone = status === "done";
+ const isRunning = status === "start" || status === "append";
+
+ useEffect(() => {
+ let timeout: number | undefined;
+
+ if (isDone) {
+ // Dismiss after 5 seconds
+ timeout = window.setTimeout(() => handleClear(), 5_000);
+ }
+
+ return () => {
+ if (timeout) {
+ window.clearTimeout(timeout);
+ }
+ };
+ }, [isDone]);
+
+ if (startupLogsAlert === null || hasCleared) {
+ return null;
+ }
+
+ return (
+ <div className="flex flex-col gap-4 mb-5 fixed top-5 left-12 min-w-[400px] max-w-[600px] z-200 opacity-95">
+ <Banner
+ kind="info"
+ className="flex flex-col rounded py-3 px-5 animate-in slide-in-from-left"
+ >
+ <div className="flex justify-between">
+ <span className="font-bold text-lg flex items-center mb-2">
+ <BoxIcon className="w-5 h-5 inline-block mr-2" />
+ {isRunning ? "Initializing..." : "Initialized"}
+ </span>
+ <Button
+ variant="text"
+ data-testid="remove-startup-logs-button"
+ size="icon"
+ onClick={handleClear}
+ >
+ <XIcon className="w-5 h-5" />
+ </Button>
+ </div>
+ <div className="flex flex-col gap-4 justify-between items-start text-muted-foreground text-base">
+ <div className="w-full">
+ <pre className="bg-muted p-3 rounded text-sm font-mono overflow-auto max-h-64 whitespace-pre-wrap">
+ {startupLogsAlert.content || "Starting startup script..."}
+ </pre>
+ </div>
+ </div>
+ </Banner>
+ </div>
+ );
+};
diff --git a/frontend/src/components/editor/renderers/CellArray.tsx b/frontend/src/components/editor/renderers/CellArray.tsx
index fe48da96ce..7d74623d3b 100644
--- a/frontend/src/components/editor/renderers/CellArray.tsx
+++ b/frontend/src/components/editor/renderers/CellArray.tsx
@@ -13,6 +13,7 @@ import {
SquareMIcon,
} from "lucide-react";
import { useEffect } from "react";
+import { StartupLogsAlert } from "@/components/editor/alerts/startup-logs-alert";
import { Cell } from "@/components/editor/Cell";
import { PackageAlert } from "@/components/editor/package-alert";
import { SortableCellsProvider } from "@/components/sort/SortableCellsProvider";
@@ -120,6 +121,7 @@ const CellArrayInternal: React.FC<CellArrayProps> = ({
innerClassName="pr-4" // For the floating actions
>
<PackageAlert />
+ <StartupLogsAlert />
<StdinBlockingAlert />
<ConnectingAlert />
<NotebookBanner width={appConfig.width} />
diff --git a/frontend/src/core/alerts/state.ts b/frontend/src/core/alerts/state.ts
index e90160e54e..fa600564ce 100644
--- a/frontend/src/core/alerts/state.ts
+++ b/frontend/src/core/alerts/state.ts
@@ -18,6 +18,11 @@ export interface InstallingPackageAlert {
packages: PackageInstallationStatus;
}
+export interface StartupLogsAlert {
+ content: string;
+ status: "append" | "start" | "done";
+}
+
export function isMissingPackageAlert(
alert: MissingPackageAlert | InstallingPackageAlert,
): alert is MissingPackageAlert {
@@ -39,10 +44,11 @@ interface AlertState {
| Identified<MissingPackageAlert>
| Identified<InstallingPackageAlert>
| null;
+ startupLogsAlert: StartupLogsAlert | null;
}
const { valueAtom: alertAtom, useActions } = createReducerAndAtoms(
- () => ({ packageAlert: null }) as AlertState,
+ () => ({ packageAlert: null, startupLogsAlert: null }) as AlertState,
{
addPackageAlert: (
state,
@@ -59,6 +65,25 @@ const { valueAtom: alertAtom, useActions } = createReducerAndAtoms(
? { ...state, packageAlert: null }
: state;
},
+
+ addStartupLog: (
+ state,
+ logData: { content: string; status: "append" | "start" | "done" },
+ ) => {
+ const prevContent = state.startupLogsAlert?.content || "";
+ return {
+ ...state,
+ startupLogsAlert: {
+ ...state.startupLogsAlert,
+ content: prevContent + logData.content,
+ status: logData.status,
+ },
+ };
+ },
+
+ clearStartupLogsAlert: (state) => {
+ return { ...state, startupLogsAlert: null };
+ },
},
);
diff --git a/frontend/src/core/islands/main.ts b/frontend/src/core/islands/main.ts
index 81cc5e17b1..1693295583 100644
--- a/frontend/src/core/islands/main.ts
+++ b/frontend/src/core/islands/main.ts
@@ -120,6 +120,7 @@ export async function initialize() {
case "datasets":
case "data-source-connections":
case "secret-keys-result":
+ case "startup-logs":
// Unsupported
return;
case "kernel-ready":
diff --git a/frontend/src/core/kernel/messages.ts b/frontend/src/core/kernel/messages.ts
index b3dc9dbd31..0b6d1182f6 100644
--- a/frontend/src/core/kernel/messages.ts
+++ b/frontend/src/core/kernel/messages.ts
@@ -39,6 +39,7 @@ export type SQLTablePreview = OperationMessageData<"sql-table-preview">;
export type SQLTableListPreview =
OperationMessageData<"sql-table-list-preview">;
export type SecretKeysResult = OperationMessageData<"secret-keys-result">;
+export type StartupLogs = OperationMessageData<"startup-logs">;
export type MessageOperation = schemas["KnownUnions"]["operation"];
export type OperationMessageType = MessageOperation["op"];
diff --git a/frontend/src/core/websocket/useMarimoWebSocket.tsx b/frontend/src/core/websocket/useMarimoWebSocket.tsx
index 69c259e9de..201ce08b6e 100644
--- a/frontend/src/core/websocket/useMarimoWebSocket.tsx
+++ b/frontend/src/core/websocket/useMarimoWebSocket.tsx
@@ -79,7 +79,7 @@ export function useMarimoWebSocket(opts: {
const { setLayoutData } = useLayoutActions();
const [connection, setConnection] = useAtom(connectionAtom);
const { addBanner } = useBannersActions();
- const { addPackageAlert } = useAlertActions();
+ const { addPackageAlert, addStartupLog } = useAlertActions();
const setKioskMode = useSetAtom(kioskModeAtom);
const setCapabilities = useSetAtom(capabilitiesAtom);
const runtimeManager = useRuntimeManager();
@@ -204,6 +204,12 @@ export function useMarimoWebSocket(opts: {
kind: "installing",
});
return;
+ case "startup-logs":
+ addStartupLog({
+ content: msg.data.content,
+ status: msg.data.status,
+ });
+ return;
case "query-params-append":
queryParamHandlers.append(msg.data);
return;
diff --git a/marimo/_cli/cli.py b/marimo/_cli/cli.py
index a447181b0f..ab941bd95f 100644
--- a/marimo/_cli/cli.py
+++ b/marimo/_cli/cli.py
@@ -454,6 +454,13 @@ https://github.com/marimo-team/marimo/issues/5219.""",
hidden=True,
help="Enable MCP server endpoint at /mcp/server for LLM integration.",
)
+ "--server-startup-command",
+ default=None,
+ type=str,
+ hidden=True,
+ help="Command to run on server startup.",
+)
@click.option(
"--asset-url",
default=None,
@@ -493,6 +500,7 @@ def edit(
remote_url: Optional[str],
convert: bool,
mcp: bool,
+ server_startup_command: Optional[str],
asset_url: Optional[str],
timeout: Optional[float],
name: Optional[str],
@@ -623,6 +631,7 @@ def edit(
ttl_seconds=None,
remote_url=remote_url,
mcp=mcp,
+ server_startup_command=server_startup_command,
asset_url=asset_url,
timeout=timeout,
)
@@ -943,6 +952,13 @@ Example:
type=bool,
help=sandbox_message,
)
+ "--server-startup-command",
+ default=None,
+ type=str,
+ hidden=True,
+ help="Command to run on server startup.",
+)
@click.option(
"--asset-url",
default=None,
@@ -971,6 +987,7 @@ def run(
allow_origins: tuple[str, ...],
redirect_console_to_browser: bool,
sandbox: Optional[bool],
+ server_startup_command: Optional[str],
asset_url: Optional[str],
name: str,
args: tuple[str, ...],
@@ -1028,6 +1045,7 @@ def run(
argv=list(args),
auth_token=_resolve_token(token, token_password),
redirect_console_to_browser=redirect_console_to_browser,
+ server_startup_command=server_startup_command,
asset_url=asset_url,
)
diff --git a/marimo/_messaging/ops.py b/marimo/_messaging/ops.py
index 2cd59ab449..5110006e80 100644
--- a/marimo/_messaging/ops.py
+++ b/marimo/_messaging/ops.py
@@ -450,6 +450,12 @@ class Reconnected(Op, tag="reconnected"):
name: ClassVar[str] = "reconnected"
+class StartupLogs(Op, tag="startup-logs"):
+ name: ClassVar[str] = "startup-logs"
+ content: str
+ status: Literal["append", "start", "done"]
+
+
class Banner(Op, tag="banner"):
name: ClassVar[str] = "banner"
title: str
@@ -674,6 +680,7 @@ MessageOperation = Union[
Banner,
MissingPackageAlert,
InstallingPackageAlert,
+ StartupLogs,
# Variables
Variables,
VariableValues,
diff --git a/marimo/_server/session/session_view.py b/marimo/_server/session/session_view.py
index 412020951c..de50ce0449 100644
--- a/marimo/_server/session/session_view.py
+++ b/marimo/_server/session/session_view.py
@@ -14,6 +14,7 @@ from marimo._messaging.ops import (
Interrupted,
MessageOperation,
SendUIElementMessage,
+ StartupLogs,
UpdateCellCodes,
UpdateCellIdsRequest,
Variables,
@@ -95,6 +96,9 @@ class SessionView:
WidgetModelId, list[SendUIElementMessage]
] = {}
+ # Startup logs for startup command - only one at a time
+ self.startup_logs: Optional[StartupLogs] = None
+
# Auto-saving
self.auto_export_state = AutoExportState()
@@ -141,11 +145,10 @@ class SessionView:
return
def add_operation(self, operation: MessageOperation) -> None:
+ """Add an operation to the session view."""
self._touch()
self.auto_export_state.mark_all_stale()
- """Add an operation to the session view."""
-
if isinstance(operation, CellOp):
previous = self.cell_operations.get(operation.cell_id)
self.cell_operations[operation.cell_id] = merge_cell_operation(
@@ -253,6 +256,13 @@ class SessionView:
# TODO: cleanup/merge previous 'update' messages
self.model_messages[operation.model_id] = messages
+ elif isinstance(operation, StartupLogs):
+ prev = self.startup_logs.content if self.startup_logs else ""
+ self.startup_logs = StartupLogs(
+ content=prev + operation.content,
+ status=operation.status,
+ )
+
def get_cell_outputs(
self, ids: list[CellId_t]
) -> dict[CellId_t, CellOutput]:
@@ -314,6 +324,9 @@ class SessionView:
if self.model_messages:
for messages in self.model_messages.values():
all_ops.extend(messages)
+ # Only include startup logs if they are in progress (not done)
+ if self.startup_logs and self.startup_logs.status != "done":
+ all_ops.append(self.startup_logs)
return all_ops
def is_empty(self) -> bool:
diff --git a/marimo/_server/start.py b/marimo/_server/start.py
index d3b071c8aa..4404668f9f 100644
--- a/marimo/_server/start.py
+++ b/marimo/_server/start.py
@@ -3,13 +3,17 @@ from __future__ import annotations
import os
import re
+import subprocess
+import threading
from typing import Optional
import uvicorn
import marimo._server.api.lifespans as lifespans
+from marimo._cli.print import echo
from marimo._config.manager import get_default_config_manager
from marimo._config.settings import GLOBAL_SETTINGS
+from marimo._messaging.ops import StartupLogs
from marimo._runtime.requests import SerializedCLIArgs
from marimo._server.file_router import AppFileRouter
from marimo._server.lsp import CompositeLspServer, NoopLspServer
@@ -32,6 +36,77 @@ DEFAULT_PORT = 2718
PROXY_REGEX = re.compile(r"^(.*):(\d+)$")
+def _execute_startup_command(
+ command: str, session_manager: SessionManager
+) -> None:
+ """Execute a server startup command in a background thread and stream logs."""
+
+ def run_command() -> None:
+ buffer = StartupLogs(content="", status="start")
+
+ try:
+
+ def write_to_all_sessions(
+ content: StartupLogs, buffer: StartupLogs
+ ) -> None:
+ for session in session_manager.sessions.values():
+ # Clear buffer if it has content
+ if buffer.content != "":
+ session.write_operation(buffer, from_consumer_id=None)
+ buffer = StartupLogs(content="", status="start")
+ session.write_operation(content, from_consumer_id=None)
+ else:
+ buffer.content += content.content
+ buffer.status = content.status
+
+ # Broadcast start message to all sessions
+ write_to_all_sessions(
+ StartupLogs(content="", status="start"), buffer
+ )
+
+ # Execute the command
+ process = subprocess.Popen(
+ command,
+ shell=True,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT, # Stream stderr to stdout
+ text=True,
+ universal_newlines=True,
+ )
+
+ # Stream output line by line
+ if process.stdout:
+ for line in process.stdout:
+ write_to_all_sessions(
+ StartupLogs(content=line, status="append"), buffer
+ )
+ echo(line, nl=False)
+
+ # Wait for process to complete
+ return_code = process.wait()
+
+ # Broadcast completion message
+ final_message = (
+ f"\nProcess completed with exit code: {return_code}\n"
+ )
+ write_to_all_sessions(
+ StartupLogs(content=final_message, status="done"), buffer
+ )
+ echo(final_message)
+
+ except Exception as e:
+ # Broadcast error message
+ error_message = f"\nError executing startup command: {str(e)}\n"
+ write_to_all_sessions(
+ StartupLogs(content=error_message, status="done"), buffer
+ )
+ echo(error_message)
+
+ # Run the command in a background thread
+ thread = threading.Thread(target=run_command)
+ thread.start()
+
+
def _resolve_proxy(
port: int, host: str, proxy: Optional[str]
) -> tuple[int, str]:
@@ -91,6 +166,7 @@ def start(
skew_protection: bool,
remote_url: Optional[str] = None,
mcp: bool = False,
+ server_startup_command: Optional[str] = None,
asset_url: Optional[str] = None,
timeout: Optional[float] = None,
) -> None:
@@ -249,4 +325,9 @@ def start(
app.state.server = server
initialize_asyncio()
+
+ # Execute server startup command if provided
+ if server_startup_command:
+ _execute_startup_command(server_startup_command, session_manager)
+
server.run()
diff --git a/packages/openapi/api.yaml b/packages/openapi/api.yaml
index 291615a76c..3a2cb9f87a 100644
--- a/packages/openapi/api.yaml
+++ b/packages/openapi/api.yaml
@@ -1842,6 +1842,7 @@ components:
- $ref: '#/components/schemas/Banner'
- $ref: '#/components/schemas/MissingPackageAlert'
- $ref: '#/components/schemas/InstallingPackageAlert'
+ - $ref: '#/components/schemas/StartupLogs'
- $ref: '#/components/schemas/Variables'
- $ref: '#/components/schemas/VariableValues'
- $ref: '#/components/schemas/QueryParamsSet'
@@ -1884,6 +1885,7 @@ components:
send-ui-element-message: '#/components/schemas/SendUIElementMessage'
sql-table-list-preview: '#/components/schemas/SQLTableListPreview'
sql-table-preview: '#/components/schemas/SQLTablePreview'
+ startup-logs: '#/components/schemas/StartupLogs'
update-cell-codes: '#/components/schemas/UpdateCellCodes'
update-cell-ids: '#/components/schemas/UpdateCellIdsRequest'
variable-values: '#/components/schemas/VariableValues'
@@ -3042,6 +3044,24 @@ components:
required: []
title: SnippetsConfig
type: object
+ StartupLogs:
+ properties:
+ content:
+ type: string
+ op:
+ enum:
+ - startup-logs
+ status:
+ enum:
+ - append
+ - done
+ - start
+ required:
+ - op
+ - content
+ - status
+ title: StartupLogs
+ type: object
StdinRequest:
properties:
text:
diff --git a/packages/openapi/src/api.ts b/packages/openapi/src/api.ts
index 909f08eb37..e51668f98e 100644
--- a/packages/openapi/src/api.ts
+++ b/packages/openapi/src/api.ts
@@ -3573,6 +3573,7 @@ export interface components {
| components["schemas"]["Banner"]
| components["schemas"]["MissingPackageAlert"]
| components["schemas"]["InstallingPackageAlert"]
+ | components["schemas"]["StartupLogs"]
| components["schemas"]["Variables"]
| components["schemas"]["VariableValues"]
| components["schemas"]["QueryParamsSet"]
@@ -4198,6 +4199,14 @@ export interface components {
custom_paths?: string[];
include_default_snippets?: boolean;
};
+ /** StartupLogs */
+ StartupLogs: {
+ content: string;
+ /** @enum {unknown} */
+ op: "startup-logs";
+ /** @enum {unknown} */
+ status: "append" | "done" | "start";
+ };
/** StdinRequest */
StdinRequest: {
text: string;
diff --git a/tests/_cli/test_parse_args.py b/tests/_cli/test_parse_args.py
index 6275894aaf..a24c6461c3 100644
--- a/tests/_cli/test_parse_args.py
+++ b/tests/_cli/test_parse_args.py
@@ -75,3 +75,19 @@ def test_lists():
"verbose": True,
}
assert parse_args(args) == expected
+
+
+def test_parse_args_with_server_startup_command():
+ args = ("--server-startup-command=echo 'hello world'",)
+ expected = {"server-startup-command": "echo 'hello world'"}
+ assert parse_args(args) == expected
+
+
+def test_parse_args_with_complex_startup_command():
+ args = (
+ '--server-startup-command=python -c \'import time; print("Starting"); time.sleep(1); print("Done")\'',
+ )
+ expected = {
+ "server-startup-command": 'python -c \'import time; print("Starting"); time.sleep(1); print("Done")\''
+ }
+ assert parse_args(args) == expected
diff --git a/tests/_messaging/test_ops.py b/tests/_messaging/test_ops.py
index 30ec70e704..0a95f3507e 100644
--- a/tests/_messaging/test_ops.py
+++ b/tests/_messaging/test_ops.py
@@ -4,7 +4,7 @@ from __future__ import annotations
from unittest.mock import MagicMock
from marimo._ast.toplevel import HINT_UNPARSABLE, TopLevelStatus
-from marimo._messaging.ops import CellOp, VariableValue
+from marimo._messaging.ops import CellOp, StartupLogs, VariableValue
from marimo._output.hypertext import Html
from marimo._plugins.ui._impl.input import slider
from marimo._types.ids import CellId_t
@@ -54,3 +54,17 @@ def test_broadcast_serialization() -> None:
cell_op = stream.operations[0]
assert isinstance(parse_raw(cell_op, CellOp), CellOp)
+
+
+def test_startup_logs_creation() -> None:
+ startup_log = StartupLogs(content="Starting up...", status="start")
+ assert startup_log.name == "startup-logs"
+ assert startup_log.content == "Starting up..."
+ assert startup_log.status == "start"
+
+
+def test_startup_logs_all_statuses() -> None:
+ for status in ["start", "append", "done"]:
+ startup_log = StartupLogs(content=f"Test {status}", status=status)
+ assert startup_log.status == status
+ assert startup_log.content == f"Test {status}"
diff --git a/tests/_server/session/test_session_view.py b/tests/_server/session/test_session_view.py
index b6a06b22d8..c58d955c51 100644
--- a/tests/_server/session/test_session_view.py
+++ b/tests/_server/session/test_session_view.py
@@ -14,6 +14,7 @@ from marimo._messaging.ops import (
DataSourceConnection,
DataSourceConnections,
SendUIElementMessage,
+ StartupLogs,
UpdateCellCodes,
UpdateCellIdsRequest,
VariableDeclaration,
@@ -980,3 +981,71 @@ def test_is_empty() -> None:
)
)
assert not session_view.is_empty()
+
+
+def test_session_view_startup_logs() -> None:
+ session_view = SessionView()
+
+ # Test adding a startup log with "start" status
+ start_log = StartupLogs(content="Starting process...", status="start")
+ session_view.add_operation(start_log)
+
+ assert session_view.startup_logs is not None
+ assert session_view.startup_logs.content == "Starting process..."
+ assert session_view.startup_logs.status == "start"
+
+ # Test appending to startup log
+ append_log = StartupLogs(content=" more content", status="append")
+ session_view.add_operation(append_log)
+
+ assert session_view.startup_logs is not None
+ assert (
+ session_view.startup_logs.content == "Starting process... more content"
+ )
+ assert session_view.startup_logs.status == "append"
+
+ # Test marking startup log as done
+ done_log = StartupLogs(content=" done!", status="done")
+ session_view.add_operation(done_log)
+
+ assert session_view.startup_logs is not None
+ assert (
+ session_view.startup_logs.content
+ == "Starting process... more content done!"
+ )
+ assert session_view.startup_logs.status == "done"
+
+
+def test_session_view_startup_logs_operations_exclude_done() -> None:
+ session_view = SessionView()
+
+ # Add startup log in progress
+ start_log = StartupLogs(content="Starting...", status="start")
+ session_view.add_operation(start_log)
+
+ # Should include in operations while in progress
+ operations = session_view.operations
+ startup_ops = [op for op in operations if isinstance(op, StartupLogs)]
+ assert len(startup_ops) == 1
+ assert startup_ops[0].status == "start"
+
+ # Mark as done
+ done_log = StartupLogs(content=" complete", status="done")
+ session_view.add_operation(done_log)
+
+ # Should not include done startup logs in operations
+ operations = session_view.operations
+ startup_ops = [op for op in operations if isinstance(op, StartupLogs)]
+ assert len(startup_ops) == 0
+
+
+def test_session_view_startup_logs_standalone_done() -> None:
+ session_view = SessionView()
+
+ # Add a standalone "done" log without prior start/append
+ done_log = StartupLogs(content="Process complete", status="done")
+ session_view.add_operation(done_log)
+
+ assert session_view.startup_logs is not None
+ assert session_view.startup_logs.content == "Process complete"
+ assert session_view.startup_logs.status == "done"