Published
Edited
Jan 3, 2022
1 fork
Insert cell
Insert cell
Insert cell
kg = py`

namespaces = {
"wtm": "http://purl.org/heals/food/",
"ind": "http://purl.org/heals/ingredient/",
}

kg = ${kglab}.KnowledgeGraph(
name = "A recipe KG example based on Food.com",
namespaces = namespaces,
)
kg`
Insert cell
Insert cell
construct_the_graph = py`import rdflib

node = rdflib.URIRef("https://www.food.com/recipe/327593")
kg = ${kg}
kg.add(node, kg.get_ns("rdf").type, kg.get_ns("wtm").Recipe)
kg.add(node, kg.get_ns("wtm").hasCookTime, rdflib.Literal("PT8M", datatype=kg.get_ns("xsd").duration))
kg.add(node, kg.get_ns("wtm").hasIngredient, kg.get_ns("ind").ChickenEgg)
kg.add(node, kg.get_ns("wtm").hasIngredient, kg.get_ns("ind").CowMilk)
kg.add(node, kg.get_ns("wtm").hasIngredient, kg.get_ns("ind").WholeWheatFlour)
`
Insert cell
Insert cell
py` #depends on having ${construct_the_graph} complete
write_out = []
for s, p, o in ${kg}.rdf_graph():
write_out.append({s, p, o})
write_out`
Insert cell
Insert cell
py`#depends on having ${construct_the_graph} complete
s = ${kg}.save_rdf_text(format="ttl")
s`
Insert cell
Insert cell
py` #depends on having ${construct_the_graph} complete
${kg}.save_rdf("tmp.ttl")
`
Insert cell
py` #depends on having ${construct_the_graph} complete
${kg}.save_rdf("tmp.xml", format="xml")
`
Insert cell
py` #depends on having ${construct_the_graph} complete
${kg}.save_jsonld("tmp.jsonld")
`
Insert cell
Insert cell
Insert cell
//libarrow and parquet not yet supported in javascript
//kg.save_parquet("tmp.parquet")
Insert cell
Insert cell
html`${await py` #depends on having ${construct_the_graph} complete
import pandas as pd
import os

file_paths = ["tmp.jsonld", "tmp.ttl", "tmp.xml"]#, "tmp.parquet"]
file_sizes = [os.path.getsize(file_path) for file_path in file_paths]

df = pd.DataFrame({"file_path": file_paths, "file_size": file_sizes})
df.to_html()`}`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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 { 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