Python Flask Web Application

Prerequisites

Python v3.6+

Usually come the Ubuntu 18.04 by default. Check version:

$ python3 --version
Python 3.6.9

To create alias python -> python3, use this commands:

$ sudo update-alternatives --install /usr/bin/python python $(command -v python3) 1
update-alternatives: using /usr/bin/python3 to provide /usr/bin/python (python) in auto mode

Pip v3.6+

Install from package:

sudo apt-get update && sudo apt-get install python3-pip -y

Check version:

$ pip3 --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

Create alias pip -> pip3:

$ sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
update-alternatives: using /usr/bin/pip3 to provide /usr/bin/pip (pip) in auto mode

Virtualenv

Install from pip:

sudo pip install virtualenv

Initialize Project

Create a new project directory and initialize virtual environment:

virtualenv env

Activate virtual environment:

source env/bin/activate

Install Flask

pip install flask

Create app.py:

from flask import Flask

app = Flask(__name__)

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

if __name__ == "__main__":
  app.run()

Run the app:

python app.py

Go to http://127.0.0.1:5000/ 

image-1598845948724.png

References