Skip to content

Commit 1a391ea

Browse files
committed
first commit
0 parents  commit 1a391ea

15 files changed

Lines changed: 443 additions & 0 deletions

.devcontainer/SETUP.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Neo4j Python Examples - Devcontainer
2+
3+
A .devcontainer configuration is included, and when opened, such as in a GitHub codespace, all the required software and packages will be installed.
4+
5+
You will need to:
6+
7+
1. Create a new [`.env`](.env) file and copy the contents of the [`.env.example`](.env.example) file into it
8+
2. Update the environment values in the [`.env`](.env) file with the values in your Aura Credentials file which you downloaded when creating your instance.
9+
3. Run the [`test_environment.py`](./llm-knowledge-graph/test_environment.py) program to check the environment is set up correctly.
10+
11+
You can explore the [`examples`](../examples/) to see how you can use the Neo4j Python driver with Aura.

.devcontainer/devcontainer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "Neo4j Python Examples",
3+
"image": "mcr.microsoft.com/devcontainers/python:1-3.12-bullseye",
4+
"postCreateCommand": "bash .devcontainer/post_create.sh",
5+
"customizations": {
6+
"codespaces": {
7+
"openFiles": [
8+
".devcontainer/SETUP.md",
9+
".env.example",
10+
"test_environment.py",
11+
"examples/connect.py"
12+
]
13+
},
14+
"vscode": {
15+
"settings": {
16+
"python.createEnvironment.trigger": "off",
17+
"workbench.editorAssociations": {
18+
"*.md": "vscode.markdown.preview.editor"
19+
}
20+
},
21+
"extensions": [
22+
"ms-python.python",
23+
"ms-python.debugpy",
24+
"neo4j-extensions.neo4j-for-vscode"
25+
]
26+
}
27+
}
28+
}

