-
Notifications
You must be signed in to change notification settings - Fork 10
/
web.py
executable file
·93 lines (76 loc) · 2.82 KB
/
web.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
import application
import flask
import shutil
import tempfile
import pathlib
import werkzeug.exceptions
TEMPDIR = tempfile.mkdtemp() # Dumping the logfile, temporary data, etc.
ARCHIVEDIR = tempfile.mkdtemp() # Dumping the ZIP archive
app, bokeh_resources = application.config.create_app() # Creating the app
@app.route('/')
def index():
"""
Renders the main page. A warning pops up, if the machine is not
connected to the internet.
"""
if application.utils.is_connected():
return flask.render_template('index.html')
else:
return flask.render_template('index.html', internet='warning')
@app.route('/help')
def help():
"""
Renders the help page.
"""
return flask.render_template('help.html')
@app.route('/modeling', methods=['POST'])
def modeling():
"""
Streams the modeling page, printing useful information to the UI.
The generated data will be dumped into a tempdir (specified above).
"""
def stream_template(template_name, **context):
app.update_template_context(context)
t = app.jinja_env.get_template(template_name)
return t.stream(context)
stream = flask.stream_with_context(application.modeling.workflow(TEMPDIR,
ARCHIVEDIR,
bokeh_resources))
return flask.Response(stream_template('modeling.html', info=stream))
@app.route('/model')
def model():
"""
Loads the dumped data, deletes the temporary data, and renders the model page.
"""
data = application.utils.load_data(TEMPDIR)
shutil.rmtree(TEMPDIR) # Removing the tempdir
return flask.render_template('model.html', **data)
@app.route('/model/download')
def download_model_output():
"""
Sends the generated data as ZIP archive to the user.
"""
zip_archive = str(pathlib.Path(ARCHIVEDIR, 'topicmodeling.zip'))
return flask.send_file(filename_or_fp=zip_archive,
mimetype='application/zip',
attachment_filename='topicmodeling.zip',
as_attachment=True)
@app.errorhandler(werkzeug.exceptions.HTTPException)
def handle_http_exception(e):
"""
Handles any HTTP Exception, shows a Bootstrap modal with the error
message and renders the index.html again.
"""
return flask.render_template('index.html', http='error', error_message=e)
for code in werkzeug.exceptions.default_exceptions:
app.errorhandler(code)(handle_http_exception)
@app.after_request
def add_header(r):
"""
Clears the cache after a request.
"""
r.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
r.headers['Pragma'] = 'no-cache'
r.headers['Expires'] = '0'
r.headers['Cache-Control'] = 'public, max-age=0'
return r