Created
March 19, 2017 02:05
-
-
Save OmarWKH/87be358fe4a4f9ac524515f6ef557dc1 to your computer and use it in GitHub Desktop.
flask/neo4j prototype
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Creates an event with 2 choices and 2 ends and serves them at http://127.0.0.1:5000/event/1 | |
Works with a running server of neo4j.com, a graph DB that uses Cypher query language | |
Based on: https://nicolewhite.github.io/neo4j-flask/ | |
(some of the py2neo code is out of date, I replaced it with the new API) | |
(this mentions virtualenv, but in python 3 just use venv) | |
Packages: | |
flask | |
py2neo | |
(the link above uses more packages) | |
''' | |
from flask import Flask, render_template # web framework @ http://flask.pocoo.org/ | |
from py2neo import Graph, Node, Relationship # neo4j python driver @ http://py2neo.org | |
from py2neo.ogm import GraphObject, Property, RelatedTo, RelatedObjects # driver object-graph mapping | |
app = Flask(__name__, template_folder='.') # the Flask app will find event.html template in current folder | |
## Schema and testing data ## | |
# (:event)-[:leads_to]->(:event) | |
graph = Graph() | |
# comment out if you get 'already exists' | |
# graph.schema.create_uniqueness_constraint('Event', 'id') # Event nodes must have unique id (but could have no id) | |
start = Node('Event', id=1, text='There is a thing') # Node with label 'Even' and properties 'id' and 'text' | |
end1 = Node('Event', id=2, text='The thing is no more') | |
end2 = Node('Event', id=3, text='The thing is grateful') | |
option1 = Relationship(start, 'LEADS_TO', end1, text='I sword it with my sword') # Relationship with label 'LEADS_TO' and property 'text' | |
option2 = Relationship(start, 'LEADS_TO', end2, text="I tell it it's pretty") | |
graph.merge(option1) # adds to DB (if doesn't already exist) | |
graph.merge(option2) | |
## Models ## | |
class Event(GraphObject): | |
__primarykey__ = 'id' | |
_id = Property('id') # used _id because python has id function. argument indicates that property name in DB is 'id' | |
text = Property() | |
next_events = RelatedTo('Event', 'LEADS_TO') # RelatedObjects object (collection of objects with given relationship) | |
def find(_id): | |
return Event.select(graph, int(_id)).first() | |
def paths(self): # return list of tuples (choice, next_event_id) | |
paths = list() | |
_next = self.next_events # assign to _name just for ease | |
for event in _next: | |
option = _next.get(event, 'text') # gets value of relationship property 'text', like 'I sword it with my sword' | |
path = (option, event._id) | |
paths.append(path) | |
return paths | |
## Views ## | |
# try going to http://127.0.0.1:5000/event/1 | |
@app.route('/event/<_id>') | |
def event(_id): | |
event = Event.find(_id) | |
text = event.text | |
paths = event.paths() | |
return render_template('event.html', text=text, paths=paths) # server will serve event.html, and text and paths can be used in event.html | |
## Run ## | |
app.run(debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p> | |
{{ text }} | |
</p> | |
<ul> | |
{% for option, next in paths %} | |
<li> | |
<a href="{{ url_for('event', _id=next) }}"> | |
{{ option }} | |
</a> | |
</li> | |
{% endfor %} | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment