-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
88 lines (60 loc) · 1.91 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
"""Latin scansion app."""
import functools
import unicodedata
import flask
import wtforms # type: ignore
import yaml
import latin_scansion
import pynini
CONFIG = "config.yaml"
## Startup.
# Creates app object.
app = flask.Flask(__name__)
# Loads configs.
with open(CONFIG, "r") as source:
app.config.update(yaml.safe_load(source))
## Forms.
class ScansionForm(wtforms.Form):
string = wtforms.StringField(
"string", [wtforms.validators.Length(min=1, max=32768)]
)
show_text = wtforms.BooleanField("show_text")
show_var_pron = wtforms.BooleanField("show_var_pron")
show_feet = wtforms.BooleanField("show_feet")
show_syllables = wtforms.BooleanField("show_syllables")
## Curries functions.
with pynini.Far(app.config["far_path"], "r") as far:
scan_document = functools.partial(
latin_scansion.scan_document,
far["NORMALIZE"],
far["PRONOUNCE"],
far["VARIABLE"],
far["SYLLABLE"],
far["WEIGHT"],
far["HEXAMETER"],
)
## Routes.
@app.route("/")
def index() -> str:
form = ScansionForm() # noqa: F841
return flask.render_template("index.html")
@app.route("/result.html", methods=["POST"])
def result() -> str:
form = ScansionForm(flask.request.form)
if form.validate():
lines = unicodedata.normalize(
"NFC", form.string.data.strip()
).splitlines()
return flask.render_template(
"result.html",
document=scan_document(lines, "<webapp input>"),
# TODO: Is there a way to make these auto-convert to bool
# in the form specification?
show_text=bool(form.show_text.data),
show_var_pron=bool(form.show_var_pron.data),
show_feet=bool(form.show_feet.data),
show_syllables=bool(form.show_syllables.data),
)
return "<p>Form validation failed.</p>"
if __name__ == "__main__":
app.run()