Advertisement
ssoni

app.py Survey (Final)

May 20th, 2022
1,163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. from flask import Flask, render_template, request
  2.  
  3. app = Flask(__name__)
  4.  
  5. @app.route("/")
  6. def index():
  7.     return render_template("index.html")
  8.  
  9. @app.route("/collect", methods=['GET', 'POST'])
  10. def collect():
  11.  
  12.     #get the data into variables
  13.     breakfast = request.form['bseq']
  14.     favTV = request.form['tv']
  15.     cp = request.form['chesspiece']
  16.     favRest = request.form['restaurant']
  17.  
  18.     #save record into txt file
  19.     f = open("results.txt", "a")
  20.     record = breakfast + '|' + favTV + '|' + cp + '|' + favRest + '\n'
  21.     f.write(record)
  22.     f.close()
  23.  
  24.     html = "<HTML><BODY>"
  25.     html = html + "Thank you for your submission."
  26.     html = html + '<br><b>Click <a href="/results">here</a> to see all results'
  27.     html = html + "</BODY></HTML>"
  28.     return(html)
  29.  
  30. @app.route("/results")
  31. def results():
  32.     #open results file
  33.     f = open("results.txt", "r")
  34.  
  35.     html = "<html><body>"
  36.     html = html + "<table border=1>"
  37.     html = html + "<tr>"
  38.     html = html + "<th>Breakfast Seq</th>"
  39.     html = html + "<th>TV Show</th>"
  40.     html = html + "<th>Fav Chess Piece</th>"
  41.     html = html + "<th>Restaurant</th>"
  42.     html = html + "</tr>"
  43.  
  44.     #build HTML table rows from results file
  45.     for line in f:
  46.         cols = line.split("|")
  47.         html = html + "<tr>"
  48.         html = html + "<td>" + cols[0] + "</td>"
  49.         html = html + "<td>" + cols[1] + "</td>"
  50.         html = html + "<td>" + cols[2] + "</td>"
  51.         html = html + "<td>" + cols[3] + "</td>"
  52.         html = html + "</tr>"
  53.  
  54.     html = html + "</table></body></html>"
  55.  
  56.     #return HTML page to browser
  57.     f.close()
  58.     return(html)
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement