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)
This is the tutorial I am following: http://adilmoujahid.com/posts/2015/01/interactive-data-visualization-d3-dc-python-mongodb/
Here is the error when I run the server.
Traceback (most recent call last): File "app.py", line 3, in <module>
from pymongo import MongoClient File "/home/olalkeith/Documents/Github/myproject/venv/local/lib/python2.7/site-packages/pymongo/init.py",
line 83, in <module>
from pymongo.collection import ReturnDocument File "/home/olalkeith/Documents/Github/myproject/venv/local/lib/python2.7/site-packages/pymongo/collection.py",
line 27, in <module>
from bson.raw_bson import RawBSONDocument File "/home/olalkeith/Documents/Github/myproject/venv/local/lib/python2.7/site-packages/bson/raw_bson.py",
line 20, in <module>
from bson import _UNPACK_INT, _iterate_elements ImportError: cannot import name _UNPACK_INT
I just ran into same problem. Try removing the bson module and reinstalling pymongo. See this answer:
https://stackoverflow.com/a/12983651/4621508
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With