-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep_5.py
More file actions
43 lines (28 loc) · 1.19 KB
/
step_5.py
File metadata and controls
43 lines (28 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from flask import Flask, render_template, request
app = Flask(__name__)
def get_films_from_file():
with open ('film_ratings.txt', 'r') as file:
split_lines = [line.split(',') for line in file.read().splitlines()]
return split_lines
@app.route('/films/table')
def get_films():
films = []
stars_filter = request.args and request.args['stars']
split_lines = get_films_from_file()
films = [{'film_name': film_name, 'stars': stars}
for [film_name, stars] in split_lines
if not stars_filter or stars == stars_filter]
return render_template('film_table.html', films=films)
@app.route('/films/submit', methods=['GET'])
def get_form():
return render_template("file_submit.html", message=None)
@app.route('/films/submit', methods=['POST'])
def add_film():
title = request.form['film-name']
stars = request.form['stars']
with open('film_ratings.txt', 'a') as file:
file.write(f'{title},{stars}\n')
#file.write(f"{request.form['film-name']},{request.form['stars']}\n")
return render_template('file_submit.html', message={"content": "Successfully added film"})
if __name__ == '__main__':
app.run()