Git Product home page Git Product logo

movies-python-bolt's Introduction

Neo4j Movies Application: Quick Start

CI sync example CI async example

movie application

This example application demonstrates how easy it is to get started with Neo4j in Python.

It is a very simple web application that uses our Movie graph dataset to provide a search with listing, a detail view and a graph visualization.

We offer two different ways to run the application: synchronous and asynchronous (using asyncio).

The Stack

These are the components of our Web Application:

  • Application Type: Python-Web Application

  • Web framework:

    • sync: Flask (Micro-Webframework)

    • async: FastAPI (Micro-Webframework)

  • Neo4j Database Connector: Neo4j Python Driver for Cypher Docs

  • Database: Neo4j-Server (4.4 or newer) with multi-database

  • Frontend: jquery, bootstrap, d3.js

Provision a database quickly with Neo4j Sandbox or Neo4j Aura.

Setup

Install Python 3.7-3.11.

Then get yourself set up with virtualenv so we don’t break any other Python stuff you have on your machine. After you’ve got that installed let’s set up an environment for our app:

virtualenv neo4j-movies
source neo4j-movies/bin/activate

The next step is to install the dependencies for the app with pip (or pip3 for python3):

sync:

pip install -r requirements-sync.txt

async:

pip install -r requirements-async.txt

Run locally

And finally let’s start up a web server:

sync:

python movies_sync.py
# or python3 movies_sync.py

Running on http://127.0.0.1:8080/

async:

python movies_async.py
# or python3 movies_async.py

Running on http://127.0.0.1:8080/

Navigate to http://localhost:8080 and you should see your first Neo4j application

Changing the Database

By default, this example application connects to a remote Neo4j database run by Neo4j for this purpose. If you want to connect to a local database, follow these instructions.

Start your local Neo4j Server (Download & Install), open the Neo4j Browser. Then install the Movies data set with :play movies, click the CREATE statement (in some versions, this will not be directly on the first page of the movies example), and hit the triangular "Run" button.

If you haven’t touched the configuration of your Neo4j Server, the database will be reachable at neo4j://localhost:7687.

Use environment variables to let the application know where to connect to the database.

sync:

NEO4J_URI=neo4j://localhost:7687 NEO4J_DATABASE=neo4j NEO4J_USER="<username>" NEO4J_PASSWORD="<password>" python movies_sync.py

Running on http://127.0.0.1:8080/

async:

NEO4J_URI=neo4j://localhost:7687 NEO4J_DATABASE=neo4j NEO4J_USER="<username>" NEO4J_PASSWORD="<password>" python movies_async.py

Running on http://127.0.0.1:8080/

All Configuration Options

Here are all environment variables that can be used to configure the application.

Environment Variable Name Default Value (or N/A)

PORT

8080

NEO4J_URI

neo4j+s://demo.neo4jlabs.com

NEO4J_USER

movies

NEO4J_PASSWORD

movies

NEO4J_DATABASE

movies

movies-python-bolt's People

Contributors

asantod avatar bigmontz avatar dependabot-preview[bot] avatar dependabot[bot] avatar fbiville avatar janekk avatar jexp avatar madhav-nividata avatar robsdedude avatar whatsocks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

movies-python-bolt's Issues

ISSUE: The client is unauthorised due to authentication failure

