Skip to content

Sample app for a Python API using FastAPI and neomodel

License

Notifications You must be signed in to change notification settings

neo4j-field/neo4j-python-api-sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

neo4j-python-api-sample

This is a starter / sample app for a Python API using FastAPI and neomodel

Requirements

  • Python >= 3.7
  • Neo4j >= 5.11

Installation

Neo4j

You need a running instance of Neo4j. See the Installation guide here for options (including AuraDB).

Then, load the dump provided in the data folder. See how to proceed here : for Aura, or for self-managed.

This database was created using a Kaggle Spotify dataset.

API

To install and run the API, run the following :

pip install pipenv
pipenv install

In main.py, change the value of config.DATABASE_URL to match your database information

Finally, run :

pipenv run dev

Querying the API

Go to http://localhost:8000 in your web browser and you should see the number of songs in your database.

Next, you can navigate to http://localhost:8000/artists?page_size=10&page_number=5 and you will see a list of artists and their songs. Pick a couple of song uids for the next step.

Finally, you can create your own playlist using the following command :

curl -X POST -H "Content-Type: application/json" -d '{
  "title": "The Hives Best Of",
  "songs": ["7r68k5cbrF0GiFGSY538MW", "05pYGBaNqoIqsy5FjsnL1o"]
}' http://localhost:8000/playlists

You should get a similar result. Note that the API went and fetched the properties of the songs after saving the playlist to the database.

{
    "uid":"2721c44d238f4de4888ddd7f7eca834e",
    "title":"The Hives Best Of",
    "songs":[
        {"uid":"05pYGBaNqoIqsy5FjsnL1o","title":"1000 Answers","popularity":0},
        {"uid":"7r68k5cbrF0GiFGSY538MW","title":"A.K.A. I-D-I-O-T","popularity":0}
    ]}

Indexes and constraints

To let neomodel figure out the constraints and indexes from the model description, run the following script :

neomodel_install_labels src/database_models.py --db=bolt://neo4j:password@localhost:7687/spotify

This will look at database_models.py and create constraints for properties with unique_index=True and indexes for properties with index=True.

Customization

If you want to start tuning this sample API to your existing Neo4j database, you can use the inspection script, available from neomodel version 5.2.0 :

# This will create a file with all classes to match your db, including indexes and constraints
neomodel_inspect_database --db=bolt://neo4j:password@localhost:7687/spotify --write-to src/custom_database_models.py

About

Sample app for a Python API using FastAPI and neomodel

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages