Flask Rest API with MongoDB

I found the great article, How to Build an API with Python and Flask. This post describes how to build REST API using Flask and MySQL. As I’d like to use MongoDB as a database. I’d like to explain how to create REST API using Flask and MongoDB in this post based on How to Build an API with Python and Flask.

1. Set up MongoDB
Please refer http://docs.mongodb.org/manual/installation/.

2. Import data into MongoDB
2.a. Download dataset from Download the raw text file of UFO sightings. This is TSV formated data.
2.b. This TSV file does not have header. So I manually added heder. See below.

$ head sightings.tsv
index	sighted_at	reported_at	location	shape	duration	description	latitude	longitude
0	19951009	19951009	Iowa City, IA			Man repts. witnessing "flash, followed by a classic UFO, w/ a tailfin at back." Red color on top half of tailfin. Became triangular.	41.6611277	-91.5301683

2.c. Also, as mongoimport command does not work well with double-quoted (“) string, I striped double-quotes from file with following command.

cat sightings.tsv | sed 's/"//g' > data.tsv

2.d. Following is command to import TSV formated data into MongoDB.

mongoimport --db ufo --collection ufo --type tsv --file data.tsv

2.e Make sure if data is loaded correctly.

$ mongo ufo
MongoDB shell version: 2.4.3
connecting to: ufo
> db.ufo.count()

3. Install PyMongo, which is Python based driver to access MongoDB

$ pip install pymongo

4. Python codes with Flask and PyMongo
4.a Create connection to MongoDB using PyMongo
4.b toJson(data): To generate JSON from MongoDB objects,  use json_util (http://api.mongodb.org/python/1.7/api/pymongo/json_util.html).
4.c sightings(): PyMongo’s find() method returns Cursor (data is not yet loaded). So it is unable to directly serialize to JSON. So, it requires to read data into another object(s)
4.d sighting(sighting_id): To query by _id, use ObjectId. find_one() method returns object (not cursor).

from flask import Flask, request
import json
from bson import json_util
from bson.objectid import ObjectId
from pymongo import Connection

# Flask
app = Flask(__name__)

# MongoDB connection
connection = Connection('localhost', 27017)
db = connection.ufo
def toJson(data):
"""Convert Mongo object(s) to JSON"""
return json.dumps(data, default=json_util.default)

@app.route('/sightings/', methods=['GET'])
def sightings():
  """Return a list of all UFO sightings
  ex) GET /sightings/?limit=10&offset=20
  if request.method == 'GET':
    lim = int(request.args.get('limit', 10))
    off = int(request.args.get('offset', 0))
    results = db['ufo'].find().skip(off).limit(lim)
    json_results = []
    for result in results:
    return toJson(json_results)

@app.route('/sightings/<sighting_id>', methods=['GET'])
def sighting(sighting_id):
  """Return specific UFO sighting
  ex) GET /sightings/123456
  if request.method == 'GET':
    result = db['ufo'].find_one({'_id': ObjectId(sighting_id)})
    return toJson(result)

if __name__ == '__main__':

6. How to test
Start flask dev server

python sightings.py

Test URLs

Note: For Geo query with MongoDB, I will post how to do it with MongoDB as soon as I could figure out.



  • pythonのインストール - 今回はActivePython2.5.2.2をインストール。
  • Apacheのインストール – 以前インストールしたApache HTTP Server 2.2.xを使用。 

2. Windows用mod_pythonのダウンロード

  • 色々バージョンがあるので、適切なBinary fileをダウンロードして下さい。
  • 私の環境に合ったものはmod_python-3.3.1.win32-py2.5-Apache2.2.exe

3. Exeファイルの実行
4. インストーラーがPtyhonのインストール先を自動で検出してくれる。
5. しかしApache HTTP Serverは自動検出してくれないので、自分で指定
6. 重要:最後のインストールスクリーンに重要な事が書いてある。ここには下記に添付したものと同じ事が書かれている。

Important Note for Windows users, PLEASE READ!!!
1. This script does not attempt to modify Apache configuration,
  you must do it manually:

Edit C:/Program Files/Apache Software Foundation/Apache2.2\conf\httpd.conf,
  find where other LoadModule lines are and add this:
  LoadModule python_module modules/mod_python.so

2. Now test your installation using the instructions at this link:

7. スクリーンに書かれているように、”LoadModule python_module modules/mod_python.so”を、httpd.confに追加
8. 変更を有効にするために、Apacheサーバー再起動
9. mod_pythonのインストールはこれで終了

***** 適当翻訳 *****
2.4 Testing
1. テスト用のディレクトリを作成、例えば C:\public_html\test <-例えば私の環境

<Directory “C:/public_html/test”> <-例えば私の環境
   AddHandler mod_python .py
   PythonHandler mptest 
   PythonDebug On

3. 全ての.pyで終わるURLがmptest.pyにフォワードされる。

from mod_python import apache

def handler(req):
   req.content_type = ‘text/plain’
  req.write(“Hello World!”)
  return apache.OK

6.ブラウザからURLにアクセスしてみる。 http://localhost/test/mptest.py
7. Hello World!とブラウザーに表示される。