Skip to main content

A Python package for modelling data in a Neo4j graph database.

Project description

Neontology

Easily ingest data into a neo4j graph database with Python and pandas. Neontology is a simple object-graph mapper which lets you use Pydantic models to define Nodes and Relationships. It imposes certain restrictions on how you model data, which aims to make life easier for most users but may provide too many limitations for others. The focus of Neontology is getting data into the database, for running complex queries and accessing data, consider using the neo4j browser or bloom.

Installation

pip install neontology

Example

from typing import ClassVar
import pandas as pd
from neontology import BaseNode, BaseRelationship, init_neontology, auto_constrain

# We define nodes by inheriting from BaseNode
class PersonNode(BaseNode):
    __primarylabel__: ClassVar[str] = "Person"
    __primaryproperty__: ClassVar[str] = "name"
    
    name: str
    age: int

# We define relationships by inheriting from BaseRelationship
class FollowsRel(BaseRelationship):
    __relationshiptype__: ClassVar[str] = "FOLLOWS"
    
    source: PersonNode
    target: PersonNode

# initialise the connection to the database
init_neontology(
    neo4j_uri=NEO4J_URI,
    neo4j_username=NEO4J_USERNAME,
    neo4j_password=NEO4J_PASSWORD
)   

# Define a couple of people
alice = PersonNode(name="Alice", age=40)

bob = PersonNode(name="Bob", age=40)

# Create them in the database
alice.create()
bob.create()

# Create a follows relationship between them
rel = FollowsRel(source=bob,target=alice)
rel.merge()

# We can also use pandas DataFrames to create multiple nodes
node_records = [{"name": "Freddy", "age": 42}, {"name": "Philipa", "age":42}]
node_df = pd.DataFrame.from_records(node_records)

PersonNode.merge_df(node_df)

# We can also merge relationships from a pandas DataFrame, using the primary property values of the nodes
rel_records = [
    {"source": "Freddy", "target": "Philipa"},
    {"source": "Alice", "target": "Freddy"}
]
rel_df = pd.DataFrame.from_records(rel_records)

FollowsRel.merge_df(rel_df)

Configuring your graph connection

With a dotenv file

You can use a .env file as below, which should automatically get picked up by neontology.

# .env
NEO4J_URI=neo4j+s://myneo4j.example.com
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=<PASSWORD>

With the above environment variables defined, you can just use init_neontology() without providing any arguments.

On initialisation

Alternatively, you can explicitly provide access information:

init_neontology(
    neo4j_uri=neo4j+s://mydatabaseid.databases.neo4j.io,
    neo4j_username=neo4j,
    neo4j_password=password
)

Project details


Download files

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

Source Distribution

neontology-0.1.1.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

neontology-0.1.1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file neontology-0.1.1.tar.gz.

File metadata

  • Download URL: neontology-0.1.1.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for neontology-0.1.1.tar.gz
Algorithm Hash digest
SHA256 dae6359bc74fba5f135d95637e241c586d40642a5eeca028bb3c6568f6c97e1b
MD5 88dcce584679fd95245597a294b329d3
BLAKE2b-256 ff47a30a6c2117c3074c7d7a4689622e994fe7b337ccef8811e370774505db7a

See more details on using hashes here.

File details

Details for the file neontology-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: neontology-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for neontology-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5bbec9402a877e2f5ad768f67703cde1d1f9ccbc6cd7b6583813ac35f90a2401
MD5 bbf498711e98ffa013af3d88797c6fe1
BLAKE2b-256 88d3243dca352fd80c0c874bc081f8e6e36ddb029861c490de8fcb30f7fa6fe1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page