Advertisement
ssoni

app.py Final

May 19th, 2022
590
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from flask import Flask, render_template, request
  2.  
  3. app = Flask(__name__)
  4.  
  5. @app.route("/process_data", methods=['GET', 'POST'])
  6. def process_data():
  7.  
  8.     #get data from form request
  9.     least_fav_class = request.form['lfc']
  10.     flavor = request.form['flavor']
  11.     lrs = request.form['state']
  12.     favsport = request.form['sport']
  13.     fvg = request.form['videogame']
  14.  
  15.     #save data record into a file
  16.     #print(f"c={choco} s={straw} v={vanilla}")
  17.     f = open("results.txt", "a")
  18.     record = least_fav_class + '|' + flavor + '|' + lrs + '|' + favsport + '|' + fvg + '\n'
  19.     f.write(record)
  20.     f.close()
  21.  
  22.     #create HTML response page
  23.     html = "<HTML><BODY>"
  24.     html = html + "Thank you for your submission"
  25.     html = html + '<br>Click <a href="/results">here</a> to see all results'
  26.     html = html + "</BODY></HTML>"
  27.     return(html)
  28.  
  29. @app.route("/")
  30. def index():
  31.     return render_template("index.html")
  32.  
  33. @app.route("/results")
  34. def results():
  35.     #read results file
  36.     f = open("results.txt", "r")
  37.  
  38.     html = "<html><body>"
  39.     html = html + "<table border=1>"
  40.     html = html + "<tr>"
  41.     html = html + "<th>Least Fav Class</th>"
  42.     html = html + "<th>Ice Cream Flavor</th>"
  43.     html = html + "<th>Preferred Vaca Destination</th>"
  44.     html = html + "<th>Sport</th>"
  45.     html = html + "<th>Video Game</th>"
  46.     html = html + "</tr>"
  47.  
  48.     #build HTML table
  49.     for line in f:
  50.         cols = line.split('|')
  51.         html = html + '<tr>'
  52.         html = html + '<td>' + cols[0] + '</td>'
  53.         html = html + '<td>' + cols[1] + '</td>'
  54.         html = html + '<td>' + cols[2] + '</td>'
  55.         html = html + '<td>' + cols[3] + '</td>'
  56.         html = html + '<td>' + cols[4] + '</td>'
  57.         html = html + '</tr>'
  58.  
  59.     html = html + '</table></body></html>'
  60.     f.close()
  61.     return(html)
  62.  
  63.  
  64.  
  65.  
  66.  
Advertisement
RAW Paste Data Copied
Advertisement