File tree Expand file tree Collapse file tree 6 files changed +89
-0
lines changed
Expand file tree Collapse file tree 6 files changed +89
-0
lines changed Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 1+ body {
2+ width : 600px ;
3+ }
4+
5+ # button {
6+ padding-top : 20px ;
7+ }
Original file line number Diff line number Diff line change 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 %}
Original file line number Diff line number Diff line change 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 >
You can’t perform that action at this time.
0 commit comments