Hi,
I am trying to implement the sample movies python application and while doing so I am facing authentication failure error. I am following the steps mentioned in the website - https://github.com/neo4j-examples/movies-python-bolt. when I am running "python movies.py" command the following error is showing up -
Traceback (most recent call last): File "movies.py", line 12, in <module> driver = GraphDatabase.driver('bolt://localhost',auth=basic_auth("neo4j", "p assword")) File "C:\Users\611593250\AppData\Local\Programs\Python\Python36-32\lib\site-pa ckages\neo4j\v1\api.py", line 124, in driver return driver_class(uri, **config) File "C:\Users\611593250\AppData\Local\Programs\Python\Python36-32\lib\site-pa ckages\neo4j\v1\direct.py", line 65, in __init__ pool.release(pool.acquire()) File "C:\Users\611593250\AppData\Local\Programs\Python\Python36-32\lib\site-pa ckages\neo4j\v1\direct.py", line 39, in acquire connection = self.acquire_direct(address) # should always be a resolved add ress File "C:\Users\611593250\AppData\Local\Programs\Python\Python36-32\lib\site-pa ckages\neo4j\bolt\connection.py", line 398, in acquire_direct connection = self.connector(address) File "C:\Users\611593250\AppData\Local\Programs\Python\Python36-32\lib\site-pa ckages\neo4j\v1\direct.py", line 64, in <lambda> pool = DirectConnectionPool(lambda a: connect(a, security_plan.ssl_context, **config), self.address) File "C:\Users\611593250\AppData\Local\Programs\Python\Python36-32\lib\site-pa ckages\neo4j\bolt\connection.py", line 555, in connect return Connection(s, der_encoded_server_certificate=der_encoded_server_certi ficate, **config) File "C:\Users\611593250\AppData\Local\Programs\Python\Python36-32\lib\site-pa ckages\neo4j\bolt\connection.py", line 184, in __init__ self.sync() File "C:\Users\611593250\AppData\Local\Programs\Python\Python36-32\lib\site-pa ckages\neo4j\bolt\connection.py", line 337, in sync detail_delta, summary_delta = self.fetch() File "C:\Users\611593250\AppData\Local\Programs\Python\Python36-32\lib\site-pa ckages\neo4j\bolt\connection.py", line 287, in fetch response.on_failure(summary_metadata or {}) File "C:\Users\611593250\AppData\Local\Programs\Python\Python36-32\lib\site-pa ckages\neo4j\bolt\response.py", line 61, in on_failure raise AuthError(message) neo4j.exceptions.AuthError: The client is unauthorized due to authentication fai lure.

Accessing graph database is slow

Hello I find that creating couple of nodes and updating them takes around 5-6 seconds each time. Is it normal ? Are there any ways to improve the access to the database ?

no neo4j package

When I used "PIP install neo4j", it warned that the package was not found,
My python version is 3.6.5

ImportError: cannot import name 'ResultError'

has this been deprecated?

>>> from neo4j.v1 import GraphDatabase, basic_auth, ResultError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'ResultError'

OSError thrown upon starting the example

I have exported the NEO4J_PASSWORD environment variable, even tried replacing the password in the driver initialization. Also tried adding a port number in the driver line.

(neo4j-movies)#> python movies.py Traceback (most recent call last): File "movies.py", line 13, in <module> driver = GraphDatabase.driver('bolt://localhost',auth=basic_auth("neo4j", password)) File "/home/blackplague/development/movies-python-bolt/neo4j-movies/lib/python3.7/site-packages/neo4j/v1/api.py", line 94, in driver return Driver(uri, **config) File "/home/blackplague/development/movies-python-bolt/neo4j-movies/lib/python3.7/site-packages/neo4j/v1/api.py", line 133, in __new__ return subclass(uri, **config) File "/home/blackplague/development/movies-python-bolt/neo4j-movies/lib/python3.7/site-packages/neo4j/v1/direct.py", line 73, in __new__ pool.release(pool.acquire()) File "/home/blackplague/development/movies-python-bolt/neo4j-movies/lib/python3.7/site-packages/neo4j/v1/direct.py", line 44, in acquire return self.acquire_direct(self.address) File "/home/blackplague/development/movies-python-bolt/neo4j-movies/lib/python3.7/site-packages/neo4j/bolt/connection.py", line 453, in acquire_direct connection = self.connector(address, self.connection_error_handler) File "/home/blackplague/development/movies-python-bolt/neo4j-movies/lib/python3.7/site-packages/neo4j/v1/direct.py", line 70, in connector return connect(address, security_plan.ssl_context, error_handler, **config) File "/home/blackplague/development/movies-python-bolt/neo4j-movies/lib/python3.7/site-packages/neo4j/bolt/connection.py", line 707, in connect raise last_error File "/home/blackplague/development/movies-python-bolt/neo4j-movies/lib/python3.7/site-packages/neo4j/bolt/connection.py", line 698, in connect s, der_encoded_server_certificate = _secure(s, address[0], ssl_context, **config) File "/home/blackplague/development/movies-python-bolt/neo4j-movies/lib/python3.7/site-packages/neo4j/bolt/connection.py", line 601, in _secure s = ssl_context.wrap_socket(s, server_hostname=host if HAS_SNI and host else None) File "/home/blackplague/bin/anaconda3/lib/python3.7/ssl.py", line 412, in wrap_socket session=session File "/home/blackplague/bin/anaconda3/lib/python3.7/ssl.py", line 853, in _create self.do_handshake() File "/home/blackplague/bin/anaconda3/lib/python3.7/ssl.py", line 1117, in do_handshake self._sslobj.do_handshake() OSError: [Errno 0] Error

Any idea how to progress from here?

issue pip install -r requirements.txt

Hello, I downloaded the zip file and after i entered these commands $ virtualenv neo4jrestclient-movies
$ source neo4jrestclient-movies/bin/activate which created a new folder in home, where i saved the unziped folder of movies, then i tried to run it by pip as below, and there's error.
(neo4jrestclient-movies) antonina@antonina-HP-Pavilion-g6-Notebook-PC:~$ pip install -r requirements.txt
Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'

Tearing down the driver

Given that in the latest version of the neo4j-driver the driver is actually a context library which needs to be closed and thus when I terminate the app I get the following errors:

Exception TypeError: TypeError("'NoneType' object is not callable",) in <bound method DirectDriver.__del__ of <neo4j.v1.direct.DirectDriver object at 0x11203e210>> ignored
Exception TypeError: "'NoneType' object is not callable" in <bound method Connection.__del__ of <neo4j.bolt.connection.Connection object at 0x11203ed90>> ignored

Is the following the correct logic one should apply?

def get_driver():
    driver = getattr(g, 'driver', None)

    if driver is None:
        driver = g.driver = GraphDatabase.driver('bolt://localhost:7687')

    return driver

def get_session():
    session = getattr(g, 'session', None)

    if session is None:
        session = g.session = get_driver().session()

    return session


@app.teardown_appcontext
def teardown(exception):
    session = getattr(g, 'session', None)

    if session is not None:
        session.close()

    driver = getattr(g, 'driver', None)

    if driver is not None:
        driver.close()

An alternative approach would be to keep the driver globally defined and close it via,

import atexit

driver = GraphDatabase.driver('bolt://localhost')
atexit.register(lambda driver=driver: driver.close())

Database access is not allowed for user 'movies' with roles [PUBLIC, movies]

This web application doesn't work either using the database on demo.neo4jlabs.com or the local database.
When using the database on demo.neo4jlabs.com, following exception is raised:

Traceback (most recent call last):                                                                                                                                                                                                                                               File "C:\Program Files\Python37\lib\site-packages\flask\app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()                                                                                                                                                                                                                                      File "C:\Program Files\Python37\lib\site-packages\flask\app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Program Files\Python37\lib\site-packages\flask\app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()                                                                                                                                                                                                                                                 File "C:\Program Files\Python37\lib\site-packages\flask\app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "movies.py", line 65, in get_graph
results = db.read_transaction(lambda tx: list(tx.run("MATCH (m:Movie)<-[:ACTED_IN]-(a:Person) "
File "C:\Program Files\Python37\lib\site-packages\neo4j\work\simple.py", line 409, in read_transaction
return self._run_transaction(READ_ACCESS, transaction_function, *args, **kwargs)
File "C:\Program Files\Python37\lib\site-packages\neo4j\work\simple.py", line 338, in _run_transaction 
result = transaction_function(tx, *args, **kwargs)
File "movies.py", line 69, in <lambda>
100)}))) 
File "C:\Program Files\Python37\lib\site-packages\neo4j\work\transaction.py", line 130, in run
result._tx_ready_run(query, parameters, **kwparameters)
File "C:\Program Files\Python37\lib\site-packages\neo4j\work\result.py", line 100, in _tx_ready_run
self._run(query, parameters, None, None, None, **kwparameters)
File "C:\Program Files\Python37\lib\site-packages\neo4j\work\result.py", line 144, in _run
self._attach()
File "C:\Program Files\Python37\lib\site-packages\neo4j\work\result.py", line 245, in _attach
self._connection.fetch_message()
File "C:\Program Files\Python37\lib\site-packages\neo4j\work\result.py", line 62, in inner
func(*args, **kwargs)
File "C:\Program Files\Python37\lib\site-packages\neo4j\io\_bolt4.py", line 271, in fetch_message
response.on_failure(summary_metadata or {})
File "C:\Program Files\Python37\lib\site-packages\neo4j\io\_common.py", line 183, in on_failure
raise Neo4jError.hydrate(**metadata)
neo4j.exceptions.Forbidden: {code: Neo.ClientError.Security.Forbidden} {message: Database access is not allowed for user
'movies' with roles [PUBLIC, movies].}

When using the local database, when doing a search, there is no content displayed on the website.

ERROR:neo4j:Unable to retrieve routing information

I followed the instruction on the README.md.

The Neo4j server works just fine, using the Desktop application and browser, I can see the movies data.

When I deploy the FastAPI server, it gives me this error.

File "/Users/phillipn/Desktop/movies-python-bolt/movies_async.py", line 78, in get_graph
    results = await db.read_transaction(work)
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_async/work/session.py", line 407, in read_transaction
    return await self._run_transaction(
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_async/work/session.py", line 360, in _run_transaction
    raise errors[-1]
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_async/work/session.py", line 327, in _run_transaction
    await self._open_transaction(
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_async/work/session.py", line 257, in _open_transaction
    await self._connect(access_mode=access_mode)
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_async/work/session.py", line 109, in _connect
    await super()._connect(access_mode)
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_async/work/workspace.py", line 80, in _connect
    self._connection = await self._pool.acquire(
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_async/io/_pool.py", line 657, in acquire
    await self.ensure_routing_table_is_fresh(
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_async/io/_pool.py", line 602, in ensure_routing_table_is_fresh
    await self.update_routing_table(
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_async/io/_pool.py", line 570, in update_routing_table
    raise ServiceUnavailable("Unable to retrieve routing information")
neo4j.exceptions.ServiceUnavailable: Unable to retrieve routing information

The same thing happens with the Flask server:

File "/Users/phillipn/Desktop/movies-python-bolt/movies.py", line 120, in get_search
    results = db.read_transaction(work, q)
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_sync/work/session.py", line 407, in read_transaction
    return self._run_transaction(
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_sync/work/session.py", line 360, in _run_transaction
    raise errors[-1]
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_sync/work/session.py", line 327, in _run_transaction
    self._open_transaction(
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_sync/work/session.py", line 257, in _open_transaction
    self._connect(access_mode=access_mode)
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_sync/work/session.py", line 109, in _connect
    super()._connect(access_mode)
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_sync/work/workspace.py", line 80, in _connect
    self._connection = self._pool.acquire(
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_sync/io/_pool.py", line 657, in acquire
    self.ensure_routing_table_is_fresh(
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_sync/io/_pool.py", line 602, in ensure_routing_table_is_fresh
    self.update_routing_table(
  File "/Users/phillipn/neo4j-movies/lib/python3.10/site-packages/neo4j/_sync/io/_pool.py", line 570, in update_routing_table
    raise ServiceUnavailable("Unable to retrieve routing information")
neo4j.exceptions.ServiceUnavailable: Unable to retrieve routing information

I'm not sure what it could be. Thanks!

Environment:
Python 3.10.1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.