Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Hello Flask world / Python web micro framework

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)


Back to
Day trip to Lancaster
Previous and next
or
Horse's mouth home
Forward to
Routing in Flask
Some other Articles
Hello Flask world / Python web micro framework
Day trip to Lancaster
What does Tcl do if you try to run a command that is not defined?
Tcl - a new example for data reformatting
Catching failed commands and not crashing the program in Tcl
Loading packages in your Tcl program
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4527_Hel ... ework.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb