Skip to main content

Official Dgraph client implementation for Python

Project description

Official Dgraph client implementation for Python (Python >= v2.7 and >= v3.5), using grpc.

This client follows the Dgraph Go client closely.

Before using this client, we highly recommend that you go through, and understand how to run and work with Dgraph.


Install using pip:

pip install pydgraph


Build and run the simple project in the examples folder, which contains an end-to-end example of using the Dgraph python client. Follow the instructions in the README of that project.

Using a client

Create a client

A DgraphClient object can be initialised by passing it a list of DgraphClientStub clients as variadic arguments. Connecting to multiple Dgraph servers in the same cluster allows for better distribution of workload.

The following code snippet shows just one connection.

import pydgraph

client_stub = pydgraph.DgraphClientStub('localhost:9080')
client = pydgraph.DgraphClient(client_stub)

Alter the database

To set the schema, create an Operation object, set the schema and pass it to DgraphClient#alter(Operation) method.

schema = 'name: string @index(exact) .'
op = pydgraph.Operation(schema=schema)

Operation contains other fields as well, including drop predicate and drop all. Drop all is useful if you wish to discard all the data, and start from a clean slate, without bringing the instance down.

# Drop all data including schema from the Dgraph instance. This is useful
# for small examples such as this, since it puts Dgraph into a clean
# state.
op = pydgraph.Operation(drop_all=True)

Create a transaction

To create a transaction, call DgraphClient#txn() method, which returns a new Txn object. This operation incurs no network overhead.

It is good practise to call Txn#discard() in a finally block after running the transaction. Calling Txn#discard() after Txn#commit() is a no-op and you can call Txn#discard() multiple times with no additional side-effects.

txn = client.txn()
  # Do something here
  # ...
  # ...

Run a mutation

Txn#mutate(mu=Mutation) runs a mutation. It takes in a Mutation object, which provides two main ways to set data: JSON and RDF N-Quad. You can choose whichever way is convenient. Most users won’t need to create a Mutation object themselves.

Txn#mutate() provides convenience keyword arguments set_obj and del_obj for setting JSON values and set_nquads and del_nquads for setting N-Quad values. See examples below for usage.

We define a person object to represent a person and use it in a transaction.

# Create data.
p = {
    'name': 'Alice',

# Run mutation.

# If you want to use a mutation object, use this instead:
# mu = pydgraph.Mutation(set_json=json.dumps(p).encode('utf8'))
# txn.mutate(mu)

# If you want to use N-Quads, use this instead:
# txn.mutate(set_nquads='_:alice <name> "Alice"')

For a more complete example with multiple fields and relationships, look at the simple project in the examples folder.

Sometimes, you only want to commit a mutation, without querying anything further. In such cases, you can set the keyword argument commit_now=True to indicate that the mutation must be immediately committed.

Keyword argument ignore_index_conflict=True can be used to not run conflict detection over the index, which would decrease the number of transaction conflicts and aborts. However, this would come at the cost of potentially inconsistent upsert operations.

Run a query

You can run a query by calling Txn#query(string). You will need to pass in a GraphQL+- query string. If you want to pass an additional dictionary of any variables that you might want to set in the query, call Txn#query(string, variables=d) with the variables dictionary d.

The response would contain the field json, which returns the response JSON.

Let’s run the following query with a variable $a:

query all($a: string) {
  all(func: eq(name, $a))

Run the query, deserialize the result from JSON and print it out:

# Run query.
query = """query all($a: string) {
  all(func: eq(name, $a))
variables = {'$a': 'Alice'}

res = client.txn().query(query, variables=variables)
# If not doing a mutation in the same transaction, simply use:
# res = client.query(query, variables=variables)

ppl = json.loads(res.json);

# Print results.
print('Number of people named "Alice": {}'.format(len(ppl['all'])))
for person in ppl['all']:

This should print:

Number of people named "Alice": 1

Commit a transaction

A transaction can be committed using the Txn#commit() method. If your transaction consisted solely of calls to Txn#query or Txn#queryWithVars, and no calls to Txn#mutate, then calling Txn#commit() is not necessary.

An error will be raised if other transactions running concurrently modify the same data that was modified in this transaction. It is up to the user to retry transactions when they fail.

txn = client.txn();
  # ...
  # Perform any number of queries and mutations
  # ...
  # and finally...
except Exception as e:
  if isinstance(e, pydgraph.AbortedError):
    # Retry or handle exception.
    raise e
  # Clean up. Calling this after txn.commit() is a no-op
  # and hence safe.

Cleanup Resources

To cleanup resources, you have to call DgraphClientStub#close() individually for all the instances of DgraphClientStub.

SERVER_ADDR = "localhost:9080"

# Create instances of DgraphClientStub.
stub1 = pydgraph.DgraphClientStub(SERVER_ADDR)
stub2 = pydgraph.DgraphClientStub(SERVER_ADDR)

# Create an instance of DgraphClient.
client = pydgraph.DgraphClient(stub1, stub2)

# ...
# Use client
# ...

# Cleanup resources by closing all client stubs.


Building the source

python install
# To install for the current user, use this instead:
# python install --user

If you have made changes to the pydgraph/proto/api.proto file, you need need to regenerate the source files generated by Protocol Buffer tools. To do that, install the grpcio-tools library and then run the following command:

python scripts/

Running tests

Make sure you have a Dgraph server running on localhost before you run this task.

python test

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
pydgraph-1.0.0-py2.py3-none-any.whl (21.8 kB) Copy SHA256 hash SHA256 Wheel py2.py3
pydgraph-1.0.0.tar.gz (18.7 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page