# We need to import request to access the details of the POST request
# and render_template, to render our templates (form and response)
# we'll use url_for to get some URLs for the app on the templates
from flask import Flask, render_template, request, url_for
import sqlite3
import os
from datetime import date, datetime
import time
# Initialize the Flask application
app = Flask(__name__)
# Define a route for the default URL, which loads the form
@app.route('/')
def form():
return render_template('form_submit.html')
@app.route('/temp', methods = ['POST'])
def temp():
if request.method == 'POST':
"""modify/update the information for <temperature>"""
# you can use <user_id>, which is a str but could
# changed to be int or whatever you want, along
# with your lxml knowledge to make the required
# changes
temperature = request.args.get('temperature')
location = request.args.get('location')
#data = request.form["temperature"]
# data = request.form # a multidict containing POST data
print temperature, " for location ", location
# if os.path.isfile('/home/pi/environ.db'):
# db = sqlite3.connect('/home/pi/temperature.db')
# c = db.cursor()
# else:
# db = sqlite3.connect('/home/pi/temperature.db')
# c = db.cursor()
# c.execute('''CREATE TABLE temps (location TEXT, temperature int, recordtime TEXT)''')
today = datetime.today()
# c.execute('''INSERT INTO temperature(created_at, tempF) VALUES(?,?)''', (today,t[1]))
#db.commit()
conn = sqlite3.connect('/home/pi/temperatures.db')
c = conn.cursor()
c.execute('''INSERT INTO temps(location, temperature, recordtime)
VALUES(?,?,?)''', (location, temperature, today))
conn.commit()
return render_template('tempform_action.html', location=location, temperature=temperature)
# Define a route for the action of the form, for example '/hello/'
# We are also defining which type of requests this route is
# accepting: POST requests in this case
@app.route('/temp1/', methods=['POST'])
def temp1():
name=request.form['location']
email=request.form['temperature']
return render_template('form_action.html', location=location, temperature=temperature)
# Run the app :)
if __name__ == '__main__':
app.run(
host="0.0.0.0",
port=int("80")
)
Comments