Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. '''
  2. This is a POC to work on AWS.
  3. Author: Vipin B Rai.
  4. Version: 0.1
  5. '''
  6.  
  7. from flask import Flask, request, g, jsonify
  8. import sqlite3
  9.  
  10. # DB config for aws.
  11. DATABASE = '/home/ubuntu/flaskapp/test.db'
  12.  
  13. # DB config for local.
  14. # DATABASE = '/home/vipin/workspaces/aws_stuff/weather_app/test.db'
  15.  
  16. app = Flask(__name__)
  17. app.config.from_object(__name__)
  18.  
  19. def connect_to_database():
  20. return sqlite3.connect(app.config['DATABASE'])
  21.  
  22. def get_db():
  23. db = getattr(g, 'db', None)
  24. if db is None:
  25. db = g.db = connect_to_database()
  26. return db
  27.  
  28. @app.teardown_appcontext
  29. def close_connection(exception):
  30. db = getattr(g, 'db', None)
  31. if db is not None:
  32. db.close()
  33.  
  34. def execute_query(query, args=()):
  35. cur = get_db().execute(query, args)
  36. rows = cur.fetchall()
  37. cur.close()
  38. return rows
  39.  
  40. @app.route('/')
  41. def hello():
  42. return "<h1> Welcome to Weather Monitoring Portal</h2>"
  43.  
  44. @app.route('/add',methods=['POST'])
  45. def addinfo():
  46. db=get_db()
  47. input_str=dict(request.get_json(force=True))
  48. t=int(input_str['temp'])
  49. h=int(input_str['humidity'])
  50. db.execute('insert into test (temp, humidity) values(?,?);',
  51. [t,h])
  52. db.commit()
  53. db.close()
  54. return "Values inserted."
  55.  
  56. # This shows the basic contents of the db.
  57. # To-Do The front end part yet to be decided.
  58. @app.route("/viewdb")
  59. def viewdb():
  60. rows = execute_query("""SELECT * FROM test;""")
  61. return '<br>'.join(str(row) for row in rows)
  62.  
  63. if __name__=='__main__':
  64. app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement