Visualize an ontology with one command

A five-minute tutorial for ttl3d, using the Pizza ontology.

What ttl3d does

ttl3d is a command-line tool that turns one or more RDF files (Turtle, N-Triples, RDF/XML, JSON-LD, TriG, N-Quads) into a single HTML page showing the graph in 3D or 2D. Every IRI that is a subject or the object of a relation becomes a node; every IRI-to-IRI triple becomes a link labelled with its predicate. The page embeds all of its JavaScript, so it opens from a file, works offline, and sends nothing anywhere. It runs on Python 3.10 or newer on Linux, macOS and Windows.

Install

pip install ttl3d

Render the Pizza ontology

The Pizza ontology is the classic OWL tutorial ontology from Stanford and Manchester. Download it, then run:

ttl3d pizza.owl --color-by type -o pizza.html

ttl3d prints one line, 121 nodes, 103 links -> pizza.html (view: 3d, layout: stress, labels: node+edge), and the page is ready. This is the result. --color-by type colours each node by its rdf:type, so classes, object properties and individuals get their own legend rows; the default colours by input file instead, which is what you want when several files make up one ontology.

What you can do in the page

Options you will reach for

OptionWhen
--view 2dStart in the 2D view; the switch is there either way.
--lang dePrefer German labels and definitions; untagged literals rank next.
--type-linksDraw rdf:type as edges instead of listing types on the card.
--attribute-preds foaf:homepage,rdfs:seeAlsoKeep predicates off the picture and show them on the card.
--layout forceSkip the precomputed layout for very large graphs; above 1000 nodes ttl3d does this on its own.
--format xmlForce a parser when the extension misleads, for example Turtle in a .owl file.

Several files, one graph

Pass every file of a modular ontology at once: ttl3d core.ttl extensions.ttl -o all.html. The nodes and links merge into one graph, the legend shows which file declared each node, and every relation row on a card names the files asserting it. That is the case ttl3d was written for, and the Solar System demo shows it with two files.

Too big to draw

A knowledge graph of a million triples needs a question first. ttl3d solar-system.ttl solar-system-missions.ttl --focus :Earth --hops 2 keeps Earth and everything within two links of it, 12 of the demo's 41 nodes; --schema on the same files keeps the classes and properties and drops the instances, 11 nodes. Both run before the layout, and ttl3d prints what it kept on stderr.

From a SPARQL endpoint

A store is drawn without an export: ttl3d --endpoint https://query.wikidata.org/sparql --query @wikidata-moons.rq -o moons.html runs the CONSTRUCT in the file, treats the answer as a source with its own legend row, and prints what it fetched. Queries repeat and mix with files; credentials come from TTL3D_SPARQL_USER/TTL3D_SPARQL_PASSWORD or TTL3D_SPARQL_TOKEN, never from the command line. Answers over 100 MB (--max-mb) are refused with a hint to narrow the query. The example query is in the repository under examples/.

From Python

The same pipeline is a function call, and the options are the command line's:

import rdflib, ttl3d

g = rdflib.Graph().parse("pizza.owl")
ttl3d.write(("pizza", g), "pizza.html", color_by="type")   # a file
ttl3d.show(("pizza", g))                                    # inline, as the last line of a notebook cell

Sources are file paths, rdflib.Graph objects, (name, source) pairs or a {name: source} mapping, in any mix. Open the example notebook in Colab.

Limits

More demos on the demo index. Source, issues and the full option table: github.com/soheilabadifard/TTL_to_3D.