使用Python Neo4j-Embedded的Minimal Flask-Example

时间:2012-06-02 12:07:09

标签: python flask neo4j

我无法通过嵌入式python绑定(neo4j / python-embedded,版本1.6)使用neo4j图形数据库实现Python Micro-Webframework Flask的工作最小示例。我所拥有的(基于此Stack-Overflow - thread)是:

import os
import jpype
from neo4j import GraphDatabase
from flask import Flask, g

# configuration
DATABASE = 'graphdatabase'
DEBUG = True
SECRET_KEY = 'blubber'
USERNAME = 'tobias'
PASSWORD = 'bla'
ADMIN = 'admin'

app = Flask(__name__)
app.config.from_object(__name__)

def connectDB(): 
    return GraphDatabase(app.config['DATABASE'])

def initDB():
    db = connectDB()
    with db.transaction:
        users = db.node()
        userIndex = db.node.indexes.create('users')
        user = db.node(name=app.config['ADMIN'])
        userIndex['name'][app.config['ADMIN']] = user
    db.shutdown()
    print "Database initialized."

@app.before_request
def before_request():
    jpype.attachThreadToJVM()
    g.db = connectDB()

@app.teardown_request
def teardown_request(exception):
    jpype.attachThreadToJVM()
    g.db.shutdown()

@app.route('/')
def index():
    with g.db.transaction:
          userIndex = g.db.node.indexes.get('users')
          user = userIndex['name'][app.config['ADMIN']].single
          username = user['name']
    return render_template('index.html', name=username)

if os.path.exists(app.config['DATABASE']) == False:
    initDB()

if __name__ == '__main__':
  app.run()

不幸的是,它抛出:

    File "/home/tobias/Esk/Dev/adhocracyLight/venv/lib/python2.7/site-packages/flask/app.py", line 1518, in __call__
return self.wsgi_app(environ, start_response)
File "/home/tobias/Esk/Dev/adhocracyLight/venv/lib/python2.7/site-packages/flask/app.py", line 1506, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/tobias/Esk/Dev/adhocracyLight/venv/lib/python2.7/site-packages/flask/app.py", line 1504, in wsgi_app
response = self.full_dispatch_request()
File "/home/tobias/Esk/Dev/adhocracyLight/venv/lib/python2.7/site-packages/flask/app.py", line 1264, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/tobias/Esk/Dev/adhocracyLight/venv/lib/python2.7/site-packages/flask/app.py", line 1262, in full_dispatch_request
rv = self.dispatch_request()
File "/home/tobias/Esk/Dev/adhocracyLight/venv/lib/python2.7/site-packages/flask/app.py", line 1248, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/tobias/Esk/Dev/adhocracyLight/adhocracyLight.py", line 73, in index
userIndex = g.db.node.indexes.get('users')
File "/home/tobias/Esk/Dev/adhocracyLight/venv/lib/python2.7/site-packages/neo4j/index.py", line 36, in get
return self._index.forNodes(name)
java.lang.RuntimeExceptionPyRaisable: java.lang.IllegalArgumentException: No index provider 'lucene' found. Maybe the intended provider (or one more of its dependencies) aren't on the classpath or it failed to load.

我猜问题是由于线程问题(g是线程局部的并且包含指向neo4j-Database的指针,这可能是个坏主意?)。

1 个答案:

答案 0 :(得分:3)

Neo4j Embedded不适用于Web服务器环境 - 您应该使用Neo4j Server和Neo4j Server客户端。

灯泡(http://bulbflow.com)是一款Neo4j服务器客户端,其设计考虑了Flask,实际上bulbflow.com使用Neo4j Heroku Add On(目前是免费的)在Heroku上的Flask上运行。 / p>

您使用Gremlin或Cypher进行查询和交易请求。

有关如何构建应用的示例,请参阅灯泡(https://github.com/espeed/lightbulb)博客示例,esp:

请注意bulbsconf.py中的这一行:

bulbs_config.set_neo4j_heroku()

Lightbulb设计为在Heroku上运行Neo4j Heroku Add On - 如果你没有在Heroku上运行,请保留该行。

然后在您的Flask视图或app.py中,执行:

from bulbsconf import graph