Files
f1-circuits/examples/grand-prix.html
T
jschoubben b6e6e9d565 Add an example site that calls every route, served at /examples/
A reader learns the API fastest from working calls: one page per area
(circuit browser, track images, GPS traces on a map, grand prix lookup,
posters, Kometa) shows the exact request URL next to its answer. Pages
call their own origin when the app serves them, and the public service
when opened from disk or given ?api=. Posters render only on a button
press, since each one fetches map tiles on the server. A body that does
not match the asked format is said in red, never shown as success.

The image copies examples/ so the deployed app serves the same pages.

Claude-Session: https://claude.ai/code/session_01NcbgfJAa38xFpzy69BWrqh
2026-11-27 09:48:04 +01:00

105 lines
4.3 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Grand prix lookup · F1 Circuits API examples</title>
<meta name="description" content="Look up the track of any F1 race by its name and season: the API finds the circuit and the layout raced that year.">
<link rel="icon" href="data:,">
<link rel="stylesheet" href="site.css">
</head>
<body>
<header class="site">
<a class="brand" href="index.html">F1 <span>Circuits</span> API</a>
<nav aria-label="Examples">
<a href="index.html">Overview</a>
<a href="circuits.html">Circuit browser</a>
<a href="tracks.html">Track images</a>
<a href="map.html">GPS traces</a>
<a href="grand-prix.html" aria-current="page">Grand prix lookup</a>
<a href="posters.html">Posters</a>
<a href="kometa.html">Kometa</a>
</nav>
<div id="api-base"></div>
</header>
<main>
<h1>Grand prix lookup</h1>
<p class="lead">
Name a race and a season, and the API answers the track it was raced on: it reads that season’s schedule from
Jolpica, matches the race by its exact name, finds the circuit in its catalogue and picks the layout of that year.
</p>
<section class="card" id="races">
<h2>1. The season’s races</h2>
<p>
This page lists the races by calling Jolpica itself, from the browser (Jolpica allows any origin). If it cannot
be reached, type the name: the API needs it exactly as Jolpica’s <code>raceName</code>, such as
<code>British Grand Prix</code>.
</p>
<div class="controls" id="race-picker"></div>
<div class="request"></div>
<div class="status"></div>
</section>
<section class="card" id="lookup">
<h2>2. The race’s track</h2>
<p><code>/grand-prix/{race name}/{season}/{format}</code>, the race name percent-encoded (a space is <code>%20</code>).</p>
<div class="controls"><fieldset id="format"><legend>Format</legend></fieldset></div>
<div id="jolpica-says"></div>
<div class="request"></div>
<div class="status idle">Choose a race.</div>
<div class="result"></div>
</section>
<h2>When the lookup finds nothing</h2>
<p>
The API answers <code>404 {"detail": "Circuit not found"}</code> when the name is not in that season’s schedule,
when Jolpica cannot be reached from the server, or when the circuit or that year’s layout is not in the catalogue
yet (browse it in the <a href="circuits.html">circuit browser</a>).
</p>
</main>
<footer class="pager">
<a rel="prev" href="map.html">← GPS traces</a>
<a class="home" href="index.html">All examples</a>
<a rel="next" href="posters.html">Posters →</a>
</footer>
<script src="site.js"></script>
<script>
(function () {
const { h, api, path, call, showImage, showGeoJSON, racePicker } = F1;
const $ = (id) => document.getElementById(id);
const card = $('lookup');
const FORMATS = ['svg', 'png', 'geojson'];
const initial = FORMATS.includes(F1.params.get('format')) ? F1.params.get('format') : 'svg';
for (const f of FORMATS) {
const input = h('input', { type: 'radio', name: 'format', value: f, checked: f === initial });
input.addEventListener('change', lookup);
$('format').append(h('label', { class: 'inline' }, input, f));
}
let chosen = null;
async function lookup() {
if (!chosen || !chosen.name || !(chosen.season >= 1950)) return;
const f = $('format').querySelector('input:checked').value;
const r = chosen.race;
$('jolpica-says').replaceChildren(r
? h('p', {}, `Jolpica: round ${r.round} on ${r.date}, at ${r.Circuit.circuitName} in ${r.Circuit.Location.locality}, ${r.Circuit.Location.country}.`)
: h('p', {}, 'Jolpica’s entry for this name is not known on this page; the API looks it up itself.'));
const answer = await call(card, api(path`/grand-prix/${chosen.name}/${chosen.season}/${f}`), { waiting: 'Looking up the race…' });
const out = card.querySelector('.result');
if (!answer) { out.replaceChildren(); return; }
const status = card.querySelector('.status');
if (f === 'geojson') await showGeoJSON(out, answer, { status });
else await showImage(out, answer, f, { alt: `${chosen.name} ${chosen.season}`, status });
}
racePicker($('race-picker'), { card: $('races'), onChange: (c) => { chosen = c; lookup(); } });
})();
</script>
</body>
</html>