from flask import Flask, jsonify, render_template
from py2neo import Graph,authenticate
app = Flask(__name__)
authenticate("localhost:7474","neo4j", "neo4j")
graph = Graph("http://localhost:7474/db/data")
def buildNodes(nodeRecord):
print("............................")
data = {"id": str(nodeRecord.n._id), "label": next(iter(nodeRecord.n.labels))}
data.update(nodeRecord.n.properties)
print(data)
return {"data": data}
def buildEdges(relationRecord):
data = {"source": str(relationRecord.r.start_node._id),
"target": str(relationRecord.r.end_node._id),
"relationship": relationRecord.r.rel.type}
return {"data": data}
@app.route('/')
def index():
print("index")
return render_template('index.html')
@app.route('/graph')
def get_graph():
# print(graph.cypher.execute('MATCH (n) RETURN n').columns)
nodes = map(buildNodes, graph.cypher.execute('MATCH (n) RETURN n'))
print(nodes)
edges = map(buildEdges, graph.cypher.execute('MATCH ()-[r]->() RETURN r'))
print(edges)
# json_2={"nodes":nodes,"edges":edges}
# return json.dumps(json_2, cls=UserEncoder)
elements = {"nodes": nodes, "edges": edges}
print(dict(elements))
return jsonify(elements)
return jsonify(elements)
if __name__ == '__main__':
app.run(debug = False)
When I use Python to connect the graph database(neo4j), I have the problem
'map object at 0x03D25C50' is not JSON serializable
, but map object at 0x03D25C50
is the result of method of map()
. I don't know how to resolve the problem.
Is there anything obvious I'm dong wrong here?