Query, inspect and visualize ontologies encoded via RDF and OWL.
npx @tessl/cli install tessl/pypi-ontospy@2.1.0A comprehensive Python library and command-line tool for inspecting, querying, and visualizing RDF vocabularies and ontologies encoded using W3C Semantic Web standards (RDF, RDFS, OWL, SKOS). Ontospy provides an intuitive API for loading and interrogating ontologies, with capabilities to analyze ontological structures, extract metadata, and generate detailed HTML documentation.
pip install ontospyimport ontospyCommon usage patterns:
from ontospy import Ontospy, Ontology, OntoClass, OntoPropertyFor entity classes:
from ontospy import RdfEntity, OntoSKOSConcept, OntoShapeimport ontospy
# Load an ontology from file
g = ontospy.Ontospy("path/to/ontology.owl", build_all=True)
# Load from URL
g = ontospy.Ontospy("http://xmlns.com/foaf/0.1/", build_all=True)
# Get basic statistics
stats = g.stats()
print(stats)
# Access ontology components
classes = g.all_classes
properties = g.all_properties
ontologies = g.all_ontologies
# Get specific class by URI
person_class = g.get_class(uri="http://xmlns.com/foaf/0.1/Person")
# Print class hierarchy
g.printClassTree()
# Serialize to RDF format
rdf_output = g.serialize(format="turtle")Ontospy follows a modular architecture centered around entity extraction and analysis:
The library uses RDFLib as its core RDF processing engine and provides higher-level abstractions optimized for ontology analysis workflows.
Load and analyze RDF ontologies from files, URLs, or SPARQL endpoints. Extract classes, properties, individuals, and ontological structures with comprehensive metadata and relationship information.
class Ontospy:
def __init__(self, uri_or_path=None, data=None, file_obj=None,
rdf_format="", verbose=False, hide_base_schemas=True,
hide_implicit_types=True, hide_implicit_preds=True,
hide_individuals=True, sparql_endpoint=None,
credentials=None, build_all=True, pref_title="qname",
pref_lang="en"): ...
def load_rdf(self, uri_or_path=None, data=None, file_obj=None,
rdf_format="", verbose=False): ...
def build_all(self, verbose=False, hide_base_schemas=True,
hide_implicit_types=True, hide_implicit_preds=True,
hide_individuals=True): ...
def stats(self): ...
def serialize(self, format="turtle"): ...
def query(self, sparql_query): ...Structured representations of RDF resources including classes, properties, ontologies, SKOS concepts, and SHACL shapes. Each entity provides methods for traversing relationships, extracting metadata, and analyzing structural properties.
class RdfEntity:
def __init__(self, uri, rdftype=None, namespaces=None,
ext_model=False, is_Bnode=False,
pref_title="qname", pref_lang="en"): ...
def bestLabel(self, prefLanguage="", qname_allowed=True, quotes=False): ...
def ancestors(self, cl=None, noduplicates=True): ...
def descendants(self, cl=None, noduplicates=True): ...
class OntoClass(RdfEntity):
def count(self): ...
def describe(self): ...
class OntoProperty(RdfEntity):
def describe(self): ...Generate professional HTML documentation, interactive visualizations, and markdown exports. Supports multiple output formats including D3.js visualizations, Bootstrap-themed websites, and single-page documentation.
from ontospy.gendocs.actions import build_visualization
from ontospy.gendocs.viz_factory import VizFactory
def build_visualization(g, viz_index, path=None, title="", theme=""): ...
class VizFactory:
def __init__(self, ontospy_graph, title=""): ...
def build(self, output_path=""): ...
def preview(self): ...Interactive command-line tools for ontology exploration, analysis, and documentation generation. Includes shell environment, library management, and batch processing capabilities.
# Entry points
def main_cli(): ...
# CLI commands
def scan(): ...
def gendocs(): ...
def shell(): ...
def lib(): ...Programmatic SPARQL query interface for extracting specific ontological patterns, performing complex analysis, and interrogating ontology structures using standard query language.
class SparqlHelper:
def __init__(self, rdfgraph, sparql_endpoint=False): ...
def getAllClasses(self, hide_base_schemas=True, hide_implicit_types=True): ...
def getAllProperties(self, hide_implicit_preds=True): ...
def getSKOSInstances(self): ...
def getShapes(self): ...# Core types used across the API
URIRef = rdflib.URIRef
Literal = rdflib.Literal
Graph = rdflib.Graph
# Entity collections
EntityList = list # List of RdfEntity objects
StatsResult = list # List of tuples: (category, count)
TreeStructure = list # Nested list representing hierarchy
# Configuration options
RdfFormat = str # "turtle", "xml", "n3", "json-ld", etc.
TitlePreference = str # "qname" or "label"
LanguageCode = str # ISO language code, e.g. "en", "fr"