#!/usr/bin/env python # coding: utf-8 # i've got a hypothesis that notebooks should have more metadata than display data. further, we can demostrate that rdf data can stored in notebooks and consumed by open source tools, specifically `rdflib`. # In[1]: from schemata import *; import rdflib, json # we'll demonstrate these points on the github user data. the snippet below makes a requests for _my_ public github data. # In[2]: user = Uri.cache()["https://api.github.com/users{/user}"] data = user("tonyfast").get().json() # now we need to annotate this information with rdf metadata. i appreciate it you ignore the messy round about way we are writing context. it will get better. # In[3]: class Context(Dict): avatar_url: Type__["@id"] + Id__[SCHEMA.image] html_url: str(Id__) description: SCHEMA.description company: SCHEMA.affiliation name: SCHEMA.name ctx = Context__[Context.schema().pop("properties")]; ctx.schema() # nevertheless, we derive a ctx # In[4]: jsonld_graph = ctx.expand(data); jsonld_graph # now we get funky on some `rdflib`. or `ctx` is json-ld that we parse into a `rdflib.Graph` # In[5]: g = rdflib.Graph() g.parse(data=json.dumps(jsonld_graph), format=mediatypes.LD.value(mediatypes.ContentMediaType)) # next we'll embed the metadata into our document as turtle that `rdflib` will discover. _this encoding will be hidden from the user, i am `print`ing it for demonstration. # In[6]: html = String.html()(F""""""); print(html); html # to ensure we've captured the metadata we will parse the `rdflib.Graph` in a few ways # 1. the `snippet_graph` is derived just for the minimal script tag. # In[7]: snippet_graph = rdflib.Graph() snippet_graph.parse(data=html, format="html") # 2. the `nbconvert_graph` is derived just for a full fledged webpage of this notebook # In[8]: nbconvert_graph = rdflib.Graph() nbconvert_graph.parse(data=__import__("nbconvert").get_exporter("html")().from_filename("embed-rdf.ipynb")[0], format="html") # 3. the `nbviewer_graph` is loaded with a single url from the `nbviewer` service # In[9]: nbviewer_graph = rdflib.Graph() nbviewer_graph.parse("https://nbviewer.jupyter.org/gist/tonyfast/16d3bc82d69890949212b46040bd86e1") # finally we verify we got all the datas # In[10]: def normalize(graph): """to something we can compare"""; return sorted(__import__("json").loads(graph.serialize(format="json-ld")), key=len) # the context from the `snippet_graph` and the local `nbconvert` translation are the same. # In[11]: assert normalize(snippet_graph)\ == normalize(nbconvert_graph) # we'll filter the primary `metadata` from the graphs # In[12]: metadata = { x: sorted(normalize(x), key=len)[-1] for x in (snippet_graph, nbconvert_graph, nbviewer_graph) } # and notice that they all contain that good, good metadata bout me. # In[13]: assert metadata[snippet_graph] == metadata[nbconvert_graph] == metadata[nbviewer_graph]
click here to see the normalized data # In[14]: normalize(snippet_graph) # In[15]: normalize(nbconvert_graph) # In[16]: normalize(nbviewer_graph)
# Ø