from flask import Flask
from flask import render_template
from pymongo import MongoClient
import json
from bson import json_util
from bson.json_util import dum
app = Flask(__name__)
MONGODB_HOST = 'localhost'
MONGODB_PORT = 27017
DBS_NAME = 'donorschoose'
COLLECTION_NAME = 'projects'
FIELDS = {'school_state': True, 'resource_type': True, 'poverty_level': True, 'date_posted': True, 'total_donations': True, '_id': False}
@app.route('/')
def index():
return render_template("index.html")
@app.route("/donorschoose/projects")
def donorschoose_projects():
connection = MongoClient(MONGODB_HOST, MONGODB_PORT)
collection = connection[DBS_NAME][COLLECTION_NAME]
projects = collection.find(projection=FIELDS)
json_projects = []
for project in projects:
json_projects.append(project)
json_projects = json.dumps(json_projects, default=json_util.default)
connection.close()
return json_projects
if __name__ == "__main__":
app.run(host='0.0.0.0',port=5000,debug=True)
Это следующее учебное пособие: http://adilmoujahid.com/posts/2015/01/interactive-data-visualization-d3-dc-python-mongodb/
Вот ошибка при запуске сервера.
Traceback (последний последний вызов): Файл "app.py", строка 3, в < модуль >
от pymongo import MongoClient File "/home/olalkeith/Documents/Github/myproject/venv/local/lib/python2.7/site-packages/pymongo/ init.py",
строка 83, в < модуль >
from pymongo.collection import ReturnDocument File "/home/olalkeith/Documents/Github/myproject/venv/local/lib/python2.7/site-packages/pymongo/collection.py",
строка 27, в < модуль >
from bson.raw_bson импортировать файл RawBSONDocument "/home/olalkeith/Documents/Github/myproject/venv/local/lib/python2.7/site-packages/bson/raw_bson.py",
строка 20, в < модуль >
from bson import _UNPACK_INT, _iterate_elements ImportError: невозможно импортировать имя _UNPACK_INT