Flask Python

Flask Tutorial Series – 1 | Hello World

This is the first tutorial of Flask Tutorial series, in this we are going to setup our flask environment and create a small web app to display Hello World on the browser.

We need a Python Package named Flask to run our Flask Application.  You can check whether that package exists in your system or not by executing below command, otherwise we need to install it.

python -c "import flask"

Above syntax should not throw an exception – Module not found, if it is already there in the system.

If the package does not exist in your system, you can install it by using pip.

pip install flask

Or you can install it in using virtualenv, this will create a virtual environment. Here venv is the name of the virtual environment.

[an.choudhary@jarvis blog]$ virtualenv venv
Installing setuptools, pip, wheel...done.

[an.choudhary@jarvis blog]$ source venv/bin/activate
(venv) [an.choudhary@jarvis blog]$ pip install flask
(venv) [an.choudhary@jarvis blog]$ python -c "import flask"

Once we have install flask package, we will create one package in our project directory as app.

Make sure you are in the project directory, here the project directory is blog

(venv) [an.choudhary@jarvis blog]$ mkdir app

Code for __init__.py in app(package) will be –

blog/app/__init__.py

from flask import Flask
app = Flask(__name__)

from app import routes

In this we have imported Flask module to create an app object,  __name__ is a python variable which will be set to the name of the module in which it will be used. This will load all the resources like templates, static files, etc.

After that, we have imported the routes which we haven’t created yet.

Here please don’t get confuse via app(from app import routes), here app is the package name.

routes.py contains the different URLs that the application will handle. URLs are mapped with the functions so that Flask Server understand which function need to be executed when a client requests a given URL.

blog/app/routes.py

from app import app

@app.route('/')
@app.route('/home')
def index():
    return "Hello World"

@app.route lines above the function are decorators, a unique feature of the Python language. A decorator modifies the function that follows it. In this case, the @app.route decorator creates an association between the URL given as an argument and the function. In this example there are two decorators, which associate the URLs / and /home to this function. This means that when a web browser requests either of these two URLs, Flask is going to invoke this function and pass the return value of it back to the browser as a response.

Now to run this application, we need to create the entry point.

blog/run.py

from app import app

if __name__ == '__main__':
    app.run(debug=True)

And execute this run.py

(venv) [an.choudhary@jarvis blog]$ python run.py 
 * Serving Flask app "app" (lazy loading)
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 279-287-454
127.0.0.1 - - [29/Apr/2018 18:29:13] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [29/Apr/2018 18:29:13] "GET /favicon.ico HTTP/1.1" 404 -

Now open up the browser and run the below

http://localhost:5000/

Youtube Link  – https://youtu.be/nYdP01VmuGo