Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Running Tentris Using Python

Requirements

  • Python>=3.12
  • rdflib>=7
  • glibc>=2.34 (e.g. ubuntu-22.04 or later)

Note: Connecting to local/remote Tentris instances via HTTP is supported on all platforms. Running Tentris within Python is only supported on Linux.

Install

pip install tentris

or edit your requirements.txt as follows.

# ... your other dependencies
tentris

Getting Started

The Tentris Python library is a plugin for rdflib.

Native (Linux only)

With the native TentrisStore, Tentris is embedded in Python applications.

from tentris import TentrisStore
import rdflib

# Create an RDFLib graph using Tentris as its backend
graph = rdflib.Graph(store=TentrisStore())

# Load the graph using the SPARQL Update operation `LOAD`
# https://www.w3.org/TR/2013/REC-sparql11-update-20130321/#load 
graph.update("LOAD <https://files.tentris.io/mona-lisa.ttl>")

# Query the Mona Lisa knowledge graph
query_str = """
   PREFIX foaf: <http://xmlns.com/foaf/0.1/>
   PREFIX dbr: <http://dbpedia.org/resource/>
   PREFIX dbo: <http://dbpedia.org/ontology/>

   SELECT ?name WHERE {
      dbr:Mona_Lisa dbo:author ?person .
      ?person foaf:name ?name .
   }
"""

for binding in graph.query(query_str):
   print(f"{binding.name.n3()}")

The class TentrisStore accepts two optional arguments

  • autocommit (bool): If True, the store will commit after each update. If False, will only commit once commit() is called. Default value: True
  • dirty_reads (bool): If True, will not commit before reading. So you cannot read what you wrote before manually calling commit. Default value: False

TentrisStore operates in-memory and does not persist data on disk. To use a disk-based instance of Tentris, the HTTP-based store should be used.

HTTP (Any Operating System)

Remote or local instances of Tentris can be queried using the HTTP-based TentrisHTTPStore.

Local

Assuming a Tentris server is running on the localhost listening to the default port, it can be queried as shown in the example below.

from tentris import TentrisHTTPStore
import rdflib

graph = rdflib.Graph(store=TentrisHTTPStore())

for binding in graph.query("SELECT * WHERE { ?s ?p ?o } LIMIT 10"):
    print(f"{binding.s.n3()} {binding.p.n3()} {binding.o.n3()}")

Remote

To query remote instances of Tentris, the URL needs to be explicitly stated.

from tentris import TentrisHTTPStore
import rdflib

graph = rdflib.Graph(store=TentrisHTTPStore("https://dbpedia.data.dice-research.org"))

for binding in graph.query("SELECT * WHERE { ?s ?p ?o } LIMIT 10"):
    print(f"{binding.s.n3()} {binding.p.n3()} {binding.o.n3()}")

The class TentrisHTTPStore accepts three optional arguments

  • host (str): The URL of the remote endpoint. Default value: "http://localhost:9080".
  • autocommit (bool): If True, the store will commit after each update. If False, will only commit once commit() is called. Default value: True
  • dirty_reads (bool): If True, will not commit before reading. So you cannot read what you wrote before manually calling commit. Default value: False

For more information about setting up a disk-based instance of Tentris, please refer to the chapter Binary.

Remote Login

To log into a remote instance of Tentris that has authentication enabled, the following can be used.

from tentris import TentrisHTTPStore
import rdflib
import requests

def login(url: str, user: str, password: str) -> str:
    s = requests.Session()
    s.post(f"{url}/login", {"username": user, "password": password})

    c = s.cookies.get("tentris")
    return f"tentris={c}"


SERVER = "https://remote-server-with-auth.example.com"

cookie = login(SERVER, "my-username", "my-password")
graph = rdflib.Graph(store=TentrisHTTPStore(SERVER, headers={"Cookie": cookie}))

for binding in graph.query("SELECT * WHERE { ?s ?p ?o } LIMIT 10"):
    print(f"{binding.s.n3()} {binding.p.n3()} {binding.o.n3()}")