与neo4j图形数据库集成的扩展烧瓶
Flask2Neo4J的Python项目详细描述
烧瓶2NEO4J
用于与Neo4J图形数据库集成的扩展烧瓶
安装
使用pip:
$ pip install flask2neo4j
用法
使用此扩展的基本步骤:
- 创建烧瓶应用程序实例:
import bcrypt
from py2neo import ogm
from flask import Flask, request
from flask2neo4j import Flask2Neo4J
app = Flask(__name__)
- 添加neo4j配置和初始化烧瓶2neo4j扩展:
app.config["NEO4J_USERNAME"] = "neo4j"
app.config["NEO4J_PASSWORD"] = "neo4j"
app.config["NEO4J_URI"] = "bolt://localhost:7687"
flask2neo = Flask2Neo4J()
flask2neo.init_app(app)
- 如果使用GraphObject,则使用Graph对象创建模型
class Users(ogm.GraphObject):
__primarykey__ = "id"
id = ogm.Property()
username = ogm.Property("username")
password = ogm.Property("password")
def create_password(self, password):
self.password = str(bcrypt.encrypt(password))
@property
def is_exist(self):
return flask2neo.graph.exists(self)
def save(self):
if self.is_exist:
flask2neo.graph.merge(self)
flask2neo.graph.push(self)
else:
flask2neo.graph.create(self)
- 将函数寄存器定义为烧瓶请求方法
POST
@app.route("/register", methods=["POST"])
def register():
form = request.form
user = Users()
user.username = form['username']
user.create_password(form['password'])
user.save()
return "User Created"
- 运行烧瓶应用程序
if __name__ == '__main__':
app.run(port=8500, debug=True)
打开浏览器并键入url:http://localhost:8500