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 active connection enpdoint (#3170)

Myles Scolnick34937902bb6a
diff --git a/marimo/_plugins/stateless/mpl/_mpl.py b/marimo/_plugins/stateless/mpl/_mpl.py
index f90f86470d..2228a9b033 100644
--- a/marimo/_plugins/stateless/mpl/_mpl.py
+++ b/marimo/_plugins/stateless/mpl/_mpl.py
@@ -34,7 +34,7 @@ from marimo._utils.signals import get_signals
if TYPE_CHECKING:
from matplotlib.axes import Axes
from matplotlib.backends.backend_webagg_core import FigureManagerWebAgg
- from matplotlib.figure import Figure
+ from matplotlib.figure import Figure, SubFigure
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.websockets import WebSocket
@@ -302,7 +302,7 @@ def get_or_create_application(
def new_figure_manager_given_figure(
- num: int, figure: Union[Figure, Axes]
+ num: int, figure: Union[Figure, SubFigure, Axes]
) -> Any:
from matplotlib.backends.backend_webagg_core import (
FigureCanvasWebAggCore,
@@ -322,7 +322,7 @@ def new_figure_manager_given_figure(
@mddoc
-def interactive(figure: Union[Figure, Axes]) -> Html:
+def interactive(figure: Union[Figure, SubFigure, Axes]) -> Html:
"""Render a matplotlib figure using an interactive viewer.
The interactive viewer allows you to pan, zoom, and see plot coordinates
diff --git a/marimo/_server/api/endpoints/health.py b/marimo/_server/api/endpoints/health.py
index 3f64aef455..712876d2c7 100644
--- a/marimo/_server/api/endpoints/health.py
+++ b/marimo/_server/api/endpoints/health.py
@@ -211,3 +211,23 @@ async def usage(request: Request) -> JSONResponse:
},
}
)
+
+
[email protected]("/api/status/connections")
+async def connections(request: Request) -> JSONResponse:
+ """
+ responses:
+ 200:
+ description: Get the number of active websocket connections
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ active:
+ type: integer
+ """
+ app_state = AppState(request)
+ return JSONResponse(
+ {"active": app_state.session_manager.get_active_connection_count()}
+ )
diff --git a/marimo/_server/sessions.py b/marimo/_server/sessions.py
index e777948e96..c315cc7eb0 100644
--- a/marimo/_server/sessions.py
+++ b/marimo/_server/sessions.py
@@ -907,6 +907,15 @@ class SessionManager:
self.watcher.start()
return Disposable(self.watcher.stop)
+ def get_active_connection_count(self) -> int:
+ return len(
+ [
+ session
+ for session in self.sessions.values()
+ if session.connection_state() == ConnectionState.OPEN
+ ]
+ )
+
class LspServer:
def __init__(self, port: int) -> None:
diff --git a/openapi/api.yaml b/openapi/api.yaml
index f26d70ad71..1de7bc3146 100644
--- a/openapi/api.yaml
+++ b/openapi/api.yaml
@@ -1953,7 +1953,7 @@ components:
type: object
info:
title: marimo API
- version: 0.9.34
+ version: 0.10.2
openapi: 3.1.0
paths:
/@file/{filename_and_length}:
@@ -2627,6 +2627,18 @@ paths:
type: string
type: object
description: Get the status of the application
+ /api/status/connections:
+ get:
+ responses:
+ 200:
+ content:
+ application/json:
+ schema:
+ properties:
+ active:
+ type: integer
+ type: object
+ description: Get the number of active websocket connections
/api/usage:
get:
responses:
diff --git a/openapi/src/api.ts b/openapi/src/api.ts
index f0a3d2ac45..d097f59267 100644
--- a/openapi/src/api.ts
+++ b/openapi/src/api.ts
@@ -1900,6 +1900,43 @@ export interface paths {
patch?: never;
trace?: never;
};
+ "/api/status/connections": {
+ parameters: {
+ query?: never;
+ header?: never;
+ path?: never;
+ cookie?: never;
+ };
+ get: {
+ parameters: {
+ query?: never;
+ header?: never;
+ path?: never;
+ cookie?: never;
+ };
+ requestBody?: never;
+ responses: {
+ /** @description Get the number of active websocket connections */
+ 200: {
+ headers: {
+ [name: string]: unknown;
+ };
+ content: {
+ "application/json": {
+ active?: number;
+ };
+ };
+ };
+ };
+ };
+ put?: never;
+ post?: never;
+ delete?: never;
+ options?: never;
+ head?: never;
+ patch?: never;
+ trace?: never;
+ };
"/api/usage": {
parameters: {
query?: never;
diff --git a/tests/_server/api/endpoints/test_health.py b/tests/_server/api/endpoints/test_health.py
index 4dde5c690e..7469ca0c3c 100644
--- a/tests/_server/api/endpoints/test_health.py
+++ b/tests/_server/api/endpoints/test_health.py
@@ -4,11 +4,17 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from marimo import __version__
-from tests._server.mocks import token_header
+from tests._server.mocks import token_header, with_session
if TYPE_CHECKING:
from starlette.testclient import TestClient
+SESSION_ID = "session-123"
+HEADERS = {
+ "Marimo-Session-Id": SESSION_ID,
+ **token_header("fake-token"),
+}
+
def test_health(client: TestClient) -> None:
response = client.get("/health")
@@ -61,3 +67,16 @@ def test_memory(client: TestClient) -> None:
# None, no active session
computer = response.json()["kernel"]
assert computer["memory"] is None
+
+
+def test_connections(client: TestClient) -> None:
+ response = client.get("/api/status/connections")
+ assert response.status_code == 200
+ assert response.json()["active"] == 0
+
+
+@with_session(SESSION_ID)
+def test_read_code(client: TestClient) -> None:
+ response = client.get("/api/status/connections", headers=HEADERS)
+ assert response.status_code == 200, response.text
+ assert response.json()["active"] == 1