Flask is a microframework for web appliactions - in other words, it provides the structure for you to write web based applications on your server in Python without the need to get involved with detailed coding common to most applications - allyou need to is to take the framework provided and fill in the extra bits.
Flask runs under Python 2.6 and later (Python 2 series) and Python 3.3 and later (Python 3 series). Installation is operating system dependent - for our training machines, we used the following:
apt-get install python pip
pip install flask
Modern convention is to write web applications using an "MVC" - "Model View Controller" structure. This allow your code to be segmented into logical sections which will be maintained on different cyles by different teams, may be shared between different applications, and may even be in different languages / formats.
•
Model - the business logic of the application
•
View - the template to be completed / look and feel of the page
•
Controller - top code that runs the model and renders the view
Although convention is to talk "MVC", there's a fourth element that's needed - and indeed that's needed first
•
Router - How particular URLs map onto particular controllers
More about the principles at
http://www.wellho.net/resources/Q915.html.
Here's a first "hello world" Flask application - to run on your server:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host="0.0.0.0",port=3333)
Source code
[here] for download
If you leave out the port, it runs on port 5000; if you leave out the host, it runs on 127.0.0.1 meaning it's only got local access - that fine (and more secure) for testing, but we find during training that separating server and client, and showing how multiple clients can access the same server, helps teach the principles and is closer to the final target.
Running the server (our host called "frankfurt" on 192.168.200.45):
trainee@frankfurt:~/flask$ python first
* Running on http://0.0.0.0:3333/ (Press CTRL+C to quit)
and leave it running.
From a client machine on the network, visit http://192.168.200.45 and you should see you first Flask web page:
and you'll see a log line on the server:
192.168.200.42 - - [11/Oct/2015 10:04:27] "GET / HTTP/1.1" 200 -
if you try for a URL for which you have not defined a route, you'll get a "404" error in your log:
192.168.200.42 - - [11/Oct/2015 10:14:08] "GET /french.html HTTP/1.1" 404 -
and see an error page in your browser:
and if your Python script has a problem at run time (ours hasn't), you'll get a 500 "Internal Server Error:
192.168.200.42 - - [11/Oct/2015 10:14:09] "GET / HTTP/1.1" 500 -
and see this on your browser:
All that really says is "it didn't work" - the equivalent of the old blue "screen on death" with no clues at all as to what caused the problem. It defaults that way for security purposes - but you'll see in the next article how you can turn debugging on and get more data about the problem (you really don't want the public to see this stuff!)
(written 2015-10-11)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y307 - The Flask micro webdevelopment framework for Python [4528] Routing in Flask - (2015-10-11)
[4529] Flask - calling in the templating engine - (2015-10-11)
[4530] Requests in Flask - (2015-10-11)
[4531] Jinja2 - Flask templating - (2015-10-11)
[4532] Flask - first forms and responses with wtforms - (2015-10-12)
[4533] Sessions in flask - (2015-10-13)
[4534] Flask - complete MVC site to navigate around a data set - (2015-10-13)
[4538] Flask and unittest - hello web app test world - (2015-10-15)
[4540] Unittest of a Flask application including forms - (2015-10-15)
Q915 - Object Orientation and General technical topics - Principles of Model - View - Controller [687] Presentation, Business and Persistence layers in Perl and PHP - (2006-04-17)
[2199] Improving the structure of your early PHP programs - (2009-05-25)
[2612] The Model, View, Controller architecture (MVC) - what, why and how. - (2010-02-01)
[3237] Using functions to keep look and feel apart from calculations - simple C example - (2011-04-09)
[3454] Your PHP website - how to factor and refactor to reduce growing pains - (2011-09-24)
[3624] Why do we need a Model, View, Controller architecture? - (2012-02-25)
[3705] Django Training Courses - UK - (2012-04-23)
[3919] What is a web framework? - (2012-11-10)
[4010] Really Simple Rails - (2013-02-17)
[4066] MVC and Frameworks - a lesson from first principles in PHP - (2013-04-19)
[4114] Teaching CodeIgniter - MVC and PHP - (2013-06-12)
[4320] An example of Model-View-Controller techniques in a Perl / CGI script - (2014-11-20)
[4391] Refactoring Perl applications to give them a rosy future - (2015-01-11)
[4641] Using an MVC structure - even without a formal framework - (2016-02-07)
[4691] Real life PHP application using our course training MVC example - (2016-06-05)
Some other Articles
Hello Flask world / Python web micro frameworkDay trip to LancasterWhat does Tcl do if you try to run a command that is not defined?Tcl - a new example for data reformattingCatching failed commands and not crashing the program in TclLoading packages in your Tcl program