8000 Adding graphs example in the readme · arangodb/python-arango-async@081eb7f · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 081eb7f

Browse files
committed
Adding graphs example in the readme
1 parent dcebf8e commit 081eb7f

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,67 @@ async def main():
7373
student_names = []
7474
async for doc in cursor:
7575
student_names.append(doc["name"])
76+
```
77+
78+
Another example with [graphs](https://docs.arangodb.com/stable/graphs/):
7679

80+
```python
81+
async def main():
82+
from arangoasync import ArangoClient
83+
from arangoasync.auth import Auth
84+
85+
# Initialize the client for ArangoDB.
86+
async with ArangoClient(hosts="http://localhost:8529") as client:
87+
auth = Auth(username="root", password="passwd")
88+
89+
# Connect to "test" database as root user.
90+
db = await client.db("test", auth=auth)
91+
92+
# Get the API wrapper for graph "school".
93+
if await db.has_graph("school"):
94+
graph = db.graph("school")
95+
else:
96+
graph = await db.create_graph("school")
97+
98+
# Create vertex collections for the graph.
99+
students = await graph.create_vertex_collection("students")
100+
lectures = await graph.create_vertex_collection("lectures")
101+
102+
# Create an edge definition (relation) for the graph.
103+
edges = await graph.create_edge_definition(
104+
edge_collection="register",
105+
from_vertex_collections=["students"],
106+
to_vertex_collections=["lectures"]
107+
)
108+
109+
# Insert vertex documents into "students" (from) vertex collection.
110+
await students.insert({"_key": "01", "full_name": "Anna Smith"})
111+
await students.insert({"_key": "02", "full_name": "Jake Clark"})
112+
await students.insert({"_key": "03", "full_name": "Lisa Jones"})
113+
114+
# Insert vertex documents into "lectures" (to) vertex collection.
115+
await lectures.insert({"_key": "MAT101", "title": "Calculus"})
116+
await lectures.insert({"_key": "STA101", "title": "Statistics"})
117+
await lectures.insert({"_key": "CSC101", "title": "Algorithms"})
118+
119+
# Insert edge documents into "register" edge collection.
120+
await edges.insert({"_from": "students/01", "_to": "lectures/MAT101"})
121+
await edges.insert({"_from": "students/01", "_to": "lectures/STA101"})
122+
await edges.insert({"_from": "students/01", "_to": "lectures/CSC101"})
123+
await edges.insert({"_from": "students/02", "_to": "lectures/MAT101"})
124+
await edges.insert({"_from": "students/02", "_to": "lectures/STA101"})
125+
await edges.insert({"_from": "students/03", "_to": "lectures/CSC101"})
126+
127+
# Traverse the graph in outbound direction, breath-first.
128+
query = """
129+
FOR v, e, p IN 1..3 OUTBOUND 'students/01' GRAPH 'school'
130+
OPTIONS { bfs: true, uniqueVertices: 'global' }
131+
RETURN {vertex: v, edge: e, path: p}
132+
"""
133+
134+
async with await db.aql.execute(query) as cursor:
135+
async for doc in cursor:
136+
print(doc)
77137
```
78138

79139
Please see the [documentation](https://python-arango-async.readthedocs.io/en/latest/) for more details.

docs/overview.rst

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Here is an example showing how **python-arango-async** client can be used:
3939
async for doc in cursor:
4040
student_names.append(doc["name"])
4141
42-
You may also use the client without a context manager, but you must ensure to close the client when done:
42+
You may also use the client without a context manager, but you must ensure to close the client when done.
4343

4444
.. code-block:: python
4545
@@ -61,3 +61,65 @@ You may also use the client without a context manager, but you must ensure to cl
6161
6262
# Close the client when done.
6363
await client.close()
64+
65+
Another example with `graphs`_:
66+
67+
.. _graphs: https://docs.arangodb.com/stable/graphs/
68+
69+
.. code-block:: python
70+
71+
from arangoasync import ArangoClient
72+
from arangoasync.auth import Auth
73+
74+
# Initialize the client for ArangoDB.
75+
async with ArangoClient(hosts="http://localhost:8529") as client:
76+
auth = Auth(username="root", password="passwd")
77+
78+
# Connect to "test" database as root user.
79+
db = await client.db("test", auth=auth)
80+
81+
# Get the API wrapper for graph "school".
82+
if await db.has_graph("school"):
83+
graph = db.graph("school")
84+
else:
85+
graph = await db.create_graph("school")
86+
87+
# Create vertex collections for the graph.
88+
students = await graph.create_vertex_collection("students")
89+
lectures = await graph.create_vertex_collection("lectures")
90+
91+
# Create an edge definition (relation) for the graph.
92+
edges = await graph.create_edge_definition(
93+
edge_collection="register",
94+
from_vertex_collections=["students"],
95+
to_vertex_collections=["lectures"]
96+
)
97+
98+
# Insert vertex documents into "students" (from) vertex collection.
99+
await students.insert({"_key": "01", "full_name": "Anna Smith"})
100+
await students.insert({"_key": "02", "full_name": "Jake Clark"})
101+
await students.insert({"_key": "03", "full_name": "Lisa Jones"})
102+
103+
# Insert vertex documents into "lectures" (to) vertex collection.
104+
await lectures.insert({"_key": "MAT101", "title": "Calculus"})
105+
await lectures.insert({"_key": "STA101", "title": "Statistics"})
106+
await lectures.insert({"_key": "CSC101", "title": "Algorithms"})
107+
108+
# Insert edge documents into "register" edge collection.
109+
await edges.insert({"_from": "students/01", "_to": "lectures/MAT101"})
110+
await edges.insert({"_from": "students/01", "_to": "lectures/STA101"})
111+
await edges.insert({"_from": "students/01", "_to": "lectures/CSC101"})
112+
await edges.insert({"_from": "students/02", "_to": "lectures/MAT101"})
113+
await edges.insert({"_from": "students/02", "_to": "lectures/STA101"})
114+
await edges.insert({"_from": "students/03", "_to": "lectures/CSC101"})
115+
116+
# Traverse the graph in outbound direction, breath-first.
117+
query = """
118+
FOR v, e, p IN 1..3 OUTBOUND 'students/01' GRAPH 'school'
119+
OPTIONS { bfs: true, uniqueVertices: 'global' }
120+
RETURN {vertex: v, edge: e, path: p}
121+
"""
122+
123+
async with await db.aql.execute(query) as cursor:
124+
async for doc in cursor:
125+
print(doc)

0 commit comments

Comments
 (0)
0