Python Flask – Web Application

Create an environment

Create a project folder and a venv folder within:

  • Ubuntu/ Macos
$ mkdir myproject
$ cd myproject
$ python3 -m venv venv

# activate the environment
$ . venv/bin/activate

  • Windows
> mkdir myproject
> cd myproject
> py -3 -m venv venv

# activate the environment
> venv\Scripts\activate

Install Flask

Within the activated environment, use the following command to install Flask

$ pip install Flask

Hello World!

Create project web page show “Hello, World!”

  1. Create file hello.py
# hello.py

from flask import Flask
app = Flask(__name__)

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

2. Run website Flask

Ubuntu/ MacOS

$ export FLASK_ENV=development
$ export FLASK_APP=hello
$ flask run
  * Running on http://127.0.0.1:5000/

Windows

> set FLASK_ENV=development
> set FLASK_APP=hello
> flask run
  * Running on http://127.0.0.1:5000/

Open browser and go to http://127.0.0.1:5000/

Hello, World!