-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathons.html
More file actions
475 lines (422 loc) · 17.6 KB
/
ons.html
File metadata and controls
475 lines (422 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ONS API Converter | Economics Observatory</title>
<style>
/* ECO colors from styleguide */
:root {
--eco-dot: #F4134D;
--eco-bg: #122B39;
--eco-gray: #676A86;
--eco-turquoise: #36B7B4;
}
body {
margin: 0;
padding: 40px;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
background-color: var(--eco-bg);
font-family: 'Work Sans', sans-serif;
color: white;
box-sizing: border-box;
}
* {
box-sizing: border-box;
}
.container {
max-width: 1000px;
width: 100%;
margin: 20px auto;
text-align: center;
}
.logo {
margin-bottom: 20px;
}
.input-box {
background: rgba(255, 255, 255, 0.1);
padding: 30px;
border-radius: 8px;
margin: 20px 0;
}
input {
width: 100%;
max-width: 600px;
padding: 12px;
margin: 10px 0;
border: 2px solid var(--eco-turquoise);
border-radius: 4px;
background: transparent;
color: white;
font-size: 16px;
}
input:focus {
outline: none;
border-color: var(--eco-dot);
}
.result {
margin-top: 20px;
padding: 20px;
background: rgba(255, 255, 255, 0.05);
border-radius: 4px;
display: none;
}
.title {
color: white;
margin-bottom: 10px;
font-weight: bold;
font-size: 1.1em;
}
.api-url {
color: var(--eco-turquoise);
word-break: break-all;
background: rgba(0, 0, 0, 0.2);
padding: 12px;
border-radius: 4px;
cursor: pointer;
position: relative;
transition: background-color 0.2s;
}
.api-url:hover {
background: rgba(0, 0, 0, 0.3);
}
.api-url::after {
content: 'Click to copy';
position: absolute;
top: -20px;
right: 0;
font-size: 12px;
color: white;
opacity: 0;
transition: opacity 0.2s;
}
.api-url:hover::after {
opacity: 1;
}
.api-url.copied::after {
content: 'Copied!';
color: var(--eco-turquoise);
}
#dataPreview {
max-height: 300px;
overflow-y: auto;
text-align: left;
font-family: monospace;
font-size: 14px;
padding: 10px;
background: rgba(0, 0, 0, 0.2);
border-radius: 4px;
margin-top: 10px;
}
.tutorial {
background: rgba(255, 255, 255, 0.1);
padding: 20px;
border-radius: 8px;
margin: 20px 0;
text-align: left;
max-width: 1000px;
width: 100%;
}
.tutorial h3 {
color: var(--eco-turquoise);
margin-bottom: 15px;
}
.tutorial code {
background: rgba(0, 0, 0, 0.2);
padding: 2px 6px;
border-radius: 4px;
font-family: monospace;
color: var(--eco-turquoise);
}
.tutorial a {
color: var(--eco-turquoise);
text-decoration: none;
}
.tutorial a:hover {
color: var(--eco-dot);
}
footer {
margin-top: auto;
padding: 20px;
color: var(--eco-gray);
font-size: 14px;
text-align: center;
}
footer a {
color: var(--eco-turquoise);
text-decoration: none;
}
footer a:hover {
color: var(--eco-dot);
}
</style>
</head>
<body>
<div class="container">
<div class="logo">
<img src="https://raw.githubusercontent.com/EconomicsObservatory/ECOvisualisations/main/guidelines/logos/eco-logo-dark.png"
alt="Economics Observatory"
height="80"
style="cursor: pointer"
onclick="window.location.href = window.location.pathname">
</div>
<div class="input-box">
<h2>ONS API Converter</h2>
<input type="text"
id="input"
placeholder="Enter CDID code or legacy URL (e.g. d7g7 or full URL)"
autocomplete="off">
<div class="result" id="result">
<div class="title" id="datasetTitle"></div>
<div class="api-url" id="newUrl"></div>
<div id="dataPreview"></div>
</div>
</div>
</div>
<div class="tutorial">
<h3>How to use</h3>
<p>This tool converts between the legacy ONS API format and the new beta API format. You can:</p>
<ul>
<li>Enter a 4-letter CDID code, for example:
<ul>
<li><a href="?code=d7g7">d7g7</a> - CPI Annual Rate</li>
<li><a href="?code=l55o">l55o</a> - GDP Quarter on Quarter Growth</li>
<li><a href="?code=mgsx">mgsx</a> - Unemployment Rate</li>
<li><a href="?code=lf24">lf24</a> - Employment Rate</li>
<li><a href="?code=ihyq">ihyq</a> - GDP Chained Volume Measure</li>
</ul>
</li>
<li>Or paste a legacy URL (e.g., <a href="?url=https://api.ons.gov.uk/timeseries/d7g7/dataset/mm23/data">https://api.ons.gov.uk/timeseries/d7g7/dataset/mm23/data</a>)</li>
</ul>
<h3>API Endpoint</h3>
<p>Example API calls:</p>
<ul>
<li>Using CDID: <a href="?code=d7g7&format=json">?code=d7g7&format=json</a></li>
<li>Using URL: <a href="?url=https://api.ons.gov.uk/timeseries/d7g7/dataset/mm23/data&format=json">?url=...&format=json</a></li>
<li>Data only (CDID): <a href="?code=d7g7&format=json&data_only=true">?code=d7g7&format=json&data_only=true</a></li>
<li>Data only (URL): <a href="?url=https://api.ons.gov.uk/timeseries/d7g7/dataset/mm23/data&format=json&data_only=true">?url=...&format=json&data_only=true</a></li>
</ul>
<h3>Documentation</h3>
<p>The tool can be used programmatically with the following endpoints:</p>
<ul>
<li>Web Interface:
<code><a href="?code=d7g7">?code=d7g7</a></code> or <code><a href="?url=https://api.ons.gov.uk/timeseries/d7g7/dataset/mm23/data">?url=https://api.ons.gov.uk/timeseries/d7g7/dataset/mm23/data</a></code>
<br>Returns the HTML interface with results
</li><br>
<li>Full JSON Response:
<code><a href="?code=d7g7&format=json">?code=d7g7&format=json</a></code>
<br>Returns complete response including metadata:
<pre style="background: rgba(0,0,0,0.2); padding: 10px; border-radius: 4px;">
{
"success": true,
"legacy_url": "...",
"new_url": "...",
"title": "...",
"data": { ... }
}</pre>
</li><br>
<li>Data-Only Response:
<code><a href="?code=d7g7&format=json&data_only=true">?code=d7g7&format=json&data_only=true</a></code>
<br>or
<code><a href="?url=https://api.ons.gov.uk/timeseries/d7g7/dataset/mm23/data&format=json&data_only=true">?url=https://api.ons.gov.uk/timeseries/d7g7/dataset/mm23/data&format=json&data_only=true</a></code>
<br>Returns only the data object from the ONS API
</li>
</ul>
<p>Parameters:</p>
<ul>
<li><code><a href="?code=d7g7">code</a></code>: CDID code (e.g., d7g7)</li>
<li><code><a href="?url=https://api.ons.gov.uk/timeseries/d7g7/dataset/mm23/data">url</a></code>: Legacy ONS API URL (alternative to code)</li>
<li><code><a href="?code=d7g7&format=json">format</a></code>: Set to 'json' for API response</li>
<li><code><a href="?code=d7g7&format=json&data_only=true">data_only</a></code>: Set to 'true' to return only the data object (works with both code and url)</li>
</ul>
</div>
<div class="tutorial">
<h3>Vega Example</h3>
<p>Here's an example of how to create a Vega chart using the ONS API data with our CORS proxy:</p>
<pre style="background: rgba(0,0,0,0.2); padding: 10px; border-radius: 4px; cursor: pointer;" onclick="copyToClipboard(this)">
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"url": "https://eco-cors-proxy.netlify.app/proxy?url=" +
"https://economicsobservatory.github.io/api/ons.html" +
"?code=l55o&format=json&data_only=true",
"format": {
"type": "json",
"property": "months"
}
},
"mark": "line",
"encoding": {
"x": {"field": "date", "type": "temporal"},
"y": {"field": "value", "type": "quantitative"}
}
}</pre>
<h4>Live Example (GDP Quarter on Quarter Growth):</h4>
<div id="vis"></div>
<script src="https://cdn.jsdelivr.net/npm/vega@5"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@5"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-embed@6"></script>
<script>
function copyToClipboard(element) {
// The actual spec to copy (with unbroken URL)
const spec = {
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"url": "https://eco-cors-proxy.netlify.app/proxy?url=https://economicsobservatory.github.io/api/ons.html?code=l55o&format=json&data_only=true",
"format": {
"type": "json",
"property": "months"
}
},
"mark": "line",
"encoding": {
"x": {"field": "date", "type": "temporal"},
"y": {"field": "value", "type": "quantitative"}
}
};
// Copy the stringified spec to clipboard
navigator.clipboard.writeText(JSON.stringify(spec, null, 2))
.then(() => {
// Visual feedback
element.style.backgroundColor = "rgba(54, 183, 180, 0.1)"; // Subtle flash of turquoise
setTimeout(() => {
element.style.backgroundColor = "rgba(0, 0, 0, 0.2)";
}, 200);
});
}
// Render the chart
const chartSpec = {
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"url": "https://eco-cors-proxy.netlify.app/proxy?url=https://economicsobservatory.github.io/api/ons.html?code=l55o&format=json&data_only=true",
"format": {
"type": "json",
"property": "months"
}
},
"mark": {"type": "line", "color": "#36B7B4"},
"encoding": {
"x": {"field": "date", "type": "temporal"},
"y": {"field": "value", "type": "quantitative"}
}
};
vegaEmbed('#vis', chartSpec);
</script>
</div>
<div class="tutorial">
<h3>Limitations</h3>
<ul>
<li>This page is hosted on GitHub Pages, so the API endpoint does not return the correct JSON headers.</li>
<li>However, if you have access to JavaScript you can still parse the JSON data from the HTML response.</li>
<li>This page is hosted on GitHub Pages, so it also does not support native CORS.</li>
<li>You can use <a href="https://economicsobservatory.github.io/cors-proxy/">our CORS Proxy</a> to bypass both of these limitations.</li>
</ul>
</div>
<footer>
<p>Built by <a href="https://www.csaladen.es" target="_blank">Denes</a> for the <a href="https://www.economicsobservatory.com" target="_blank" rel="noopener noreferrer">Economics Observatory</a> |
<a href="https://economicsobservatory.github.io/api/ons.html" target="_blank" rel="noopener noreferrer">ECO ONS API</a> |
<a href="https://economicsobservatory.github.io/cors-proxy/" target="_blank" rel="noopener noreferrer">ECO CORS Proxy</a> |
<a href="https://github.com/EconomicsObservatory" target="_blank" rel="noopener noreferrer">GitHub</a> |
Data from <a href="https://www.ons.gov.uk/" target="_blank" rel="noopener noreferrer">ONS</a></p>
</footer>
<script>
const input = document.getElementById('input');
const result = document.getElementById('result');
const datasetTitle = document.getElementById('datasetTitle');
const newUrl = document.getElementById('newUrl');
const dataPreview = document.getElementById('dataPreview');
async function convertApi(value) {
// Extract CDID from input - updated regex to handle full URLs
const cdid = value.match(/timeseries\/([a-zA-Z0-9]{4})\/dataset/i)?.[1]?.toLowerCase() || value.match(/^[a-zA-Z0-9]{4}$/i)?.[0]?.toLowerCase();
if (!cdid) {
result.style.display = 'block';
datasetTitle.textContent = 'Error';
newUrl.textContent = 'Please enter a valid CDID code or ONS URL';
dataPreview.textContent = '';
return;
}
try {
// First search for the timeseries
const searchUrl = `https://api.beta.ons.gov.uk/v1/search?content_type=timeseries&cdids=${cdid}`;
const searchResponse = await fetch(searchUrl);
const searchData = await searchResponse.json();
if (!searchData.items?.length) {
throw new Error('Timeseries not found');
}
const item = searchData.items[0];
const uri = item.uri;
// Fetch the actual data
const dataUrl = `https://api.beta.ons.gov.uk/v1/data?uri=${uri}`;
const dataResponse = await fetch(dataUrl);
const data = await dataResponse.json();
// Show results
result.style.display = 'block';
datasetTitle.textContent = data.description.title;
newUrl.textContent = dataUrl;
dataPreview.textContent = JSON.stringify(data, null, 2);
// Return API response format
return {
success: true,
legacy_url: `https://api.ons.gov.uk/timeseries/${cdid}/dataset/mm23/data`,
new_url: dataUrl,
title: data.description.title,
data: data
};
} catch (error) {
result.style.display = 'block';
datasetTitle.textContent = 'Error';
newUrl.textContent = error.message;
dataPreview.textContent = '';
return {
success: false,
error: error.message
};
}
}
// Handle input changes with immediate trigger
input.addEventListener('input', (e) => {
convertApi(e.target.value);
});
// Handle API endpoint requests
if (window.location.search) {
const params = new URLSearchParams(window.location.search);
const code = params.get('code');
const url = params.get('url');
if (code || url) {
// Fill in the input box
input.value = code || url;
convertApi(code || url).then(response => {
if (params.get('format') === 'json') {
// Clear existing content
document.body.innerHTML = '';
document.body.style.padding = '0';
// Get the data to return
const jsonData = params.get('data_only') === 'true' ? response.data : response;
// Clear head
document.head.innerHTML = '';
// Write just the JSON in a pre tag
document.write(`<pre>${JSON.stringify(jsonData, null, 2)}</pre>`);
// Close the document
document.close();
}
});
}
}
// Add click-to-copy functionality
newUrl.addEventListener('click', async () => {
try {
await navigator.clipboard.writeText(newUrl.textContent);
newUrl.classList.add('copied');
setTimeout(() => {
newUrl.classList.remove('copied');
}, 2000);
} catch (err) {
console.error('Failed to copy:', err);
}
});
</script>
</body>
</html>