Skip to content

Instantly share code, notes, and snippets.

Created October 11, 2011 09:22
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Simple Flask-couchdb demo
import simplejson
from flask import Flask, g, request
from import ViewDefinition
import flaskext.couchdb
app = Flask(__name__)
CouchDB permanent view
docs_by_author = ViewDefinition('docs', 'byauthor',
'function(doc) { emit(, doc);}')
Retrieve docs
def docs(author_id):
docs = []
for row in docs_by_author(g.couch)[author_id]:
return simplejson.dumps(docs)
Add doc
@app.route("/<author_id>/add", methods=['POST'])
def add_doc(author_id):
# Build doc with posted values
doc = { 'author': author_id }
# Insert into database
state = True
except Exception, e:
state = False
return simplejson.dumps({'ok': state})
Flask main
if __name__ == "__main__":
DEBUG = True,
COUCHDB_SERVER = 'http://localhost:5984/',
manager = flaskext.couchdb.CouchDBManager()
manager.add_viewdef(docs_by_author) # Install the view
manager.sync(app)'', port=5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment