-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
69 lines (55 loc) · 2.24 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from datetime import datetime
import os
from flask import Flask, flash, request, redirect, send_from_directory, url_for
from werkzeug.utils import secure_filename
from main import convert
UPLOAD_FOLDER = 'images'
OUTPUT_FOLDER = 'outputs'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['OUTPUT_FOLDER'] = OUTPUT_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/outputs/<name>')
def download_file(name):
full_path = os.path.join(app.config['OUTPUT_FOLDER'], name)
return send_from_directory(full_path, "output.txt", as_attachment=True)
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
dt = datetime.today()
seconds = dt.timestamp()
os.mkdir(f"outputs/{seconds}")
convert(file_path, f"outputs/{seconds}", request.form.to_dict())
return redirect(url_for('download_file', name=seconds))
return '''
<!doctype html>
<title>Hatching Style</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file><br/><br/>
<label for="fname">Image Scale:</label>
<input type="text" id="image_scale" name="image_scale" value=5><br/><br/>
<label for="fname">Hatch Angle:</label>
<input type="text" id="hatch_angle" name="hatch_angle" value=45><br/><br/>
<input type=submit value=Upload>
</form>
'''
if __name__ == '__main__':
app.run()