Adding the Driver

This is your first challenge of this course. Your challenge here is to modify the code to create a new instance of the Driver that can be used across the application.

As we discussed in the About the Driver lesson, it is best practice to create a single instance of the driver in our application per Neo4j cluster or DBMS.

Inside api/neo4j.py, you will see an init_driver() function.

Open api/neo4j.py
python
api/neo4j.py
def init_driver(uri, username, password):
    # TODO: Create an instance of the driver here
    current_app.driver = None

    return None

This function should use the uri, username and password parameters supplied to create an instance of the Neo4j Python Driver. The driver instance should then be assigned to the current_app variable to ensure that it is available throughout the application.

The get_driver() function also included in neo4j.py will provide access to the driver variable set in the init_driver() above.

python
api/neo4j.py
def get_driver():
    return current_app.driver

Challenge: Implement the init_driver function.

Your first challenge is to modify the init_driver() function in api/neo4j.py to create an instance of the driver and assign it to the driver variable declared above. To do this, we will need to:

  1. Install the neo4j dependency using pip.

  2. Import the GraphDatabase object from neo4j into neo4j.py.

  3. Create the driver instance with the uri, username and password parameters passed to the function and use the verify_connectivity() method to assert that the credentials are correct.

Open api/neo4j.py

1. Install the neo4j Dependency

First, you will need to install the neo4j dependency. Run the following command in your terminal session:

sh
Install neo4j
pip install neo4j

2. Importing the Dependency

To include the Driver dependency in our module, you will add an import command to the top of the file. Copy and paste the following code at the top of api/neo4j.py.

python
# Import the neo4j dependency
from neo4j import GraphDatabase

3. Creating the Driver Instance

Create the driver instance by calling the GraphDatabase.driver() function. The first argument will be the uri passed as the first parameter to the function. The function will also require a named auth argument with the tuple: (username, password)

Replace the init_driver placeholder function with the following code:

python
api/neo4j.py
def init_driver(uri, username, password):
    # Create an instance of the driver
    current_app.driver = GraphDatabase.driver(uri, auth=(username, password))

    # Verify Connectivity
    current_app.driver.verify_connectivity()

    return current_app.driver

The function calls the verify_connectivity() function on the driver instance to verify that the connection details are correct before returning the newly created driver instance.

If the connection cannot be made for any reason, an Exception will be thrown. If this occurs, your application will be unable to communicate with Neo4j. Manual investigation will be required to diagnose the issue.

If the connection has been successfully verified, the function will return an instance of the driver.

Testing

To test that this functionality has been correctly implemented, run the following code in a new terminal session:

sh
Running the test
pytest tests/01_connect_to_neo4j__test.py

The test file is located at tests/01_connect_to_neo4j__test.py.

Are you stuck? Click here for help

If you get stuck, you can see a working solution by checking out the 01-connect-to-neo4j branch by running:

sh
Check out the 01-connect-to-neo4j branch
git checkout 01-connect-to-neo4j

You may have to commit or stash your changes before checking out this branch. You can also click here to expand the Support pane.

Check Your Understanding

How many tests are collected and run by the pytest command?

  • ❏ 1

  • ❏ 2

  • ❏ 3

  • ✓ 4

Hint

You can run the tests/01_connect_to_neo4j__test.py test by running the following command:

sh
pytest tests/{test-filename}.py

Solution

There are 4 tests in the test suite.

Lesson Summary

You have implemented the code to create a new driver instance with environment variables read from the .env file. The test also verifies that the driver can connect to the DBMS before the application starts.

The Neoflix API will now be able to read data from and write data to your Neo4j Sandbox instance.

In the next module we will look at how to use the driver to query the DBMS.