Files
longhikes/web/olt/browse.py
T
swaitsandClaude Fable 5.2 51baaa401e
Mirror to forge / mirror (push) Canceled after 0s
Validate / validate (push) Canceled after 0s
Validate / site (push) Canceled after 0s
Validate / deploy (push) Canceled after 0s
Sort every list of trails, and map trails that have no track
Sorting. Every list of trails has a "Sort by" menu: name, length, time
or climb, each in both directions. Where the list is a table, the
Trail, Length, Time and Climb heads sort on click and reverse on a
second click. Lists open by name, A to Z, except the four that are
defined by an order (the longest, under 100 km, a week or less, one to
two weeks). The chosen order is kept in the page address. The pages are
written in their default order, so they are right without JavaScript.

Maps. A trail with no track but a known start and end is drawn as a
dotted straight line between the two with a ring at each; one with a
single known point is drawn as a ring. They follow the filters, the
row highlight and the popup like any track, and that trail's own page
shows a map in place of the empty frame. The legend, the caption and
the popup say the dotted line is not the trail's path. It gives no
download, no GPX status and no elevation.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-14 17:30:12 -07:00

109 lines
3.8 KiB
Python

"""Lists: ready-made selections of trails, each a rule over a trail's attributes (olt/facets.py).
A list is defined once, here, as a test on a trail. No list names a trail by
id: membership follows the records, so a new trail or a new [hike] table moves
trails in and out of lists with no change to this file.
A list is shown (on the home page, on /lists/, and with a page of its own)
only when it is useful with today's trails:
MIN_SIZE at least this many trails (fewer reads as a gap, not a list)
MAX_SHARE at most this share of all trails (more is nearly everything)
Every list has its page at /lists/<key>/, except Routes, which keeps /routes/.
"""
from dataclasses import dataclass
from typing import Callable
from . import common, facets, words
MIN_SIZE = 4
MAX_SHARE = 0.75
LONG_KM = 2000
SHORT_KM = 100
@dataclass(frozen=True)
class List:
key: str
title: str
blurb: str
test: Callable[[dict], bool]
# The order the list opens in, a key of facets.SORTS. Name order unless
# the list is defined by an order (the longest, the shortest, a week).
sort: str = "name"
def _mi(km):
return words.fmt_int(words.km_to_mi(km))
def _band(key):
return lambda r: facets.time_band(r) == key
LISTS = (
List("national-scenic-trails", "National Scenic Trails",
"The long trails the United States Congress has named National Scenic Trails.",
facets.is_national),
List("routes", "Routes", facets.ROUTE_DEFINITION,
lambda r: facets.kind(r) == "route"),
List("longest", "The longest",
f"Trails of {LONG_KM:,} km ({_mi(LONG_KM)} mi) or more.",
lambda r: r["length_km"] >= LONG_KM, sort="length-desc"),
List("under-100-km", f"Under {SHORT_KM} km",
f"Trails shorter than {SHORT_KM} km ({_mi(SHORT_KM)} mi).",
lambda r: r["length_km"] < SHORT_KM, sort="length-asc"),
List("a-week-or-less", "A week or less",
"Trails most people walk in a week or less, as a source states it or as estimated from the length.",
_band("week"), sort="time-asc"),
List("one-to-two-weeks", "One to two weeks",
"Trails that take one to two weeks, as a source states it or as estimated from the length.",
_band("two-weeks"), sort="time-asc"),
List("loops", "Loops", "Trails that finish where they start.",
lambda r: facets.shape(r) == "loop"),
List("with-gpx", "With a GPX to download",
"Trails with a GPX file of the track on their page.",
facets.gpx_here),
List("canada", "In Canada", "Trails in Canada, or crossing into it.",
lambda r: "CA" in common.countries(r)),
List("united-states", "In the United States", "Trails in the United States, or crossing into it.",
lambda r: "US" in common.countries(r)),
List("off-trail", "Cross-country",
"Trails with stretches of walking where there is no trail.",
lambda r: facets.walking(r) in facets.CROSS_COUNTRY),
) + tuple(
List(f"terrain-{t}", label, f"Trails with {t} among their main terrain.",
lambda r, t=t: t in facets.terrain(r))
for t, label in words.TERRAIN.items()
)
def members(lst, records):
return facets.sort_records([r for r in records if lst.test(r)], lst.sort)
def is_shown(n, total):
"""The threshold rule: a list is shown when it holds at least MIN_SIZE
trails and no more than MAX_SHARE of all of them."""
return n >= MIN_SIZE and n <= MAX_SHARE * total
def shown(records):
"""[(list, its trails)] for every list that passes is_shown, in LISTS order."""
out = []
for lst in LISTS:
recs = members(lst, records)
if is_shown(len(recs), len(records)):
out.append((lst, recs))
return out
def by_key(key):
return next(lst for lst in LISTS if lst.key == key)
def path(lst):
return "/routes/" if lst.key == "routes" else f"/lists/{lst.key}/"