flask template

This commit is contained in:
rasbt 2015-06-25 14:16:13 -04:00
parent c4feff05e5
commit 06830b9393
6 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,13 @@
Sebastian Raschka, 2015
# Flask Example App 1
A simple Flask app that calculates the sum of two numbers entered in the respective input fields.
A more detailed description is going to follow some time in future.
You can run the app locally by executing `python app.py` within this directory.
<hr>
![](./img/img_1.png)

View File

@ -0,0 +1,31 @@
from flask import Flask, render_template, request
from wtforms import Form, DecimalField, validators
app = Flask(__name__)
class EntryForm(Form):
x_entry = DecimalField('x:',
places=10,
validators=[validators.NumberRange(-1e10, 1e10)])
y_entry = DecimalField('y:',
places=10,
validators=[validators.NumberRange(-1e10, 1e10)])
@app.route('/')
def index():
form = EntryForm(request.form)
return render_template('entry.html', form=form, z='')
@app.route('/results', methods=['POST'])
def results():
form = EntryForm(request.form)
z = ''
if request.method == 'POST' and form.validate():
x = request.form['x_entry']
y = request.form['y_entry']
z = float(x) + float(y)
return render_template('entry.html', form=form, z=z)
if __name__ == '__main__':
app.run(debug=True)

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

View File

@ -0,0 +1,7 @@
body{
width:600px;
}
#button{
padding-top: 20px;
}

View File

@ -0,0 +1,12 @@
{% macro render_field(field) %}
<dt>{{ field.label }}
<dd>{{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class=errors>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</dd>
{% endmacro %}

View File

@ -0,0 +1,26 @@
<!doctype html>
<html>
<head>
<title>Webapp Ex 1</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
{% from "_formhelpers.html" import render_field %}
<form method=post action="/results">
<dl>
{{ render_field(form.x_entry, cols='1', rows='1') }}
{{ render_field(form.y_entry, cols='1', rows='1') }}
</dl>
<div>
<input type=submit value='Submit' name='submit_btn'>
</div>
<dl>
x + y = {{ z }}
</dl>
</form>
</body>
</html>