Published
Edited
Jan 3, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
g = py`
g = ${rdflib}.Graph()
g.parse(data=${ttl_text}, format="ttl") ;
g`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
sparqlquery = py`
g = ${g}
out = []
for rows in g.query(${sparql}):
out.append(rows.asdict())
out
`
Insert cell
Insert cell
foaf_demo = py`
out = []
g = ${g}
for row in g.query(${sparql}):
person = row["person"].n3(g.namespace_manager)
surname = eval(row["surname"].n3())
email = row["email"].n3()
out.append(dict({"person": person, "surname": surname,"email": email}))
out
`
Insert cell
Insert cell
Insert cell
kg = py`
namespaces = {
"nom": "http://example.org/#",
"wtm": "http://purl.org/heals/food/",
"ind": "http://purl.org/heals/ingredient/",
"skos": "http://www.w3.org/2004/02/skos/core#",
}

kg = ${kglab}.KnowledgeGraph(
name = "A recipe KG example based on Food.com",
base_uri = "https://www.food.com/recipe/",
namespaces = namespaces,
)
import io

with io.open('ex2_0_tmp.ttl','w',encoding='utf8') as f:
f.write(${await FileAttachment("ex2_0_tmp.ttl").text()})
kg.load_rdf("ex2_0_tmp.ttl")
kg`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
pyVisFrame(await py`
sparql = ${sparql2}
pyvis_graph = ${kg}.visualize_query(sparql, notebook=True)

pyvis_graph.force_atlas_2based()
pyvis_graph.show("tmp.fig06.html")`)
Insert cell
Insert cell
html`${await py`
${pd}.set_option("max_rows", None)

df = ${kg}.query_as_df(${sparql2})
df.head(20).to_html()
`}`
Insert cell
Insert cell
Insert cell
kg_nom = py`
namespaces = {
"nom": "http://example.org/#",
"wtm": "http://purl.org/heals/food/",
"ind": "http://purl.org/heals/ingredient/",
"skos": "http://www.w3.org/2004/02/skos/core#",
}

kg_nom = ${kglab}.KnowledgeGraph(
name = "A recipe KG example based on Food.com",
base_uri = "https://www.food.com/recipe/",
namespaces = namespaces,
)
import io

with io.open('tmp.ttl','w',encoding='utf8') as f:
f.write(${await FileAttachment("ex2_0_tmp.ttl").text()})
kg_nom.load_rdf("tmp.ttl")

with io.open('nom.ttl','w',encoding='utf8') as f:
f.write(${await kgzip.file("kglab-main/dat/nom.ttl").text()})
kg_nom.load_rdf("nom.ttl")
kg_nom`
Insert cell
Insert cell
sparql3 = `SELECT ?recipe ?definition
WHERE {
?recipe rdf:type wtm:Recipe .
?recipe skos:definition ?definition .
?recipe wtm:hasIngredient ind:ChickenEgg .
?recipe wtm:hasIngredient ind:AllPurposeFlour .
?recipe wtm:hasIngredient ind:Salt
}
`
Insert cell
Insert cell
tally_print = py`
tally = []
kg = ${kg_nom}
prints = []
for row in kg.query(${sparql3}):
url = row.recipe
recipe_name = row.definition
# collect two flags to evaluate our noodle vs. pancake rules
has_butter = (url, kg.get_ns("wtm").hasIngredient, kg.get_ns("ind").Butter) in kg.rdf_graph()
sez_noodle = any([x in recipe_name for x in ["noodle", "spaetzle", "dumpling", "pasta"]])
tally.append(( has_butter, sez_noodle ))

if not has_butter and not sez_noodle:
prints.append(dict({"url": url, "recipe_name": recipe_name, "has_butter": has_butter, "sez_noodle": sez_noodle }))

{"tally": tally, "prints": prints}

`
Insert cell
Insert cell
contingency_table = py`
# ${packagesPy}
from collections import Counter

plt = ${plt}

#convert the interior 2-element lists coming from javascript back into tuples:
tally_tuples = [ tuple(x) for x in ${tally_print.get("tally")}]

# warning: \`mosaic\` expects data in (x_axis, y_axis), so revert
# the axis so that this contingency table has the same order
contingency_table = dict(Counter(tally_tuples))

# pyodide failed to convert the contingency_table toJs() at this step so could not split out textual and plot into 2 ObservableHQ cells

from statsmodels.graphics.mosaicplot import mosaic

fig, ax = plt.subplots(1, 1)

fig, _ = mosaic(
contingency_table,
index=[1, 0],
gap=0.05,
title="recipes",
# improvde default cmap
properties=lambda key: {"color": "yellow" if key[1] == "True" else "gray"},
ax=ax,
)

ax.set_xlabel("sez_noddle")
ax.set_ylabel("has_butter")
ax.set_box_aspect(1)
(str(contingency_table), plt.show())
`
Insert cell
contingency_table[1]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
kg_annotated_nom = py`
kg = ${kg_nom}
rdflib = ${rdflib}
for id in ${noodle_ids}:
url = f"https://www.food.com/recipe/{id}"
node = rdflib.URIRef(url)
kg.add(node, kg.get_ns("rdf").type, kg.get_ns("nom").Noodle)

for id in ${pancake_ids}:
url = f"https://www.food.com/recipe/{id}"
node = rdflib.URIRef(url)
kg.add(node, kg.get_ns("rdf").type, kg.get_ns("nom").Pancake)

kg.save_rdf("tmp.ttl")
kg`
Insert cell
Insert cell
{
// be sure to run the dependency first
kg_annotated_nom;

return downloadPyoTTLFile("tmp.ttl");
}
Insert cell
Insert cell
//convert from the UTF-8 arrayBuffer that was saved into something we can see in the browser using the one line function above ab2str
{
// be sure to run the dependency first
kg_annotated_nom;
const ttlArrayBuffer = await py`
read = ""
with open("tmp.ttl", "rb") as f:
read = f.read()
read
`;
return ab2str(ttlArrayBuffer);
}
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
kglab = py` # ${packagesPy}

#make completely sure pyvis is loaded first
import micropip
micropip.install("https://thadk.net/pyodide_packages/pyodide_18_1/pyvis-0.1.9-py2.py3-none-any.whl")

import kglab
# ${otherKglabDependencies} ${micropipDependenciesPy} ${kglabDependency}
kglab
`
Insert cell
rdflib = py` # ${otherKglabDependencies}${micropipDependenciesPy}
import rdflib
rdflib
`
Insert cell
pd = py` # ${packagesPy}
import pandas as pd
pd
`
Insert cell
py`dir(${kglab})`
Insert cell
py`repr(${kglab})`
Insert cell
Insert cell
/* Patch plt.show() to work in Observable from https://observablehq.com/@gnestor/pyodide-demo */
plt = py`
from matplotlib import pyplot as plt
import types
import io
import base64
from js import document

def show(self):
buf = io.BytesIO()
self.savefig(buf, format='png')
buf.seek(0)
img_str = 'data:image/png;base64,' + base64.b64encode(buf.read()).decode('UTF-8')
el = document.createElement('img')
el.src = img_str
return el

plt._show = types.MethodType(plt.show, plt)
plt.show = types.MethodType(show, plt)

plt`
Insert cell
Insert cell
Insert cell
Insert cell
Yasgui = require("@triply/yasgui@4.2.20/build/yasgui.min.js")
Insert cell
yasgui_css = html`<link href="https://unpkg.com/@triply/yasgui/build/yasgui.min.css" rel="stylesheet" type="text/css" />`
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more