HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Exemple de graphique et tableau</title>
<meta name="description" content="Exemple de graphique et tableau">
<link href="style.css" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row title">
<header class="masthead mb-auto">
<div class="inner">
<h1 class="masthead-brand" id="title"></h1>
</div>
</header>
</div>
<div class="row graph-and-table">
<div class="reps0 col-sm-12">
<div id="reps0">
</div>
</div>
<div class="table-probs col-sm-offset-0 col-sm-12">
</div>
</div>
<div class="col-sm-offset-0 col-sm-3">
<div class="class-sm-offset-1 col-sm-12">
<p></p>
</div>
<div class="class-sm-offset-1 col-sm-12">
<p></p>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.5.0/build/ol.js"></script>
<script src="https://cdn.plot.ly/plotly-2.0.0.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="main.js"></script>
</body>
</html>
html,
body {
height: 100%;
background-color: rgb(255, 255, 255);
}
h1 {
text-align: center;
}
body {
display: -ms-flexbox;
display: -webkit-box;
display: flex;
-ms-flex-pack: center;
-webkit-box-pack: center;
justify-content: center;
color: #fff;
text-shadow: 0 .05rem .1rem rgba(0, 0, 0, .5);
-webkit-print-color-adjust: exact;
}
#reps0 {
display: flex;
}
#table-title {
padding-top: .75rem;
padding-bottom: .75rem;
color: #6c757d;
text-align: center;
caption-side: top;
}
.probs {
border: 1px solid #C0C0C0;
border-collapse: collapse;
padding: 5px;
margin: auto;
}
.probs th {
border: 1px solid #C0C0C0;
padding: 5px;
background: #F0F0F0;
background: #797979;
color: white;
text-align: center;
}
.probs td {
border: 1px solid #C0C0C0;
text-align: center;
padding: 5px;
}
#nan {
background-color: #797979;
}
h1#title.masthead-brand {
text-align: center;
}
div.row.title {
margin: auto;
display: inline;
}
const parser = new DOMParser();
const [coordX, coordY] = [45.472, -73.750]; //Aéroport de Montréal Pierre-Elliott Trudeau International (CYUL)
const layer02 = 'REPS.DIAG.6_PRMM.ERGE0.2'; //Probabilités de précipitations >= 0.2mm
const layer1 = 'REPS.DIAG.6_PRMM.ERGE1'; //Probabilités de précipitations >= 1mm
const layer25 = 'REPS.DIAG.6_PRMM.ERGE2.5'; //Probabilités de précipitations >= 2.5mm
const layer5 = 'REPS.DIAG.6_PRMM.ERGE5'; //Probabilités de précipitations >= 5mm
const layerTemp = 'RDPS.ETA_TT'; //Température
const layerWindDir = 'RDPS.ETA_WD'; //Direction du vent
const layerWindSp = 'RDPS.ETA_WSPD'; //Module du vent
const server = 'https://geo.weather.gc.ca/geomet?service=WMS&version='; //Serveur de GeoMet du SMC
const getCapab = server + '1.3.0&request=GetCapabilities&layers='; //Lien vers le GetCapabilities
let [version, request, info_format] = ['1.3.0', 'GetFeatureInfo', 'application/json']; //Version, request and format for the information
let [minx, miny, maxx, maxy] = [coordX - 0.25, coordY - 0.25, coordX + 0.25, coordY + 0.25]; //bbox around CYVP airport
async function getCapabilities() {
let response = await fetch(getCapab + layer02);
let data = await response.text().then((data) =>
parser
.parseFromString(data, "text/xml")
.getElementsByTagName("Dimension")[0]
.innerHTML.split("/")
);
return [data[0], data[1], data[2]];
}
function prepareRequests(headers, server) {
const interval = headers[0];
let startDate = headers[1];
let dif = headers[2];
let timesteps = [];
timesteps.push(startDate.toISOString().replace('.000', '')); //Remove trailing zeroes for compatibility with time= parameter
let increment = startDate;
for (let i = 0; i < dif; ++i) {
increment.setHours(increment.getHours() + Number(interval)); //Set the hour according to the intervals supported
timesteps.push(increment.toISOString().replace('.000', '')); //Add to timesteps array
}
let url02 = server +
version + '&request=' + request + '&layers=' + layer02 + '&crs=EPSG:4326&bbox=' +
minx + ',' + miny + ',' + maxx + ',' + maxy + '&exceptions=xml&width=10&height=10&INFO_FORMAT=' +
info_format + '&query_layers=' + layer02 + '&x=1&y=1';
let url1 = server +
version + '&request=' + request + '&layers=' + layer1 + '&crs=EPSG:4326&bbox=' +
minx + ',' + miny + ',' + maxx + ',' + maxy + '&exceptions=xml&width=10&height=10&INFO_FORMAT=' +
info_format + '&query_layers=' + layer1 + '&x=1&y=1';
let url25 = server +
version + '&request=' + request + '&layers=' + layer25 + '&crs=EPSG:4326&bbox=' +
minx + ',' + miny + ',' + maxx + ',' + maxy + '&exceptions=xml&width=10&height=10&INFO_FORMAT=' +
info_format + '&query_layers=' + layer25 + '&x=1&y=1';
let url5 = server +
version + '&request=' + request + '&layers=' + layer5 + '&crs=EPSG:4326&bbox=' +
minx + ',' + miny + ',' + maxx + ',' + maxy + '&exceptions=xml&width=10&height=10&INFO_FORMAT=' +
info_format + '&query_layers=' + layer5 + '&x=1&y=1';
let urlTemp = server +
version + '&request=' + request + '&layers=' + layerTemp + '&crs=EPSG:4326&bbox=' +
minx + ',' + miny + ',' + maxx + ',' + maxy + '&exceptions=xml&width=10&height=10&INFO_FORMAT=' +
info_format + '&query_layers=' + layerTemp + '&x=1&y=1';
let urlWd = server +
version + '&request=' + request + '&layers=' + layerWindDir + '&crs=EPSG:4326&bbox=' +
minx + ',' + miny + ',' + maxx + ',' + maxy + '&exceptions=xml&width=10&height=10&INFO_FORMAT=' +
info_format + '&query_layers=' + layerWindDir + '&x=1&y=1';
let urlWspd = server +
version + '&request=' + request + '&layers=' + layerWindSp + '&crs=EPSG:4326&bbox=' +
minx + ',' + miny + ',' + maxx + ',' + maxy + '&exceptions=xml&width=10&height=10&INFO_FORMAT=' +
info_format + '&query_layers=' + layerWindSp + '&x=1&y=1';
let info = [];
info.push(timesteps, url02, url1, url25, url5, urlTemp, urlWd, urlWspd);
return info;
}
function adjustUTC(time) {
let adjustedDate = [];
let dateTime;
for (let i = 0; i < time.length; ++i) {
dateTime = new Date(time[i]);
adjustedDate.push(dateTime);
}
return adjustedDate;
}
async function sendRequests(headers, nb) {
let responses = [];
let datesMod = [];
let url = headers[nb].concat("&time=" + headers[0][0]);
for (let k = 0; k < headers[0].length; ++k) {
datesMod.push(headers[0][k]);
}
for (let i = 0; i < datesMod.length; ++i) {
try {
if (i != 0) {
url = url.replace(/[^=]*$/.exec(url), datesMod[i]);
}
let response = await fetch(url);
let data = await response.json();
responses.push(data.features[0].properties.value);
} catch (e) {
console.log(e);
}
}
return responses;
}
function traceGraph(xaxis, yaxis) {
let date = [];
let tempRounded = [];
let windKm = [];
let lastDate = new Date(xaxis[xaxis.length-1]);
lastDate.setHours(lastDate.getHours() + 6);
lastDate = lastDate.toLocaleString('fr-CA');
lastDate = lastDate.substring(0, lastDate.indexOf('m'));
lastDate = lastDate.replace(' h ', 'h');
for (let i = 0; i < xaxis.length; ++i) {
date[i] = xaxis[i].toLocaleString('fr-CA');
date[i] = date[i].substring(0, date[i].indexOf('m'));
date[i] = date[i].replace(' h ', 'h');
}
date.push(lastDate);
for (let j = 0; j < yaxis[4].length; ++j) {
tempRounded[j] = Number(Math.round(yaxis[4][j]));
}
for (let k = 0; k < yaxis[6].length; ++k) {
if (!isNaN(Math.round(yaxis[6][k]))) {
windKm[k] = Number(Math.round(yaxis[6][k] * 3.6));
}
}
for (let l = 0; l < yaxis.length; ++l) {
yaxis[l].push(0);
}
var trace1 = {
x: date,
y: yaxis[0],
mode: 'lines+markers',
name: '≥ 0.5mm',
line: {
color: 'rgb(144, 224, 239)',
width: 3,
shape: 'hv'
},
fill: 'tozeroy',
};
var trace2 = {
x: date,
y: yaxis[1],
mode: 'lines+markers',
name: '≥ 1.0mm',
line: {
color: 'rgb(72, 202, 228)',
width: 3,
shape: 'hv'
},
fill: 'tozeroy',
}
var trace3 = {
x: date,
y: yaxis[2],
mode: 'lines+markers',
name: '≥ 2.5mm',
line: {
color: 'rgb(0, 150, 199)',
width: 3,
shape: 'hv'
},
fill: 'tozeroy',
}
var trace4 = {
x: date,
y: yaxis[3],
mode: 'lines+markers',
name: '≥ 5.0mm',
line: {
color: 'rgb(2, 62, 138)',
width: 3,
shape: 'hv'
},
fill: 'tozeroy',
}
var layout = {
title: 'Probabilités de précipitations',
automargin: true,
showlegend: true,
showlegend: true,
legend: {
"orientation": "h"
},
xaxis: {
showticklabels: true,
tickangle: 0,
nticks: 3,
tickfont: {
family: 'Old Standard TT, serif',
size: 12,
color: 'black'
},
},
yaxis: {
title: 'Probabilités (%)',
range: [0, 100]
}
};
var data = [trace1, trace2, trace3, trace4 /*trace5, trace6*/];
Plotly.newPlot('reps0', data, layout);
}
getCapabilities().then(function(response) {
let startTime = response[0]; //Initialise la date de début
let endTime = response[1]; //Initialise la date de fin
let interval = response[2].match(/\d+/)[0]; //RegEx pour avoir le nombre d'heures d'intervalles: Par exemple: PT6H -> 6
startDate = new Date(startTime); //Objet Date pour la date de début
endDate = new Date(endTime); //Objet Date pour la date de fin
var dif = (Math.abs(endDate - startDate) / 36e5) / interval; //Différence entre la date de début et celle de fin selon l'intervalle
let headers = [];
headers.push(interval, startDate, dif); //Pousse les informations nécéssaires pour la préparation des requêtes
headers = prepareRequests(headers, server); //Prépare les requêtes. Headers[0] contient les dates et le reste du tableau, les URLs
let begin = startTime.split('T')[0]; //Titre de la page
document.getElementById("title").innerHTML = "Prévisions CYUL du " + begin; //Titre du DOM
let adjustedUTC = adjustUTC(headers[0]); //Ajuste les heures en fonctions du fuseau horaire de l'utilisateur
let promises = []; //Tableau pour les Promises
/*
* La boucle for envoie de manière asynchrone toutes les requêtes contenues dans "headers"
* Comme nous avons besoin du résultat de toutes les requêtes avant de tracer le graphique et remplir le tableau,
* l'utilisation du Promise.all est élémentaire
*/
for (let i = 1; i < headers.length; ++i) {
promises.push(sendRequests(headers, i)); //Pousse dans la tableau des Promise, la promesse du résultat de la requête
}
Promise.all(promises)
.then((results) => {
traceGraph(adjustedUTC, results); //Trace the graph
createTable(adjustedUTC, results); //Populate table
}).catch(err => { //Catch failed fetch
console.log(err);
});
}).catch(err => {
console.log(err);
});
Also see: Tab Triggers