Published
Edited
Feb 27, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data_graph = ({
"@context": { "@vocab": "http://schema.org/" },
"@id": "http://example.org/ns#Bob",
"@type": "Person",
givenName: "Robert",
familyName: "Junior",

birthDate: "1971-07-07",
deathDate: "1968-09-10",
address: {
"@id": "http://example.org/ns#BobsAddress",
streetAddress: "1600 Amphitheatre Pkway",
postalCode: 9404
}
})
Insert cell
Insert cell
conforms = py`
pyshacl = ${pyshacl}
shape_graph = ${shapes_graph}
data_graph = ${JSON.stringify(data_graph)}
results = pyshacl.validate(
data_graph,
shacl_graph=shape_graph,
data_graph_format="json-ld",
shacl_graph_format="ttl",
inference="rdfs",
debug=True,
serialize_report_graph="ttl",
)

conforms, report_graph, report_text = results

"conforms", conforms, results, dir(report_graph), repr(report_graph)`
Insert cell
Insert cell
`${conforms[2][2].replace(/\t/g, " ")}`
Insert cell
Insert cell
Insert cell
conforms[2][1]
Insert cell
py`
import io
out= []
report_g = ${rdflib}.Graph()
report_graph = ${conforms[2][1]}.tobytes()
report_g.parse(data=report_graph, format="ttl", encoding="utf-8")
nm = report_g.namespace_manager

for s, p, o in sorted(report_g):
out.append(dict({"subject": s.n3(nm),"predicate": p.n3(nm),"object": o.n3(nm)}))
out
`
Insert cell
Insert cell
Insert cell
kg = py`
from os.path import dirname
import os, io

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,
)
with io.open('recipes.ttl','wb') as f:
f.write(${await kgzip
.file("kglab-main/dat/recipes.ttl")
.arrayBuffer()}.tobytes())

kg.load_rdf("recipes.ttl") ;
kg
`
Insert cell
Insert cell
Insert cell
Insert cell
recipe_conforms = py`
conforms, report_graph, report_text = ${kg}.validate(
shacl_graph=${recipe_shape_graph},
shacl_graph_format="ttl"
)
conforms, report_graph, report_text, dir(report_graph), repr(report_graph)
`
Insert cell
Insert cell
Insert cell
Insert cell
df = py`
${pd}.set_option("max_rows", None)

sparql = """
SELECT ?id ?focus ?path ?value ?constraint ?message
WHERE {
?id rdf:type sh:ValidationResult .
?id sh:focusNode ?focus .
?id sh:resultPath ?path .
?id sh:value ?value .
?id sh:resultMessage ?message .
?id sh:sourceConstraintComponent ?constraint
}
"""

df = ${recipe_conforms[1]}.query_as_df(sparql)
df`
Insert cell
table(
df
.to_dict('records')
.toJs()
.map(Object.fromEntries)
)
Insert cell
// html`${await df.to_html()}`
Insert cell
Insert cell
Insert cell
pyVisFrame(shacl_pyvis)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import { table } from "@gnestor/table"
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
pyshacl = py` # ${packagesPy} ${otherKglabDependencies}
import pyshacl
pyshacl
`
Insert cell
Insert cell
Insert cell
kglab = 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")

import kglab
# ${otherKglabDependencies} ${kglabDependency} ${micropipDependenciesPy} ${packagesPy}
kglab
`
Insert cell
py`dir(${kglab})`
Insert cell
py`repr(${kglab})`
Insert cell
Insert cell
import { pyVisFrame } from "@thadk/ex3_0-interactive-graph-visualization-with-pyvis"
Insert cell
import { py, pyodide, downloadPyoFile } from "@thadk/pyodide-18"
Insert cell
/* https://pyodide.org/en/stable/usage/type-conversions.html */
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