-
Notifications
You must be signed in to change notification settings - Fork 0
/
5. flask_templates.py
85 lines (65 loc) · 1.68 KB
/
5. flask_templates.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
from flask import Flask, render_template
app = Flask(__name__)
'''
you can do something like this:
@app.route('/')
def index():
return '<html><body><h1>Hello World</h1></body></html>'
# However, generating HTML content from Python code is cumbersome,
# especially when variable data and Python language elements
# like conditionals or loops need to be put.
# This would require frequent escaping from HTML.
'''
'''
Alternatively, We can take advantage of Jinja2 template engine, on which Flask is based.
Instead of returning hardcode HTML from the function,
a HTML file can be rendered by the render_template() function.
'''
@app.route('/hello/<user>')
def hello_name(user):
return render_template('hello.html', name = user)
# hello.html should be contained in ./templates/ directory
'''
OUR hello.html:
<!doctype html>
<html>
<body>
<h1>Hello {{ name }}!</h1>
</body>
</html>
'''
if __name__ == '__main__':
app.run(debug = True)
'''
# More examples:
<!doctype html>
<html>
<body>
{% if marks>50 %}
<h1> Your result is pass!</h1>
{% else %}
<h1>Your result is fail</h1>
{% endif %}
</body>
</html>
@app.route('/hello/<int:score>')
def hello_name(score):
return render_template('hello.html', marks = score)
<!doctype html>
<html>
<body>
<table border = 1>
{% for key, value in result.iteritems() %}
<tr>
<th> {{ key }} </th>
<td> {{ value }} </td>
</tr>
{% endfor %}
</table>
</body>
</html>
@app.route('/result')
def result():
dict = {'phy':50,'che':60,'maths':70}
return render_template('result.html', result = dict)
'''