Python driver
This section describes the Fauna open source Python driver, which provides the resource required to interact with Fauna.
Current stable version |
4.5.0 |
Repository |
Compatibility
The following versions of Python are supported:
-
Python 3.5
-
Python 3.6
-
Python 3.7
-
Python 3.8
-
Python 3.9
-
Python 3.10
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.
Tracing
A trace uniquely identifies a transaction flow. In distributed scenarios,
a traceparent propagates a contextual identifier, which lets you associate
logs from disparate services that participate in the application. The
traceparent
request header represents a request in a tracing system using
a common format.
See traceparent.
Fauna supports distributed tracing by allowing you to apply a
traceparent header to individual queries. The traceparent
request header
format is similar to this example:
traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
.
Use the identifier in the query response in subsequent queries. If the Fauna query logic is not the main application, you should use the identifier provided by that application and follow the recommended processing model for mutating the identifier.
If you don’t include a traceparent identifier, one is generated for you and attached to the query response.
Tags
You can tag individual queries with identifiers that can be used to
associate operations by using the x-fauna-tags
header tag.
-
Tags and associated values can be alphanumeric strings and include the underscore (
_
) character. -
Tag names can be up to 40 characters, and tag values can be up to 80 characters.
-
You can have up to 25 tags per query.
A query fails with a 400 status code if the tag is invalid.
Event streaming
This section demonstrates how to subscribe to change events.
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.
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!