flaskPage.py 970 B

12345678910111213141516171819202122232425262728293031
  1. from flask import Flask, render_template, request, url_for
  2. import sqlite3
  3. foo = {'cat': 1, 'dog': 0}
  4. with sqlite3.connect('cat_dog.db') as conn:
  5. c = conn.cursor()
  6. c.execute('CREATE TABLE IF NOT EXISTS preference(name TEXT, fav_col TEXT, cat INTEGER)')
  7. c.close()
  8. app = Flask(__name__)
  9. @app.route("/")
  10. def index():
  11. return render_template("webpage.html")
  12. @app.route("/status", methods=["POST"])
  13. def status():
  14. name = request.form.get("unique_name")
  15. with sqlite3.connect('cat_dog.db') as conn:
  16. c = conn.cursor()
  17. c.execute("SELECT EXISTS(SELECT name FROM preference WHERE name=?)", (name,))
  18. (swit, ) = c.fetchone()
  19. if swit:
  20. return render_template("stat.html", outline = False, link = request.url_root)
  21. else:
  22. c.execute('INSERT INTO preference (name, fav_col, cat) VALUES(?, ?, ?)', (name, request.form.get("fav_col"), foo[request.form.get("animal")]))
  23. conn.commit()
  24. return render_template("stat.html", outline = True, link = request.url_root)