Python driver
This section describes the Fauna open source Python driver, which provides the resource required to interact with Fauna.
Current stable version |
4.3.1 |
Repository |
Compatibility
The following versions of Python are supported:
-
Python 3.5
-
Python 3.6
-
Python 3.7
-
Python 3.8
-
Python 3.9
Usage
from faunadb import query as q
from faunadb.objects import Ref
from faunadb.client import FaunaClient
client = FaunaClient(
secret="YOUR_FAUNA_SECRET",
# NOTE: Use the correct endpoint for your database's Region Group.
endpoint="https://db.fauna.com/",
)
indexes = client.query(q.paginate(q.indexes()))
print(indexes)
See Connections for more details on creating client connections.
Event streaming
This section demonstrates how to subscribe to change events. To learn more, see Event streaming.
There are two kinds of event streaming:
The code required to subscribe to each type is similar. The main difference is the type of Reference involved in the subscription, and the kinds of events that are included in the stream.
There is a cost in compute operations to hold a stream open, or to repeatedly start a stream that fails. See Billing for details. |
Document streaming
The following example subscribes to change events for a document:
Before you run the example:
-
Set the
FAUNADB_SECRET
environment variable, and optionally theFAUNADB_ENDPOINT
environment variable (if you are using Region Groups or Fauna Dev). -
The collection
Scores
must exist.
Once the example is running, you can use the Fauna Dashboard, or another client application, to create or update the target document and watch the events arrive as the changes are made.
For example, if the document does not yet exist, you could run this query in the Fauna Dashboard Shell:
Create(Ref(Collection("Scores"), "1"), { data: { scores: [1, 2, 3] }})
Once the document exists, you could run this query:
Update(Ref(Collection("Scores"), "1"), { data: { scores: [5, 2, 3] }})
The streaming example waits indefinitely for events. Use Ctrl+C to terminate the program.
Set streaming
The following example subscribes to change events for a set:
Before you run the example:
-
Set the
FAUNADB_SECRET
environment variable, and optionally theFAUNADB_ENDPOINT
environment variable (if you are using Region Groups or Fauna Dev). -
The collection
Scores
must exist.
Once the example is running, you can use the Fauna Dashboard, or another client application, to add or delete documents in the "Scores" collection and watch the events arrive as the changes are made. For example, you could run this query in the Fauna Dashboard's Shell:
Create(Collection("Scores"), { data: { scores: [5, 6, 7] }})
The streaming example waits indefinitely for events. Use Ctrl+C to terminate the program.
Next steps
-
Driver repository: https://github.com/fauna/faunadb-python
-
For more information about the Fauna Query Language, see the reference documentation.
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!