.devcontainer/post_create.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
pip3 install -r requirements.txt

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
NEO4J_URI="neo4j+s://"
2+
NEO4J_USERNAME="neo4j"
3+
NEO4J_PASSWORD="password"
4+
NEO4J_DATABASE=neo4j

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
.vscode
2+
.env
3+
.venv
4+
5+
# Byte-compiled / optimized / DLL files
6+
__pycache__/
7+
*.py[cod]
8+
*$py.class
9+
10+
# C extensions
11+
*.so
12+
13+
# Distribution / packaging
14+
.Python
15+
build/
16+
develop-eggs/
17+
dist/
18+
downloads/
19+
eggs/
20+
.eggs/
21+
lib/
22+
lib64/
23+
parts/
24+
sdist/
25+
var/
26+
wheels/
27+
share/python-wheels/
28+
*.egg-info/
29+
.installed.cfg
30+
*.egg
31+
MANIFEST

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Neo4j Python Examples
2+
3+
This repository contains code examples to use Python with Neo4j Aura.
4+
5+
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://github.com/codespaces/new/martinohanlon/neo4j-py-example)
6+
7+
> [!IMPORTANT]
8+
> To use GitHub Codespaces you will need to login with a GitHub account.
9+
> You can use the [GitHub Codespaces free monthly usage](https://docs.github.com/en/billing/managing-billing-for-your-products/managing-billing-for-github-codespaces/about-billing-for-github-codespaces#monthly-included-storage-and-core-hours-for-personal-accounts) to view these examples.
10+
11+
## Install
12+
13+
Install the [`neo4j` Python pakage](https://neo4j.com/docs/python-manual/current/).
14+
15+
```bash
16+
pip install neo4j
17+
```
18+
19+
## Examples
20+
21+
- [`connect.py` - Connect to a Neo4j Graph Database](examples/connect.py)
22+
- [`run_cypher.py` - Run a Cypher statement](examples/run_cypher.py)
23+
- [`create_data.py` - Read data from a CSV file and create nodes and relationships](examples/create.py)
24+
- [`transaction.py` - Execute cypher in a transaction](examples/transaction.py)
25+
- [`export_to_dataframe.py` - Export data to a Pandas DataFrame ](examples/export_to_dataframe.py)
26+
27+
## Run
28+
29+
To run the examples on your environment, you will need to:
30+
31+
1. Install the required packages including the [`neo4j` Python driver](https://neo4j.com/docs/python-manual/current/).:
32+
```bash
33+
pip install -r requirements.txt
34+
```
35+
1. Create a new [`.env`](.env) file and copy the contents of the [`.env.example`](.env.example) file into it
36+
1. Update the environment values in the [`.env`](.env) file with the values in your Aura Credentials file which you downloaded when creating your instance.
37+
1. Run the [`test_environment.py`](./llm-knowledge-graph/test_environment.py) program to check the environment is set up correctly.

examples/connect.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This example demonstrates how to connect to a Neo4j database using the Neo4j
2+
# Python driver.
3+
#
4+
# It connects to the database, verifies the connection, runs a simple Cypher
5+
# query to count the nodes, and prints the result.
6+
7+
import os
8+
from dotenv import load_dotenv
9+
load_dotenv()
10+
11+
from neo4j import GraphDatabase
12+
13+
# Initialize the Neo4j driver
14+
driver = GraphDatabase.driver(
15+
os.getenv('NEO4J_URI'),
16+
auth=(
17+
os.getenv('NEO4J_USERNAME'),
18+
os.getenv('NEO4J_PASSWORD')
19+
)
20+
)
21+
22+
# Verify the connection
23+
driver.verify_connectivity()
24+
25+
# Run a simple query to count nodes in the database
26+
records, summary, keys = driver.execute_query(
27+
"RETURN COUNT {()} AS count"
28+
)
29+
30+
# Get the first record
31+
first = records[0]
32+
33+
# Print the count entry
34+
print(first["count"]) # (3)
35+
36+
# Close the driver
37+
driver.close()

examples/create_data.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# This example reads CSV data from examples/data/employees.csv
2+
# and creates nodes and relationships in the Neo4j database.
3+
#
4+
# It creates a graph with 3 node labels, `Person`, `Company`,
5+
# and `Location`, connected by `WORKS_AT` and `LIVES_IN` relationships.
6+
#
7+
# +-----------+ WORKS_AT +-----------+
8+
# | Person |------------------------->| Company |
9+
# +-----------+ +-----------+
10+
# |
11+
# |
12+
# | LIVES_IN
13+
# v
14+
# +-----------+
15+
# | Location |
16+
# +-----------+
17+
18+
import os
19+
from dotenv import load_dotenv
20+
load_dotenv()
21+
22+
import csv
23+
from neo4j import GraphDatabase
24+
25+
FILE_PATH = os.path.join('examples','data','employees.csv')
26+
27+
# Initialize the Neo4j driver
28+
driver = GraphDatabase.driver(
29+
os.getenv('NEO4J_URI'),
30+
auth=(
31+
os.getenv('NEO4J_USERNAME'),
32+
os.getenv('NEO4J_PASSWORD')
33+
)
34+
)
35+
36+
# Verify the connection
37+
# driver.verify_connectivity()
38+
39+
# Cypher query to create the data
40+
cypher_query = """
41+
MERGE (p:Person {id: toInteger($id), name: $name, governmentId: $gov_id})
42+
MERGE (l:Location {name: $location})
43+
MERGE (c:Company {name: $company})
44+
MERGE (p)-[:LIVES_IN]->(l)
45+
MERGE (p)-[:WORKS_AT {position: $position}]->(c)
46+
"""
47+
48+
# Load the CSV file
49+
with open(FILE_PATH, newline='') as csvfile:
50+
reader = csv.DictReader(csvfile)
51+
for row in reader:
52+
53+
# Execute the query with parameters
54+
records, summary, keys = driver.execute_query(
55+
cypher_query,
56+
id=row['id'],
57+
name=row['name'],
58+
gov_id=row['gov_id'],
59+
location=row['location'],
60+
company=row['company'],
61+
position=row['position']
62+
)
63+
64+
# Alternatively, you can pass the row as parameters
65+
# records, summary, keys = driver.execute_query(
66+
# cypher_query,
67+
# parameters_= row
68+
# )
69+
70+
print(summary.counters)
71+
72+
driver.close()

examples/data/employees.csv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
id,name,company,position,location,gov_id
2+
1,"John Doe","TechCorp","Engineer","Delhi",123456789
3+
2,"Jane Smith","TechCorp","Manager","Sydney",987654321
4+
3,"Rajesh Kumar","FinanceLLC","Analyst","London",456789123
5+
4,"Alice Brown","MarketSolutions","Director","London",321654987
6+
5,"Anjali Sharma","TechCorp","Developer","San Francisco",654321987
7+
6,"Emily Green","MarketSolutions","Designer","New York",789123456
8+
7,"David Black","FinanceLLC","CTO","New York",159753456
9+
8,"Susan Blue","TechCorp","HR","Sydney",753159852
10+
9,"Michael Red","MarketSolutions","Sales","San Francisco",951753456
11+
10,"Sunita Patel","FinanceLLC","Sales","Delhi",852456963

0 commit comments

Comments
 (0)