"""Tables built from the records: key figures for a set of trails, a collection set side by side (the Triple Crown), and the rankings under /numbers/. Nothing here names a trail. A collection is read from collections.toml; a ranking takes the records that carry the figure and says how many that is. Every figure comes from a record field, and the climb and the high point carry the caveats the records themselves state. """ import collections import statistics from . import browse, common, datafiles, elevation, facets, hike, pagekit, regions, seo, summary, words from .markup import Markup, esc NUMBERS = "/numbers/" RANKING_ROWS = 25 # rows in a ranking table (the page says how many records have the figure) HUB_ROWS = 10 def _places(rec): return ", ".join(words.region_name(c) for c in rec.get("regions", [])) def high_point(rec): """(name, metres) as the record states them; either may be missing.""" h = hike.table(rec) m = h.get("high_point_m") return h.get("high_point", ""), (m if isinstance(m, (int, float)) and not isinstance(m, bool) else None) def high_point_cell(rec): name, m = high_point(rec) if m is None and not name: return "Not on file" if m is None: return name return Markup(f"{esc(name + ', ') if name else ''}{words.elevation(m)}") def climb_cell(rec): m = facets.row_climb_m(rec) return "Not computed" if m is None else words.elevation(m) def time_cell(rec): return facets.time_text(rec) def season_cell(rec): return facets.season_text(rec) or "Not on file" # One figure: (label, function of a record). The order is the order of columns and rows. FIGURES = ( ("Length", lambda r: words.length(r["length_km"], mi=r["derived"]["length_mi"])), ("States and provinces", _places), ("Typical time", time_cell), ("High point", high_point_cell), ("Climb", climb_cell), ("Season", season_cell), ("Publisher", lambda r: r.get("source", {}).get("publisher", "") or "Not on file"), ) def key_figures_table(recs, caption="Key figures"): """One row per trail: length, places, time, high point, climb, season and publisher.""" rows = [[pagekit.trail_link(r)] + [fn(r) for _, fn in FIGURES] for r in recs] return pagekit.table(["Trail"] + [label for label, _ in FIGURES], rows, caption=caption, row_heads=True) def figures_note(recs): """What the figures in key_figures_table mean, with the caveats the records state.""" est = sum(1 for r in recs if facets.time_days(r)[2]) parts = [f"Times that start with “About” are estimated from the length ({est} of these). " "Otherwise a source states the time.", climb_caveat(recs)] return " ".join(p for p in parts if p) def climb_caveat(recs): """The caveat the elevation files give for their climb figures: the most common one among recs.""" limits = collections.Counter( e["source"]["limits"] for e in (elevation.get(r) for r in recs) if e and e.get("source", {}).get("limits")) text = limits.most_common(1)[0][0] if limits else "" return ("Climb: " + text) if text else "" def figures_section(recs, intro=""): return (f'
{intro}
{esc(figures_note(recs))}
' # Collections ---------------------------------------------------------------------- def collection_members(collection, records): by_id = {r["id"]: r for r in records} return [by_id[i] for i in collection.get("members", []) if i in by_id] def collection_path(collection): return f"/{collection['id']}/" def collection_page(collection, records, site): """(title, description, body, recs) for a collection set side by side, or None when a member is missing. The members are set in the order collections.toml gives, with every figure in a row of its own. The page ranks and recommends nothing.""" recs = collection_members(collection, records) if not recs or len(recs) != len(collection["members"]): return None name = collection["name"] abbrs = words.join_names(r.get("short_name") or common.plain_name(r) for r in recs) title = common.fit_title([f"{name} Trails Compared: {abbrs}", f"{name} Trails Compared"], site) total_mi = sum(r["derived"]["length_mi"] for r in recs) total_km = sum(r["length_km"] for r in recs) places = {c for r in recs for c in r.get("regions", [])} lede = (f"{esc(collection['definition'])} Here are the {len(recs)} trails side by side, in the order the " f"definition names them, from what each trail's page says. Their published lengths add up to {words.fmt_int(total_mi)} " f"miles ({words.fmt_int(total_km)} km), across {len(places)} states and provinces.") headers = ["Figure"] + [common.plain_name(r) for r in recs] rows = [[label] + [fn(r) for r in recs] for label, fn in FIGURES] rows.insert(0, ["Full name"] + [pagekit.trail_link(r) for r in recs]) rows.append(["Designated"] + [common.designated_year(r) or "Not on file" for r in recs]) source = (f'The award is described by {esc(collection.get("source_publisher", "its holder"))} at ' f'{esc(collection["source_url"])}. ' f'{esc(figures_note(recs))} Lengths are the published figures. ' f'See the method for how they are checked.
') from . import home body = (pagekit.head(f"{name} trails compared", lede, [("Trails", "/"), ("Lists", "/lists/")]) + 'Where two trails share a figure, they are in name order. ' f'The same figures are on each trail’s page. See the method for what the caveats mean.
' f'A trail that crosses several states is counted in each, so the counts add up to more than {len(records)}.
{pagekit.table(["Place", "Trails", "Routes", "Loops", "With a GPX", "Longest"], place_rows, caption="Trails by state and province", numeric=(1, 2, 3, 4))}Lengths are the published figures, which are check values, not measured from a track. See the method. The longest trails and the shortest have their own lists.