{"id":15990875,"url":"https://github.com/prrao87/neo4j-python-fastapi","last_synced_at":"2025-03-27T07:31:28.840Z","repository":{"id":160130291,"uuid":"635072795","full_name":"prrao87/neo4j-python-fastapi","owner":"prrao87","description":"Bulk ingest data into Neo4j using sync or async Python, and expose the data via FastAPI","archived":false,"fork":false,"pushed_at":"2023-07-13T22:01:06.000Z","size":41,"stargazers_count":18,"open_issues_count":0,"forks_count":2,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-03-20T11:39:41.136Z","etag":null,"topics":["async","data-engineering","etl","fastapi","neo4j","pydantic","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prrao87.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-01T22:53:45.000Z","updated_at":"2025-03-15T09:56:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"4f311754-05f2-4298-b638-1ecccf61dcd3","html_url":"https://github.com/prrao87/neo4j-python-fastapi","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prrao87%2Fneo4j-python-fastapi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prrao87%2Fneo4j-python-fastapi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prrao87%2Fneo4j-python-fastapi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prrao87%2Fneo4j-python-fastapi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prrao87","download_url":"https://codeload.github.com/prrao87/neo4j-python-fastapi/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245802644,"owners_count":20674718,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","data-engineering","etl","fastapi","neo4j","pydantic","python"],"created_at":"2024-10-08T05:40:41.338Z","updated_at":"2025-03-27T07:31:28.833Z","avatar_url":"https://github.com/prrao87.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Neo4j for Pythonistas\n\nThis repo contains code for the methods described in this series of blog posts:\n\n1. [Neo4j for Pythonistas: Part 1](https://thedataquarry.com/posts/neo4j-python-1/)\n   * Using Pydantic and async Python to build a graph in Neo4j\n2. [Neo4j for Pythonistas: Part 2](https://thedataquarry.com/posts/neo4j-python-2/)\n   * Build a RESTful API on top of a Neo4j graph\n\n## Goals\n\nThe aim of this code is to build a Neo4j graph via its [officially maintained Python client](https://github.com/neo4j/neo4j-python-driver), using either the sync or async database drivers. The async driver offers support for using Python's `asyncio` coroutine-based asynchronous, concurrent workflows, which can be beneficial in certain scenarios. Both sync and async code is provided in `src/ingest` as a starter template to bulk-ingest large amounts of data into Neo4j in batches, so as to be as efficient as possible. Code will be added in three parts:\n\n\n1. Bulk data ingestion into Neo4j using Pydantic and async Python\n2. Building a RESTful API on top of the Neo4j graph via [FastAPI](https://fastapi.tiangolo.com/)\n3. Building a GraphQL API on top of the Neo4j graph via FastAPI and [Strawberry](https://strawberry.rocks/)\n\nThere are lots of clever ways one can write an API on top of Neo4j, but the main focus of this repo is to keep code readable, and the logic simple and easy enough to extend for future use cases as they arise.\n\n\n## Requirements\n\n### Install Python dependencies\n\nInstall Python dependencies in a virtual environment using `requirements.txt` as follows. All code in this repo has been tested on Python 3.11.\n\n```\n# Setup the environment for the first time\npython -m venv neo4j_venv\n\n# Activate the environment (for subsequent runs)\nsource neoj_venv/bin/activate\n\npython -m pip install -r requirements.txt\n```\n\n\n### Install and run Docker\n\n* [Download Docker](https://docs.docker.com/get-docker/) and run the Docker daemon\n* Use the provided `docker-compose.yml` to set up and run the database in a container\n  * This ensures reproducibility and ease of setup, regardless of the platform used.\n* Copy the file `.env.example` and rename it to `.env`.\n* Fill in the `NEO4J_PASSWORD` field in `.env` to a non-null value -- this will be the password used to log into the Neo4j database running on `localhost`.\n\nTo start the database service, run Docker in detached mode via the compose file.\n\n```sh\ndocker compose up -d\n```\n\nThis command starts a persistent-volume Neo4j database so that any data that's ingested persists on the local system even after Docker is shut down.\n\nTear down the database process and containers at any time using the following command:\n\n```\ndocker compose down\n```\n\n## Dataset\n\nThe [wine reviews dataset](./data/) provided in this repo is a newline-delimited JSON-formatted version of the version obtained from Kaggle datasets.\n\n\n## Run tests\n\nOnce the data is ingested into Neo4j, the APIs and schemas can be tested via `pytest` to ensure that endpoints behave as expected. \n\n\u003e 💡 **Note:** Run the tests **inside the Docker container** as FastAPI communicates with the Neo4j service via its own network inside the container.\n\nTo enter the Docker container, in the following example, the name of the running container obtained via `docker ps` is `neo4j-python-fastapi-fastapi-1`.\n\n```\ndocker exec -it neo4j-python-fastapi-fastapi-1 bash\npytest -v\n```\n\nThe first line runs an interactive bash shell inside the container, and the second runs the tests in verbose mode. Assuming that the data has been ingested into the database, the tests should pass and return something like this.\n\n```\n======================== test session starts ========================\nplatform linux -- Python 3.11.3, pytest-7.3.1, pluggy-1.0.0 -- /usr/local/bin/python\ncachedir: .pytest_cache\nrootdir: /wine\nplugins: asyncio-0.21.0, anyio-3.6.2\nasyncio: mode=Mode.STRICT\ncollected 7 items                                                   \n\nsrc/api/test_main.py::test_search PASSED                      [ 14%]\nsrc/api/test_main.py::test_top_by_country PASSED              [ 28%]\nsrc/api/test_main.py::test_top_by_province PASSED             [ 42%]\nsrc/api/test_main.py::test_most_by_variety PASSED             [ 57%]\nsrc/tests/test_crud.py::test_sync_transactions PASSED         [ 71%]\nsrc/tests/test_crud.py::test_async_transactions PASSED        [ 85%]\nsrc/tests/test_schemas.py::test_wine_schema PASSED            [100%]\n\n========================= 7 passed in 0.45s =========================\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprrao87%2Fneo4j-python-fastapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprrao87%2Fneo4j-python-fastapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprrao87%2Fneo4j-python-fastapi/lists"}