Skip to content

Exercise #1_01 | Flask Web Server Basics

Genie52 edited this page Jun 18, 2019 · 5 revisions

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. However, Flask supports extensions that can add application features as if they were implemented in Flask itself. Extensions exist for object-relational mappers, form validation, upload handling, various open authentication technologies and several common framework related tools. Extensions are updated far more regularly than the core Flask program.

Useful Links for more information:

Flask Homepage | Flask on Wikipedia | Web Dev with Python(Flask) and JS - from Harvard Uni - Videos | Flask on Tutorialspoint

Exercise 01 - Flask Web Server Basics

With this exercise we want to show how easily is to build your own Flask web server and run the first web page (created in python - not using any HTML file yet!)

#01 - Start Atom

#02 - Drop Labs TCE19 directory in Atom

#03 - This will open the folder as Atom project (like this):

#04 - Open 01-Flask-Basic-Finish.py from 01 - Flask Basics directory in Atom. You will notice that all files have FINISH or START at the end.

  • FINISH ones is the code we want to have at the end of the exercise - also we can always start that code first so we can also see what will be the end result for a user.
  • START ones is the template code that we want to expand with the additional snippets of code - this is where you learn the most important aspects for the current exercise.

In the example above you can see that FINISH file has a placeholder with the guide where to paste the code AND in this case the code is already there.

#------- PASTE CODE HERE!! <<< START >>> ------------------
def index():
    return '<h1>Hello Tabloids from TCE 19 Berlin!</h1>' #this is actual HTML code!
#------- PASTE CODE HERE!! <<< END >>> ------------------

when you open the START file you will find the space for you to write/paste the code

#------- PASTE CODE HERE!! <<< START >>> ------------------


#------- PASTE CODE HERE!! <<< END >>> ------------------

#05 - Open 01-Flask-Basic-Start.py in Atom to see where you need to write/paste the code

#06 - Write (preferred way to make this stick with you) or copy/paste the following code in the designated area of 01-Flask-Basic-Start.py file

def index():
    return '<h1>Hello Tabloids from TCE 19 Berlin!</h1>' #this is actual HTML code!

Example:

If for some reason you are not able to make your code work properly - do not worry. As said in the beginning you can always open the file with FINISH at the end for the given exercise and you will have a working file. In this case it would be 01-Flask-Basic-Finish.py

#07 - Once you have copy/paste the code and saved it, you have now a ready file to start - 01-Flask-Basic-Start.py (it's no longer empty!). To do that you must be in the folder where the file is saved and run it with the command:

python 01-Flask-Basics-Start.py

Example:

and if everything goes well the result is:

That means we just have a living, working, amazing web server running on http://127.0.0.1:5000/ or http://localhost:5000/

And all of that with just 7 LINES OF CODE!!

When done you can press CRTL + C in CMD window with Flask running to quit the Flask server.