Skip to content

Instantly share code, notes, and snippets.

@lucidguppy
Created March 2, 2013 00:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucidguppy/5068915 to your computer and use it in GitHub Desktop.
Save lucidguppy/5068915 to your computer and use it in GitHub Desktop.
from flask import render_template
from random import randrange
#create the app object
app = Flask(__name__)
#get the database
from tables import engine, temperature_table
@app.before_request
def before_request():
g.con = engine.connect()
@app.teardown_request
def teardown_request(exception):
g.con.close()
@app.route("/add_reading")
def add_data():
data = {'temperature': randrange(-40,140)}
stmt = temperature_table.insert(data)
res = g.con.execute(stmt)
return render_template('added_temp.html', results=res)
from sqlalchemy import *
from datetime import datetime
engine = create_engine('postgres://blahblahblah')
metadata = MetaData(bind=engine)
temperature_table = Table(
'tf_temperatures', metadata,
Column('id', Integer, primary_key=True),
Column('temperature', Float, nullable=False),
Column('datetime',DateTime, default=datetime.now))
metadata.create_all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment