-
Notifications
You must be signed in to change notification settings - Fork 1
/
moviegraph.py
51 lines (36 loc) · 1.44 KB
/
moviegraph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# coding: utf-8
from flask import Flask, abort, render_template, request
from neo4j.v1 import GraphDatabase
app = Flask(__name__)
# Set up a driver for the local graph database.
driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))
def match_movies(tx, q):
return tx.run("MATCH (movie:Movie) WHERE toLower(movie.title) CONTAINS toLower($term) "
"RETURN movie", term=q).value()
def match_movie(tx, title):
return tx.run("MATCH (movie:Movie) WHERE movie.title = $title "
"OPTIONAL MATCH (person)-[:ACTED_IN]->(movie) "
"RETURN movie, collect(person) AS actors", title=title).single()
@app.route("/")
def get_index():
""" Show the index page.
"""
search_term = request.args.get("q", "")
if search_term:
with driver.session() as session:
movies = session.read_transaction(match_movies, q=search_term)
else:
movies = []
return render_template("index.html", movies=movies, q=search_term)
@app.route("/movie/<path:title>")
def get_movie(title):
""" Display details of a particular movie.
"""
with driver.session() as session:
record = session.read_transaction(match_movie, title)
if record is None:
abort(404, "Movie not found")
return render_template("movie.html", movie=record["movie"], actors=record["actors"])
if __name__ == "__main__":
app.run(debug=True)