Skip to content

Commit

Permalink
fix: snippet endpoint (#1098)
Browse files Browse the repository at this point in the history
  • Loading branch information
akshayka authored Apr 9, 2024
1 parent e9312ae commit 02f39e6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
11 changes: 4 additions & 7 deletions marimo/_server/api/endpoints/documentation.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,15 +13,14 @@
router = APIRouter()


# Cache the snippets
@lru_cache(1)
async def _read_snippets_once() -> Snippets:
return await read_snippets()
_SNIPPETS: list[Snippets] = []


@router.get("/snippets")
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]
4 changes: 3 additions & 1 deletion marimo/_snippets/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions tests/_server/api/endpoints/test_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 02f39e6

Please sign in to comment.