-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
41 lines (31 loc) · 1.08 KB
/
Copy pathapp.py
File metadata and controls
41 lines (31 loc) · 1.08 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
from flask import Flask, render_template, request, redirect, Response
import csv
from datetime import datetime
app = Flask(__name__)
students = []
@app.route('/')
def index():
return render_template('index.html', students=students)
@app.route('/submit', methods=['POST'])
def submit():
id = request.form['id']
name = request.form['name']
present = request.form.get('present')
date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
students.append([id, name, date])
return redirect('/')
@app.route('/export', methods=['GET'])
def export():
with open('students.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['ID', 'Name', 'Date'])
writer.writerows(students)
def generate():
with open('students.csv', 'r') as file:
data = file.read()
response = Response(data, mimetype='text/csv')
response.headers.set("Content-Disposition", "attachment", filename="students.csv")
return response
return generate()
if __name__ == '__main__':
app.run(debug=True)