12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- from flask import Flask, render_template, url_for, redirect
- from flask_bootstrap import Bootstrap
- import csv
- from addform import CafeForm
- app = Flask(__name__)
- app.config['SECRET_KEY'] = '8BYkEfBA6O6donzWlSihBXox7C0sKR6b'
- Bootstrap(app)
- # all Flask routes below
- @app.route("/")
- def home():
- return render_template("index.html")
- @app.route('/add', methods=['GET', 'POST'])
- def add():
- form = CafeForm()
- if form.validate_on_submit():
- print(form.data)
- with open('cafe-data.csv', 'a') as csv_file:
- csv_file.write(f"\n{form.cafe.data},"
- f"{form.location.data},"
- f"{form.open.data},"
- f"{form.close.data},"
- f"{form.coffee_rating.data},"
- f"{form.wifi_rating.data},"
- f"{form.power_rating.data}")
- return redirect(url_for('cafes'))
- # Exercise:
- # Make the form write a new row into cafe-data.csv
- # with if form.validate_on_submit()
- return render_template('add.html', form=form)
- @app.route('/cafes')
- def cafes():
- with open('cafe-data.csv', newline='') as csv_file:
- csv_data = csv.reader(csv_file, delimiter=',')
- list_of_rows = []
- for row in csv_data:
- list_of_rows.append(row)
- return render_template('cafes.html', cafes=list_of_rows)
- if __name__ == '__main__':
- app.run(debug=True)
|