Skip to content

Commit 7a09196

Browse files
committed
Set the sort order for the listing.
1 parent 426bf33 commit 7a09196

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

src/psc/resources.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55
from dataclasses import dataclass
66
from dataclasses import field
7+
from operator import attrgetter
78
from pathlib import Path
89
from pathlib import PurePath
910
from typing import cast
@@ -16,13 +17,12 @@
1617
from psc.here import HERE
1718
from psc.here import PYODIDE
1819

19-
2020
EXCLUSIONS = ("pyscript.css", "pyscript.js", "favicon.png")
2121

2222

2323
def tag_filter(
24-
tag: Tag,
25-
exclusions: tuple[str, ...] = EXCLUSIONS,
24+
tag: Tag,
25+
exclusions: tuple[str, ...] = EXCLUSIONS,
2626
) -> bool:
2727
"""Filter nodes from example that should not get included."""
2828
attr = "href" if tag.name == "link" else "src"
@@ -155,14 +155,19 @@ class Resources:
155155
pages: dict[PurePath, Page] = field(default_factory=dict)
156156

157157

158+
def get_sorted_examples() -> list[PurePath]:
159+
"""Return an alphabetized listing of the examples."""
160+
examples_dir = HERE / "gallery/examples"
161+
examples = [e for e in examples_dir.iterdir() if e.is_dir()]
162+
return sorted(examples, key=attrgetter("name"))
163+
164+
158165
def get_resources() -> Resources:
159166
"""Factory to construct all the resources in the site."""
160167
resources = Resources()
161168

162169
# Load the examples
163-
examples_dir = HERE / "gallery/examples"
164-
examples = [e for e in examples_dir.iterdir() if e.is_dir()]
165-
for example in examples:
170+
for example in get_sorted_examples():
166171
this_path = PurePath(example.name)
167172
this_example = Example(path=this_path)
168173
resources.examples[this_path] = this_example

tests/test_app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ def test_examples_listing(client_page: PageT) -> None:
5858
first_example = examples_soup.select_one("p.title a")
5959
assert first_example
6060
first_href = first_example.get("href")
61-
assert first_href == "../gallery/examples/hello_world/"
61+
assert first_href == "../gallery/examples/altair/"
6262
hello_soup = client_page(first_href)
6363
assert hello_soup
6464
title = hello_soup.select_one("title")
6565
assert title
66-
assert title.text == "Hello World | PyScript Collective"
66+
assert title.text == "Altair Visualization | PyScript Collective"
6767

6868

6969
def test_static(test_client: TestClient) -> None:

tests/test_resources.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
import pytest
66
from bs4 import BeautifulSoup
77

8-
from psc.resources import Example
8+
from psc.resources import Example, get_sorted_examples
99
from psc.resources import Page
1010
from psc.resources import get_body_content
1111
from psc.resources import get_head_nodes
1212
from psc.resources import get_resources
1313
from psc.resources import is_local
1414
from psc.resources import tag_filter
1515

16-
1716
IS_LOCAL = is_local()
1817

1918

@@ -117,8 +116,8 @@ def test_example() -> None:
117116
this_example = Example(path=PurePath("hello_world"))
118117
assert this_example.title == "Hello World"
119118
assert (
120-
this_example.subtitle
121-
== "The classic hello world, but in Python -- in a browser!"
119+
this_example.subtitle
120+
== "The classic hello world, but in Python -- in a browser!"
122121
)
123122
assert "hello_world.css" in this_example.extra_head
124123
assert "<h1>Hello ...</h1>" in this_example.body
@@ -153,6 +152,12 @@ def test_missing_page() -> None:
153152
assert str(exc.value) == "No page at xxx"
154153

155154

155+
def test_sorted_examples() -> None:
156+
examples = get_sorted_examples()
157+
first_example = examples[0]
158+
assert "altair" == first_example.name
159+
160+
156161
def test_get_resources() -> None:
157162
"""Ensure the dict-of-dicts is generated with PurePath keys."""
158163
resources = get_resources()
@@ -162,8 +167,8 @@ def test_get_resources() -> None:
162167
hello_world = resources.examples[hello_world_path]
163168
assert hello_world.title == "Hello World"
164169
assert (
165-
hello_world.subtitle
166-
== "The classic hello world, but in Python -- in a browser!"
170+
hello_world.subtitle
171+
== "The classic hello world, but in Python -- in a browser!"
167172
)
168173

169174
# Page

0 commit comments

Comments
 (0)