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
|
#!/usr/bin/env python3
import sys as _sys
import os.path as _path
from os import environ as _environ
from flask import Flask as _Flask, render_template as _template, \
request as _r
from textoutpc import tohtml as _translate
def _empty_page():
""" Page sans entrée. """
return _template('page.html', result = None, text = '')
def _process_page():
""" Page avec entrée. """
if not 'text' in _r.form:
return _empty_page()
text = _r.form['text']
result = _translate(text)
return _template('page.html', result = result, text = text)
def _guide_page():
""" Page de guide. """
return _template('guide.html')
def run_app(port, debug = False):
""" Création, configuration et lancement de l'application. """
gd = lambda x: _path.relpath(_path.join(_path.dirname(__file__),
x))
app = _Flask('textout', root_path = _path.curdir,
template_folder = gd('templates'), static_folder = gd('static'),
static_url_path = '/static')
# Définition des routes.
app.add_url_rule('/', view_func = _empty_page, methods = ['GET'])
app.add_url_rule('/index.html', view_func = _empty_page, methods = ['GET'])
app.add_url_rule('/index.html', view_func = _process_page,
methods = ['POST'])
app.add_url_rule('/guide.html', view_func = _guide_page, methods = ['GET'])
# Lancement de l'application.
return app.run(port = port, debug = debug)
if __name__ == '__main__':
run_app(int(_environ.get('PORT')), True)
# End of file.
|