mirror of
https://github.com/rasbt/python_reference.git
synced 2024-11-23 20:11:13 +00:00
flask template
This commit is contained in:
parent
c4feff05e5
commit
06830b9393
13
templates/webapp_ex1/README.md
Normal file
13
templates/webapp_ex1/README.md
Normal 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)
|
31
templates/webapp_ex1/app.py
Normal file
31
templates/webapp_ex1/app.py
Normal 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)
|
BIN
templates/webapp_ex1/img/img_1.png
Normal file
BIN
templates/webapp_ex1/img/img_1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.1 KiB |
7
templates/webapp_ex1/static/style.css
Normal file
7
templates/webapp_ex1/static/style.css
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
body{
|
||||||
|
width:600px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#button{
|
||||||
|
padding-top: 20px;
|
||||||
|
}
|
12
templates/webapp_ex1/templates/_formhelpers.html
Normal file
12
templates/webapp_ex1/templates/_formhelpers.html
Normal 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 %}
|
26
templates/webapp_ex1/templates/entry.html
Normal file
26
templates/webapp_ex1/templates/entry.html
Normal 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>
|
Loading…
Reference in New Issue
Block a user