MongoDB with a slice of CherryPy: Running a Query and Displaying Results

15 Nov

As you may have noticed, I love CherryPy. Recently, I have been playing with MongoDB and pymongo. In my last post I showed how to create a shapefile from a mongoDB. In this post, I will show how to connect to mongoDB through Python using CherryPy and spitting out the results of a find() in an HTML table.

Here is what the webpage will look like:

And here is the code – I’m sure there is a cleaner way to write this instead of looping twice but i’m writing this on my lunch break and don’t have a lot of time to do more than make it run.

import cherrypy
from pymongo import Connection,GEO2D
class mongocherry(object):
def index(self):
output =[]
output.append(‘<HTML><HEAD><TITLE>QUERY MONGODB</TITLE></HEAD><BODY><h1>Query MongoDB</h1>’)
output.append(‘<table border=”1″><tr><td>ID</td><td>LAT</td><td>LONG</td></tr>’)

for x in db.places.find():

while i<len(output):

return html = True

cherrypy.config.update({‘server.socket_host’: ‘’,
‘server.socket_port’: 8000,


I have servers running on some ports so I needed to set a config to change the port. This site creates a front end to data that will always be current. You can make your index page a form and allow the user to select the data they want, then in another def returnResults(self, user selected data): Spit out all the HTML that is in the index now, passing ‘user selected data’ to mongoDB.

I have really enjoyed playing with mongoDB and am curious about importing Revit schedule. Not sure what I would do with it once it’s loaded. But still thinking…… Any ideas?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: