Skip to content


To run this notebook in JupyterLab, load examples/ex1_0.ipynb

Building a graph in RDF using rdflib

First we'll build a Graph object in rdflib to which we can add nodes and relations:

import rdflib

g = rdflib.Graph()

In RDF, a graph is constructed from triples, each of which represents an RDF statement that has at least three components:

  • subject: the entity being annotated
  • predicate: a relation between the subject and the object
  • object: another entity or a literal value

We'll represent the anytime crepes recipe by making programmatic calls to rdflib, starting with a URL constructed from the recipe id as an initial node. We'll show this as our first subject s to be annotated using RDF statements.

uri = ""
s = rdflib.URIRef(uri)

Throughout work with KGs, there's an important practice of using persistent identifiers which are both unique and persistent, in other words the opposite of link rot.

We could have used other ways to identify that node, such as a unique name. Even so, if we think of this recipe as a resource online, then its URL is both unique and persistent as long as the "" website is available.

Next we'll use rdf:type as the predicate p to describe the subject as an instance of wmt:Recipe

from rdflib.namespace import RDF

p = RDF.type

While the first two nodes in the graph used vocabularies that are predefined in rdflib, now we'll need to reference other vocabularies. We'll need to use the NamespaceManager in rdflib to bind and access the namespaces for those vocabularies, which is the nm variable:

nm = g.namespace_manager

By convention we use a prefix as a convenience way to abbreviate each namespace. For example, in the rdf:type predicate above the rdf: prefix is an abbreviation for the full URL of the RDF namespace. See the online resource to lookup the common usages for prefixes.

Next we'll define the wtm prefix for the "What to Make Base Ontology" at

uri = ""
ns_wtm = rdflib.Namespace(uri)

prefix = "wtm"
nm.bind(prefix, ns_wtm)

Now we can use this wtm: namespace to reference the object o as the wtm:Recipe entity:

o = ns_wtm.Recipe

Note how that object resolves to the URL – which is a link to the vocabulary's RDF description.

Finally, we'll add the tuple (s, p, o,) to the graph:

g.add((s, p, o,))
<Graph identifier=Ndbc3a429d3724f2db72d661a0d6067ea (<class 'rdflib.graph.Graph'>)>

Now let's add the remaining metadata for the anytime crepes recipe. The required cooking time of "8 minutes" can be represented as a predicate wtm:hasCookTime and the literal 8 which we'll define as an xsd:integer value:

p = ns_wtm.hasCookTime
from rdflib.namespace import XSD

o = rdflib.Literal("8", datatype=XSD.integer)
rdflib.term.Literal('8', datatype=rdflib.term.URIRef(''))
g.add((s, p, o,))

Now let's add the three ingredients ["eggs", "milk", "whole wheat flour"] based on the vocabulary of food ingredients:

p = ns_wtm.hasIngredient
uri = ""
ns_ind = rdflib.Namespace(uri)

prefix = "ind"
nm.bind(prefix, ns_ind)
o = ns_ind.ChickenEgg
g.add((s, p, o,))
g.add((s, p, ns_ind.CowMilk,))
g.add((s, p, ns_ind.WholeWheatFlour,))

To confirm what we've built so far, we can iterate through each of the (s, p, o,) statements in the graph:

for s, p, o in g:
    print(s, p, o) 8

Serialization as "Turtle" statements

First let's show how to serialize the graph as ttl or turtle format. This will be returned from RDF as a byte array, so we'll need to use a Unicode codec to convert the serialized graph into a string:

s = g.serialize(format="ttl")
@prefix ind: <> .
@prefix wtm: <> .
@prefix xsd: <> .

<> a wtm:Recipe ;
    wtm:hasCookTime 8 ;
    wtm:hasIngredient ind:ChickenEgg,
        ind:WholeWheatFlour .

Similarly, we can serialize the graph as RDF statements to a file tmp.ttl in the local directory:

g.serialize(destination="tmp.ttl", format="ttl", encoding="utf-8")

Try taking a look at the tmp.ttl file. Is it the same as the serialization shown above?

Serialization as JSON-LD

Next, let's serialize the graph in JSON-LD format, stored in the tmp.jsonld file in the local directory:

data = g.serialize(

with open("tmp.jsonld", "wb") as f:

Try taking a look at the tmp.jsonld file. Each entity, relation, and literal value has a full URL known as an IRI (internationalized resource locator) which identifies a resource used to define it.

We can make these JSON-LD files a bit more succinct by adding a context that defines prefixes for each of the vocabularies used:

context = {
    "@language": "en",
    "wtm": "",
    "ind": "",
{'@language': 'en',
 'wtm': '',
 'ind': ''}

Now we'll serialize again as JSON-LD, this time using the context:

data = g.serialize(

with open("tmp.jsonld", "wb") as f:

Open these two files and compare the difference. Notice how the ttl file is easier to read (for people), while the json-ld file has all of the metadata explicitly linked and it easier for machines to read – even simply as a JSON file, not using any semantic technologies.


Exercise 1:

By using ns_ind.AllPurposeFlour to represent "flour" as another possible ingredient, how would you extend the graph to represent the German Egg Pancakes recipe?

Exercise 2:

The wtm:hasCookTime predicate uses an xsd:integer literal to represent cooking time in minutes. That may be confusing to someone who is not familiar with this dataset. Instead, represent the cooking time using an xsd:duration literal.

Last update: 2021-05-09