This repository was archived by the owner on Jun 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
105 lines (83 loc) · 2.32 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import json
import os
from urllib.parse import unquote
from mpld3._display import NumpyEncoder
from werkzeug.utils import secure_filename
from werkzeug.exceptions import BadRequest
from flask import (
flash,
Flask,
jsonify,
make_response,
redirect,
request,
render_template,
session,
url_for
)
from flask.views import MethodView
import config
from utils import allowed_file, get_plot
app = Flask(
__name__,
static_folder="static/dist",
template_folder="static")
app.config['SESSION_TYPE'] = config.SESSION_TYPE
app.config['SECRET_KEY'] = config.SECRET_KEY
app.config['UPLOAD_FOLDER'] = config.UPLOAD_FOLDER
# basic views
@app.errorhandler(400)
def err_400(error):
return make_response(
jsonify({'error': 'Bad Request'}), 400)
@app.errorhandler(404)
def err_404(error):
return make_response(
jsonify({'error': 'Not Found'}), 404)
@app.route("/")
def index():
path_list = [
os.path.join(r, file)
for r, d, f in os.walk(app.config["UPLOAD_FOLDER"])
for file in f
]
return render_template("index.html", path_list=sorted(path_list))
class FileAPI(MethodView):
def get(self):
path = request.args.get('path')
try:
plt = get_plot(path)
except TypeError as e:
return jsonify("", 204)
return json.dumps(plt, cls=NumpyEncoder)
def post(self):
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filepath = os.path.join(app.config["UPLOAD_FOLDER"], filename)
file.save(filepath)
return jsonify({"filepath": filepath}, 201)
raise BadRequest
def delete(self, fpath):
os.remove(fpath)
return jsonify({"success": True}, 200)
file_view = FileAPI.as_view('file_api')
app.add_url_rule(
"/api/files/",
view_func=file_view,
methods=["GET", "POST"]
)
app.add_url_rule(
"/api/files/<path:fpath>",
view_func=file_view,
methods=['DELETE',]
)
if __name__ == '__main__':
session.init_app(app)
app.run()