Published
Edited
Jan 3, 2022
1 star
Insert cell
Insert cell
rdflib = py` # depends on ${packagesPy} and ${kglabDependencies} being loaded
import rdflib
rdflib`
Insert cell
URIRef = py`# depends on ${rdflib} being loaded
from rdflib import URIRef
URIRef`
Insert cell
g = py` # ${kglabDependencies} ${kglabpy} ${micropipDependenciesPy}
g = ${rdflib}.Graph()
g`
Insert cell
Insert cell
Insert cell
s = py`${URIRef}("https://www.food.com/recipe/327593")`
Insert cell
Insert cell
Insert cell
p = py`
# depends on ${rdflib} being loaded
from rdflib.namespace import RDF

p = RDF.type
p`
Insert cell
Insert cell
nm = py`${g}.namespace_manager`
Insert cell
py`repr(${nm})`
Insert cell
Insert cell
ns_wtm = py`uri = "http://purl.org/heals/food/"
${rdflib}.Namespace(uri)`
Insert cell
o = py`

#the added namespace gets lost when coerced into a string when passed between Javascript and Python so we will repeat this.

uri = "http://purl.org/heals/food/"
ns_wtm = ${rdflib}.Namespace(uri)
prefix = "wtm"
nm = ${nm}.bind(prefix, ns_wtm)

#this is new:
repr(ns_wtm.Recipe)
`
Insert cell
o_namespaces = py`
#the added namespace gets lost when coerced into a string when passed between Javascript and Python so repeat this for now.
uri = "http://purl.org/heals/food/"
ns_wtm = ${rdflib}.Namespace(uri)
prefix = "wtm"
nm = ${nm}.bind(prefix, ns_wtm)

# This is new
dict(${nm}.namespaces())
`
Insert cell
// py` # depends on ${kglabDependencies} ${micropipDependenciesPy}
// ${nm}.compute_qname("http://purl.org/heals/food/")
// `
Insert cell
Insert cell
py`
ns_wtm = ${rdflib}.Namespace( "http://purl.org/heals/food/")
ns_wtm
`
Insert cell
anytime_crepes_spo_1 = py` #depends on ${rdflib} being loaded
# "g" will carry our state between cells but we'll also track
# it per-cell so it is guaranteed to have at
# least run in order.

from rdflib.namespace import FOAF, RDF

uri = "http://purl.org/heals/food/"
ns_wtm = ${rdflib}.Namespace(uri)
prefix = "wtm"
nm = ${nm}.bind(prefix, ns_wtm)
o = ns_wtm.Recipe

g= ${g}.add((${URIRef}(${s}), RDF.type, o,))
repr(g)
`
Insert cell
Insert cell
anytime_crepes_spo_2 = py` #depends on ${anytime_crepes_spo_1} completion
from rdflib.namespace import XSD

s = ${rdflib}.URIRef("https://www.food.com/recipe/327593")

#the added namespace gets lost when coerced into a string when passed between Javascript and Python so repeat this for now.
uri = "http://purl.org/heals/food/"
ns_wtm = ${rdflib}.Namespace(uri)
prefix = "wtm"
nm = ${nm}.bind(prefix, ns_wtm)

p = ns_wtm.hasCookTime

o = ${rdflib}.Literal("8", datatype=XSD.integer)
g = ${g}.add((s, p, o,))
repr(g)
`
Insert cell
Insert cell
py` #depends on ${anytime_crepes_spo_2} completion

#the added namespace gets lost when coerced into a string when passed between Javascript and Python so repeat this for now.
uri = "http://purl.org/heals/ingredient/"
ns_ind = ${rdflib}.Namespace(uri)

prefix = "ind"
${nm}.bind(prefix, ns_ind)
dict(${nm}.namespaces())
`
Insert cell
o_chickenEgg = py`

#the added namespace gets lost when coerced into a string when passed between Javascript and Python so repeat this for now.
uri = "http://purl.org/heals/ingredient/"
ns_ind = ${rdflib}.Namespace(uri)

prefix = "ind"
${nm}.bind(prefix, ns_ind)

#these are the new things:
o = ns_ind.ChickenEgg
o`
Insert cell
anytime_crepes_spo_ingredients = py` #depends on ${anytime_crepes_spo_2}

# this part is duplicated in this cell from above
s = ${URIRef}("https://www.food.com/recipe/327593")

uri = "http://purl.org/heals/food/"
ns_wtm = ${rdflib}.Namespace(uri)
p = ns_wtm.hasIngredient

# this part is duplicated in this cell from above
uri = "http://purl.org/heals/ingredient/"
ns_ind = ${rdflib}.Namespace(uri)

o = ns_ind.ChickenEgg

# these are the new things:
${g}.add((s, p, o,))
${g}.add((s, p, ns_ind.CowMilk,))
g = ${g}.add((s, p, ns_ind.WholeWheatFlour,))
repr(g)`
Insert cell
Insert cell
anytime_crepes_spo_check = py` #depends on ${anytime_crepes_spo_ingredients}
output = []
for s, p, o in ${g}:
output.append({"subject": s, "predicate": p, "object": o})
output
`
Insert cell
Insert cell
py` #depends on ${anytime_crepes_spo_check}
s = ${g}.serialize(format="ttl")
s`
Insert cell
Insert cell
serialize_g = py` #depends on ${anytime_crepes_spo_check}
import os
${g}.serialize(destination="tmp.ttl", format="ttl", encoding="utf-8")
os.listdir("/")
`
Insert cell
Insert cell
Insert cell
ttlArrayBuffer = py` #depends on ${serialize_g}
read = ""
with open("tmp.ttl", "rb") as f:
read = f.read()
read
`
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
ab2str(ttlArrayBuffer)
Insert cell
Insert cell
jsonLdArrayBuffer = py` #depends on ${anytime_crepes_spo_check}
import os

data = ${g}.serialize(
format="json-ld",
indent=2,
encoding="utf-8",
)

with open("tmp.jsonld", "wb") as f:
f.write(data)
# os.listdir("/")
data
`
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
ab2str(jsonLdArrayBuffer)
Insert cell
Insert cell
context = py`
{
"@language": "en",
"wtm": "http://purl.org/heals/food/",
"ind": "http://purl.org/heals/ingredient/",
}
`
Insert cell
Insert cell
jsonLdArrayBufferWContext = py` #depends on ${anytime_crepes_spo_check}
import os

data = ${g}.serialize(
format="json-ld",
indent=2,
context=${context},
encoding="utf-8",
)

with open("tmp.jsonld", "wb") as f:
f.write(data)
# os.listdir("/")
data
`
Insert cell
ab2str(jsonLdArrayBufferWContext)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
micropipPyvis = py`# make completely sure pyvis got included
import micropip
micropip.install("https://thadk.net/pyodide_packages/pyodide_18_1/pyvis-0.1.9-py2.py3-none-any.whl")`
Insert cell
kglab = py`
# depends on ${micropipPyvis}
import kglab
# ${kglabDependencies} ${kglabpy} ${micropipDependenciesPy} ${packagesPy}
kglab
`
Insert cell
py`dir(${kglab})`
Insert cell
py`repr(${kglab})`
Insert cell
kgzip = FileAttachment("kglab-main.zip").zip()
Insert cell
import { py, pyodide } from "@thadk/pyodide-18"
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