Scripting >> Python >> Frameworks >> Flask >> Getting started with Flask


Example for RHEL 7.8


## assuming we already have python3 installed (/usr/bin/python3)
## we will use virtualenv for the user throughout
## the app will run as non-root user

[myfaqbase@rhel7server1 ~]$ python3 -V
Python 3.6.8

[myfaqbase@rhel7server1 ~]$ python3 -m venv venv

[myfaqbase@rhel7server1 ~]$ source venv/bin/activate

(venv) [myfaqbase@rhel7server1 ~]$ pip -V
pip 9.0.3 from /home/myfaqbase/venv/lib64/python3.6/site-packages (python 3.6)

(venv) [myfaqbase@rhel7server1 ~]$ pip install wheel
Collecting wheel
...
Installing collected packages: wheel
Successfully installed wheel-0.37.0

(venv) [myfaqbase@rhel7server1 ~]$ pip install flask
Collecting flask
  Using cached https://files.pythonhosted.org/packages/54/4f/1b294c1a4ab7b2ad5ca5fc4a9a65a22ef1ac48be126289d97668852d4ab3/Flask-2.0.1-py3-none-any.whl
Collecting Jinja2>=3.0 (from flask)
  Using cached https://files.pythonhosted.org/packages/80/21/ae597efc7ed8caaa43fb35062288baaf99a7d43ff0cf66452ddf47604ee6/Jinja2-3.0.1-py3-none-any.whl
Collecting itsdangerous>=2.0 (from flask)
  Using cached https://files.pythonhosted.org/packages/9c/96/26f935afba9cd6140216da5add223a0c465b99d0f112b68a4ca426441019/itsdangerous-2.0.1-py3-none-any.whl
Collecting click>=7.1.2 (from flask)
  Using cached https://files.pythonhosted.org/packages/76/0a/b6c5f311e32aeb3b406e03c079ade51e905ea630fc19d1262a46249c1c86/click-8.0.1-py3-none-any.whl
Collecting Werkzeug>=2.0 (from flask)
  Using cached https://files.pythonhosted.org/packages/bd/24/11c3ea5a7e866bf2d97f0501d0b4b1c9bbeade102bb4b588f0d2919a5212/Werkzeug-2.0.1-py3-none-any.whl
Collecting MarkupSafe>=2.0 (from Jinja2>=3.0->flask)
  Using cached https://files.pythonhosted.org/packages/fc/d6/57f9a97e56447a1e340f8574836d3b636e2c14de304943836bd645fa9c7e/MarkupSafe-2.0.1-cp36-cp36m-manylinux1_x86_64.whl
Collecting importlib-metadata; python_version < "3.8" (from click>=7.1.2->flask)
  Using cached https://files.pythonhosted.org/packages/71/c2/cb1855f0b2a0ae9ccc9b69f150a7aebd4a8d815bd951e74621c4154c52a8/importlib_metadata-4.8.1-py3-none-any.whl
Collecting dataclasses; python_version < "3.7" (from Werkzeug>=2.0->flask)
  Using cached https://files.pythonhosted.org/packages/fe/ca/75fac5856ab5cfa51bbbcefa250182e50441074fdc3f803f6e76451fab43/dataclasses-0.8-py3-none-any.whl
Collecting typing-extensions>=3.6.4; python_version < "3.8" (from importlib-metadata; python_version < "3.8"->click>=7.1.2->flask)
  Using cached https://files.pythonhosted.org/packages/74/60/18783336cc7fcdd95dae91d73477830aa53f5d3181ae4fe20491d7fc3199/typing_extensions-3.10.0.2-py3-none-any.whl
Collecting zipp>=0.5 (from importlib-metadata; python_version < "3.8"->click>=7.1.2->flask)
  Using cached https://files.pythonhosted.org/packages/92/d9/89f433969fb8dc5b9cbdd4b4deb587720ec1aeb59a020cf15002b9593eef/zipp-3.5.0-py3-none-any.whl
Installing collected packages: MarkupSafe, Jinja2, itsdangerous, typing-extensions, zipp, importlib-metadata, click, dataclasses, Werkzeug, flask
Successfully installed Jinja2-3.0.1 MarkupSafe-2.0.1 Werkzeug-2.0.1 click-8.0.1 dataclasses-0.8 flask-2.0.1 importlib-metadata-4.8.1 itsdangerous-2.0.1 typing-extensions-3.10.0.2 zipp-3.5.0


## create our flask web app as flaskhello.py, the contents as shown below

(venv) [myfaqbase@rhel7server1 ~]$ cat flaskhello.py
#!venv/bin/python
from flask import Flask
app = Flask(__name__)

@app.route("/")
def homepage():
    return "<p>Hello, world</p>"

if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=3000)



## set it to executable and run the app

(venv) [myfaqbase@rhel7server1 ~]$ chmod +x flaskhello.py
(venv) [myfaqbase@rhel7server1 ~]$ ./flaskhello.py
 * Serving Flask app 'flaskhello' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://10.0.2.15:3000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 627-792-629


## test the web app by loading http://localhost:3000 in firefox web browser