-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
61 lines (48 loc) · 1.93 KB
/
Copy pathapp.py
File metadata and controls
61 lines (48 loc) · 1.93 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Num2Vid Flask Server.
All Flask-related logic, as well as routes are centralized here.
"""
import logging
import sys
from os import path as osp
from os import environ
from flask import Flask, render_template, send_file
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
import num2vid
APP = Flask(__name__)
APP.config["WTF_CSRF_ENABLED"] = False
CONFIG = num2vid.Config(environ["NUM2VID_CONFIG"])
logging.basicConfig(filename=osp.join(
CONFIG.get("logging_output_dir"), "num2vid_log"), level=logging.DEBUG)
sys.stdout.write("Logging to: {0}\n".format(osp.abspath(CONFIG.get("logging_output_dir"))))
## routes
@APP.route("/", methods=["GET", "POST"])
def server_ui():
"""Serve server index with form and calculation result if present."""
form = Num2VidServerForm()
# data to be sent to UI
data = {
"ver": num2vid.__version__,
"form": form
}
# if something was entered attempt calculation
if form.math_str.data:
try:
data["calculated_result"] = num2vid.calculate(form.math_str.data)
except (num2vid.errors.InvalidMathStrError,
num2vid.errors.InvalidMathStrResultError) as err:
data["calculated_result"] = err # err implements custom-defined __repr__ method
return render_template("num2vid_server_ui.html", data=data)
@APP.route("/convert/<int:num>")
def convert(num) -> "ffmpeg-generated vid":
"""Convert num to video.
:param num: the num received from client.
"""
APP.logger.info("Recieved request for num: {0}".format(num))
return send_file(num2vid.convert(num), as_attachment=True)
class Num2VidServerForm(FlaskForm):
"""flask_wtf FlaskForm description."""
math_str = StringField("Enter a math equation:", validators=[DataRequired()])
submit = SubmitField("Calculate.")
APP.run(host=CONFIG.get("flask_host"), port=CONFIG.get("flask_port"), debug=True)