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.

fix: snippet endpoint (#1098)

Akshay Agrawal02f39e6db122
diff --git a/marimo/_server/api/endpoints/documentation.py b/marimo/_server/api/endpoints/documentation.py
index 8b30431dc3..e9fab2a6a8 100644
--- a/marimo/_server/api/endpoints/documentation.py
+++ b/marimo/_server/api/endpoints/documentation.py
@@ -1,8 +1,6 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
-from functools import lru_cache
-
from starlette.requests import Request
from marimo import _loggers
@@ -15,10 +13,7 @@ LOGGER = _loggers.marimo_logger()
router = APIRouter()
-# Cache the snippets
-@lru_cache(1)
-async def _read_snippets_once() -> Snippets:
- return await read_snippets()
+_SNIPPETS: list[Snippets] = []
@router.get("/snippets")
@@ -26,4 +21,6 @@ async def load_snippets(
request: Request,
) -> Snippets:
del request
- return await _read_snippets_once()
+ if not _SNIPPETS:
+ _SNIPPETS.append(await read_snippets())
+ return _SNIPPETS[0]
diff --git a/marimo/_snippets/snippets.py b/marimo/_snippets/snippets.py
index 1e90a865f3..67782b0e8d 100644
--- a/marimo/_snippets/snippets.py
+++ b/marimo/_snippets/snippets.py
@@ -61,7 +61,9 @@ async def read_snippets() -> Snippets:
snippets.append(Snippet(title=title, sections=sections))
- return Snippets(snippets=snippets)
+ return Snippets(
+ snippets=sorted(snippets, key=lambda snippet: snippet.title)
+ )
def should_ignore_code(code: str) -> bool:
diff --git a/tests/_server/api/endpoints/test_documentation.py b/tests/_server/api/endpoints/test_documentation.py
index f40c0d1323..3d8343402e 100644
--- a/tests/_server/api/endpoints/test_documentation.py
+++ b/tests/_server/api/endpoints/test_documentation.py
@@ -8,3 +8,13 @@ def test_snippets(client: TestClient) -> None:
content = response.json()
assert content["snippets"] is not None
assert len(content["snippets"]) > 0
+ snippets = content["snippets"]
+
+ # call the endpoint a second time and make sure the
+ # same snippets are retrieved
+ response = client.get("/api/documentation/snippets")
+ assert response.status_code == 200, response.text
+ content = response.json()
+ assert content["snippets"] is not None
+ assert len(content["snippets"]) > 0
+ assert content["snippets"] == snippets