-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
63 lines (51 loc) · 1.78 KB
/
app.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
52
53
54
55
56
57
58
59
60
61
62
63
import os
import py2neo
from flask import Flask
app = Flask(__name__)
STATEMENT_PEOPLE_FRAMEWORKS = """
MATCH
(dept:Department {name: "IT Department"})
<-[:WORKS_AT]-(people:Person)-[:LIKES]->
(framework:Framework)
RETURN framework.name, people.name
ORDER BY framework.name
"""
STATEMENT_RECOMMEND = """
MATCH (subject:Person)
WHERE subject.name=$person_name
MATCH
(dept:Department {name: "IT Department"})
-[:WORKS_AT]-
(people:Person)
-[:LIKES]-
(likeable_framework:Framework)
-[:LANGUAGE]-
(:Language {name: "Python"})
WHERE NOT (subject)-[:LIKES]->(likeable_framework)
RETURN DISTINCT likeable_framework.name
"""
graph = py2neo.Graph("bolt://localhost:7687", password=os.getenv("PASSWORD"))
@app.route("/")
def people():
results = graph.run(STATEMENT_PEOPLE_FRAMEWORKS).data()
display, framework = "", ""
for result in results:
if not framework == result["framework.name"]:
prefix = "<br>"
else:
prefix = ""
framework = result["framework.name"]
display = display + f"""{prefix}<div><b>{result['people.name']}</b> likes <b>{framework}</b></div>\n"""
display = display + "</p>"
for result in graph.match(r_type="WORKS_AT").all():
name = result.nodes[0]["name"]
display = display + f"""<div>Recommend framework for <a href="/recommend_framework/{name}">{name}</a></div>\n"""
display = display + "</p>"
return display
@app.route("/recommend_framework/<person_name>")
def recommend(person_name):
recommendation = graph.run(STATEMENT_RECOMMEND, person_name=person_name).data()
if recommendation:
return f"""<div><b>{person_name}</b> should try: <b>{recommendation[0]['likeable_framework.name']}</b></div>"""
else:
return "No recommendations!"