Files
longhikes/web/olt/browse.py
T
swaitsandClaude Fable 5.2 19fabadd49
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
Put miles and feet first, and give the site a proper mark
Units. Miles and feet now come first everywhere unless the reader
chooses kilometres and metres: paired figures, the time estimate, the
elevation profile, page descriptions and the map scale. The length
filter and the length scale use round mile steps (round kilometre steps
for a reader who chose them). The lists defined by length are now
"Under 100 miles" and "The longest" at 1,000 miles or more; the old
/lists/under-100-km/ address redirects. The records stay metric.

Branding. The tab icon was a bare red blaze that read as a red block;
it is now a mountain with a trail up it, with PNG sizes for browsers
and phones that need them. The home page opens with the mark, the name
and what the site offers. Link previews get a real 1200x630 image:
the old one was an SVG, which Reddit, Slack and iMessage do not draw.

The decorative contour lines now stay inside the content column and
thin out to nothing on every side, where they used to run to the right
edge of a wide window and stop dead at the bottom of the band.

Tests updated for the new trail counts.

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

113 lines
4.0 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
# The two lists defined by length are set in miles; membership compares the
# record's kilometres with the mile figure converted once, here.
LONG_MI = 1000
SHORT_MI = 100
LONG_KM = LONG_MI * words.KM_PER_MI
SHORT_KM = SHORT_MI * words.KM_PER_MI
@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 _km(mi):
return words.fmt_int(mi * words.KM_PER_MI)
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_MI:,} miles ({_km(LONG_MI)} km) or more.",
lambda r: r["length_km"] >= LONG_KM, sort="length-desc"),
List("under-100-miles", f"Under {SHORT_MI} miles",
f"Trails shorter than {SHORT_MI} miles ({_km(SHORT_MI)} km).",
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}/"