-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
42 lines (30 loc) · 1.4 KB
/
app.py
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
from flask import Flask, render_template, request, send_file
import os
import tempfile
from converter import read_assets, read_flight_data, convert, remove_duplicates
from werkzeug.middleware.proxy_fix import ProxyFix
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
if 'file' not in request.files:
return render_template('index.html', error='No file part')
file = request.files['file']
if file.filename == '':
return render_template('index.html', error='No selected file')
if file:
temp = tempfile.NamedTemporaryFile(delete=False)
file.save(temp.name)
airlines_df, planes_df = read_assets('data')
flights_df = read_flight_data(temp.name)
df = convert(flights_df, airlines_df, planes_df)
if request.form.get('remove_duplicates'):
df = remove_duplicates(df)
result_file = tempfile.NamedTemporaryFile(delete=False, suffix='.csv')
df.sort_values(by='Date').to_csv(result_file.name, index=False)
os.unlink(temp.name)
return send_file(result_file.name, as_attachment=True, download_name='converted_flights.csv')
return render_template('index.html')
if __name__ == '__main__':
app.run()