Skip to content

Instantly share code, notes, and snippets.

Created December 5, 2014 19:14
  • Star 2 You must be signed in to star a gist
  • Fork 0 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?
Flask application that serves RethinkDB data in CSV format
#!/usr/bin/env python
from csvkit.convert.js import json2csv
from collections import OrderedDict
from flask import Flask
import rethinkdb as r
import json, StringIO
config = {
"port": 8096,
"host": "",
"database": {
"host": "localhost",
"port": 28015,
"db": "quake"
app = Flask(__name__)
feedUrl = ""
refresh = r.table("quakes").insert(r.http(feedUrl)["features"], conflict="replace")
conn = r.connect(**config["database"])
except Exception as e:
if "already exists" not in e.message: print e
finally: conn.close()
def quakesJSON():
conn = r.connect(**config["database"])
output = r.table("quakes") \
.group(r.epoch_time(r.row["properties"]["time"] / 1000).date()) \
.ungroup().merge({"count": r.row["reduction"].count()}) \
return json.dumps([OrderedDict([
["date", item["group"].strftime("%D")],
["count", item["count"]]]) for item in output])
def quakesCSV():
return json2csv(StringIO.StringIO(quakesJSON()))
if __name__ == "__main__":["host"], port=config["port"], debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment