Public
Edited
Jul 12, 2023
2 forks
6 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
fetch_options = get_fetch_options (/*{
headers: {
Authorization: `Bearer ${API_KEY}`, //37bcb284-333f-37fe-951c-151802198031
'Content-Type': 'text/xml; charset=UTF-8'
}
}*/)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
let idbank = "000067681" // taux de mortalité infantile
const url = "https://api.insee.fr/series/BDM/V1/data/SERIES_BDM/" + idbank

return fetch(url, fetch_options).then( response => response.text() )
}
Insert cell
Insert cell
xml_nodelist = {

let idbank = "000067681" // taux de mortalité infantile
const url = "https://api.insee.fr/series/BDM/V1/data/SERIES_BDM/" + idbank

let xml_string = await fetch(url, fetch_options).then( response => response.text() )
let xml_doc = (new DOMParser).parseFromString(xml_string, "application/xml") // XMLDocument

console.log("Dépliez cet arbre :", xml_doc)
return d3.hierarchy(xml_doc).descendants()
}
Insert cell
Insert cell
serie_data = {
return xml_nodelist
.filter(d => d.data.tagName == "Obs")
.map(d => { return {TIME_PERIOD:d.data.getAttribute("TIME_PERIOD"),
OBS_VALUE: +d.data.getAttribute("OBS_VALUE")}
})
}
Insert cell
Insert cell
viewof serie_dt = {
let a = xml_nodelist
.filter(d => d.data.tagName == "Obs")
.map(d => { return {TIME_PERIOD: d.data.getAttribute("TIME_PERIOD"),
OBS_VALUE: +d.data.getAttribute("OBS_VALUE"),
IDBANK: d.parent.data.getAttribute("IDBANK"), // remontée niveau parent
OBS_STATUS: d.data.getAttribute("OBS_STATUS"),
OBS_QUAL: d.data.getAttribute("OBS_QUAL"),
OBS_TYPE: d.data.getAttribute("OBS_TYPE")
}
})
return Inputs.table(a, {locale: 'fr'})
}
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
let idbanks = ["010536463", "010536465"]
const url = "https://api.insee.fr/series/BDM/V1/data/SERIES_BDM/" + idbanks.join("%2B") // "%2B" équivaut à "+"

let xml_string = await fetch(url, fetch_options).then( response => response.text() )
let xml_doc = (new DOMParser).parseFromString(xml_string, "application/xml")
let xml_nodelist = d3.hierarchy(xml_doc).descendants()
let dt = xml_nodelist
.filter(d => d.data.tagName == "Obs")
.map(d => { return {TIME_PERIOD: d.data.getAttribute("TIME_PERIOD"),
OBS_VALUE: +d.data.getAttribute("OBS_VALUE"),
TITLE: d.parent.data.getAttribute("TITLE_FR")} })
return vl.markLine({interpolate:'cardinal'})
.encode(
vl.x().fieldT('TIME_PERIOD').title(null),
vl.y().fieldQ('OBS_VALUE').title(null).scale({zero: false}),
vl.color().fieldN('TITLE').legend({orient:'top', symbolStrokeWidth: 3, symbolSize: 400,
labelFontSize: 11, title: null, labelLimit: 300}),
vl.tooltip([vl.tooltip().fieldT('TIME_PERIOD').title('période ').format('%Y'),
vl.tooltip().fieldQ('OBS_VALUE').format(',').title('valeur ')])
)
.data(dt)
.title({anchor:'middle', text: "Mon 1er graphique connecté à l'API web BDM",
subtitle: 'Source : Insee BDM' })
.width("container")
.height(250)
.render({renderer: 'svg'})
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
dtf_tb = {
const url = "https://api.insee.fr/series/BDM/V1/dataflow/FR1/all"

let xml_string = await fetch(url, fetch_options ).then( response => response.text() )

let xml_doc = (new DOMParser).parseFromString(xml_string, "application/xml") // XMLDocument
let xml_nodelist = d3.hierarchy(xml_doc).descendants()
let urls = xml_nodelist.filter(d => d.data.tagName == "com:AnnotationURL")
.map(d => ({ url : d.data.innerHTML,
dataset : d.parent.parent.parent.data.getAttribute('id') }) )
let libs = xml_nodelist.filter(d => d.data.tagName == "com:Name" && d.data.getAttribute('xml:lang') == 'fr' )
.map(d => ({ lib : d.data.innerHTML,
dataset : d.parent.data.getAttribute('id') }) )

let series = xml_nodelist.filter(d => d.data.tagName == "com:AnnotationText")
.map(d => ({ series : +d.data.innerHTML.split(':')[1],
dataset : d.parent.parent.parent.data.getAttribute('id') }) )
let structures = xml_nodelist.filter(d => d.data.tagName == "str:Structure")
.map(d => ({ structure : d.children[0].data.getAttribute('id'),
dataset : d.parent.data.getAttribute('id') }) )

return aq.from(urls).join(aq.from(libs))
.join(aq.from(series))
.join(aq.from(structures)) // jointure auto sur le champ commun id
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Chargement d'une codelist et stockage dans codelist_map pour ne pas avoir à la recharger
get_codelist = async function(cl) {
if (!cl) return false
if (codelist_map.has(cl))
return codelist_map.get(cl)
if (codelist_map.get(cl) === "fetching") // récup en cours, pas la peine de déclencher un nouveau chargement
return
codelist_map.set(cl, "fetching")
const url = "https://api.insee.fr/series/BDM/V1/codelist/FR1/" + cl

// gestion du quota de 30 requêtes max par minute pour une clé
// s'il est dépassé, on passe à une autre clé via get_fetch_options()
let i = 0, xml_codelist_bdm

while (i++ <= 5) { // 5 tentatives max
xml_codelist_bdm = await fetch(url, get_fetch_options())
.then( response => {
if (response.status == 429) { return {error: 429, msg: response.text() } } //quota dépassé
return response.text()
})
if (typeof xml_codelist_bdm == 'string') i = 10
}
let xml_tree = (new DOMParser).parseFromString(xml_codelist_bdm, "application/xml")
let nodes = d3.hierarchy(xml_tree)
let a = nodes.descendants().filter(d => d.data.tagName == "str:Code")
.map(d => { return {id:d.data.getAttribute("id"),
lib: d.children.filter(e => e.data.tagName == "com:Name"
&& e.data.getAttribute('xml:lang') == 'fr')[0].data.innerHTML,
c: d.parent.data.getAttribute("id")
}})
let b = nodes.descendants().filter(d => d.data.tagName == "str:Codelist")
.map(d => d.children.filter(e => e.data.tagName == "com:Name"
&& e.data.getAttribute('xml:lang') == 'fr')[0].data.innerHTML)
codelist_lib_map.set(cl, b[0])
let dt = aq.from(a)
codelist_map.set(cl, aq.from(a)) // stockage dans une hashmap centrale sous forme d'une table Arquero
return dt
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
get_fetch_options = function() {
let i = mutable id_token

if (++i == token_info.length)
i = 0
mutable id_token = i
return {
headers: {
Authorization: `Bearer ${token_info[i].access_token}`,
'Content-Type': 'text/xml; charset=UTF-8'
}
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more