Link Search Menu Expand Document
Start for Free

Build a Python App

Learn how to build a Python app that interacts with Stardog.

Page Contents
  1. Build a Python App
    1. 1. Install Python and Stardog
    2. 2. Create a Directory and venv for your App
    3. 3. Download Sample Data
    4. 4. Creating and Querying our Database
    5. Final Thoughts

In this tutorial we’re going to learn how to create a basic Python app that can interact with Stardog.

1. Install Python and Stardog

If you haven’t already done so, install Python and/or Stardog.

In order to install Python, visit the Python website. In order to install Stardog, follow the instructions on our installation page.

2. Create a Directory and venv for your App

Now that we have Python and Stardog installed, we can start setting up our app. We’ll start by creating a directory for our project.

mkdir python-client && cd python-client

Next, we will make a venv in order to manage and isolate our dependencies for this project.

python3 -m venv env

This will create a new folder in our project directory called env. We now want to activate our venv by executing the following command:

source env/bin/activate

When you want to stop working on your project, run the deactivate command. Now that we have activated our environment, we can install the pystardog library. This will allow us to interact with Stardog from our Python app.

python -m pip install pystardog

3. Download Sample Data

Download the sample data for this tutorial from our Github. In this tutorial, we will store this in a folder named resources.

4. Creating and Querying our Database

With all that out of the way, we can move on to actually creating our app! We’ll start by importing the necessary libraries and setting up our connection. In this example, the endpoint, username, and password for the Stardog server are all the default values. Be sure to change these to the correct values for your installation. Here, we create a new database with the name pythondb.

import stardog
from pathlib import Path

# specify our endpoint, username, and password
# default values are provided, be sure to change if necessary
conn_details = {
    'endpoint': 'http://localhost:5820',
    'username': 'admin',
    'password': 'admin'
}

# create a new admin connection
with stardog.Admin(**conn_details) as admin:
    # create a new database
    db = admin.new_database('pythondb')
    print('Created db')

    db.drop()
    print('Dropped db')

In this code, we drop our new database since it simply contains sample data and we want to be able to run this code multiple times. In an actual app, you most likely will not want to do this.

After we create the database, we can initiate a connection to the server in order to add data to and query our database. Here we first begin the transaction with conn.begin(), add our data to this transaction with conn.add(), then commit our changes to the database with conn.commit(). This code assumes the resources directory is located in the same directory as your Python file.

    with stardog.Connection('pythondb', **conn_details) as conn:
        # begin transaction
        conn.begin()
        # add data to transaction from file
        path = str(Path(__file__).parent.resolve() / 'resources/GettingStarted_Music_Data.ttl')
        conn.add(stardog.content.File(path))
        # commit the changes
        conn.commit()

We follow a similar process for executing a query.

        # SELECT some of the data we just inserted
        print(conn.select('SELECT * {?s a :Person} LIMIT 5'))

Data can also be added directly through code without being stored in a file like so:

conn.begin()
conn.add(stardog.content.Raw(':Yo_Yo_Ma a :Person', 'text/turtle'))
conn.commit()

print(conn.select('SELECT ?s { ?s a :Person }'))

We can also remove data with a DELETE statement like so:

conn.begin()
conn.update('DELETE WHERE { ?s ?p ?o }')
conn.commit()

print(conn.select('SELECT * { ?s ?p ?o }'))

Final Thoughts

We have now created a basic client in Python that can interact with our databases in Stardog. The full documentation for pystardog can be found on the Read The Docs page. The full code for this example can be found in the stardog-examples repo. A Jupyter notebook showcasing the usage of pystardog for data analysis with with pandas can also be found on the pystardog Github page.