Skip to content

Commit 06830b9

Browse files
committed
flask template
1 parent c4feff0 commit 06830b9

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed

templates/webapp_ex1/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Sebastian Raschka, 2015
2+
3+
# Flask Example App 1
4+
5+
A simple Flask app that calculates the sum of two numbers entered in the respective input fields.
6+
7+
A more detailed description is going to follow some time in future.
8+
9+
You can run the app locally by executing `python app.py` within this directory.
10+
11+
<hr>
12+
13+
![](./img/img_1.png)

templates/webapp_ex1/app.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from flask import Flask, render_template, request
2+
from wtforms import Form, DecimalField, validators
3+
4+
app = Flask(__name__)
5+
6+
7+
class EntryForm(Form):
8+
x_entry = DecimalField('x:',
9+
places=10,
10+
validators=[validators.NumberRange(-1e10, 1e10)])
11+
y_entry = DecimalField('y:',
12+
places=10,
13+
validators=[validators.NumberRange(-1e10, 1e10)])
14+
15+
@app.route('/')
16+
def index():
17+
form = EntryForm(request.form)
18+
return render_template('entry.html', form=form, z='')
19+
20+
@app.route('/results', methods=['POST'])
21+
def results():
22+
form = EntryForm(request.form)
23+
z = ''
24+
if request.method == 'POST' and form.validate():
25+
x = request.form['x_entry']
26+
y = request.form['y_entry']
27+
z = float(x) + float(y)
28+
return render_template('entry.html', form=form, z=z)
29+
30+
if __name__ == '__main__':
31+
app.run(debug=True)

templates/webapp_ex1/img/img_1.png

6.13 KB
Loading

templates/webapp_ex1/static/style.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
body{
2+
width:600px;
3+
}
4+
5+
#button{
6+
padding-top: 20px;
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% macro render_field(field) %}
2+
<dt>{{ field.label }}
3+
<dd>{{ field(**kwargs)|safe }}
4+
{% if field.errors %}
5+
<ul class=errors>
6+
{% for error in field.errors %}
7+
<li>{{ error }}</li>
8+
{% endfor %}
9+
</ul>
10+
{% endif %}
11+
</dd>
12+
{% endmacro %}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Webapp Ex 1</title>
5+
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
6+
</head>
7+
<body>
8+
9+
{% from "_formhelpers.html" import render_field %}
10+
11+
<form method=post action="/results">
12+
<dl>
13+
{{ render_field(form.x_entry, cols='1', rows='1') }}
14+
{{ render_field(form.y_entry, cols='1', rows='1') }}
15+
</dl>
16+
<div>
17+
<input type=submit value='Submit' name='submit_btn'>
18+
</div>
19+
20+
<dl>
21+
x + y = {{ z }}
22+
</dl>
23+
</form>
24+
25+
</body>
26+
</html>

0 commit comments

Comments
 (0)