![Page 1: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/1.jpg)
Python Programming
Dr Diarmuid Ó Briain
![Page 2: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/2.jpg)
Web developmentwith Flask
![Page 3: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/3.jpg)
● Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries.
Flask, web development in python
![Page 4: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/4.jpg)
● Werkzeug implements WSGI, the standard Python interface between applications and servers.
● Jinja is a template language that renders the pages your application serves.
● MarkupSafe comes with Jinja. It escapes untrusted input when rendering templates to avoid injection attacks.
● ItsDangerous securely signs data to ensure its integrity. This is used to protect Flask’s session cookie.
● Click is a framework for writing command line applications. It provides the flask command and allows adding custom management commands.
Flask, dependencies
![Page 5: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/5.jpg)
● Blinker provides support for Signals.● SimpleJSON is a fast JSON implementation
that is compatible with Python’s json module. It is preferred for JSON operations if it is installed.
● python-dotenv enables support for loading environment variables from .env files when running flask commands.
Flask, dependencies
![Page 6: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/6.jpg)
● Install the Python3 virtual environment, which serves to manage the dependencies for projects, both in development and in production. Then install Flask and its dependencies.
Install flask
$ sudo apt-get install python3-venv
$ python3 -m venv ./python3_flask
![Page 7: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/7.jpg)
Install flask virtual environment
$ tree python3_flask .├── bin│ ├── activate│ ├── activate.csh│ ├── activate.fish│ ├── easy_install│ ├── easy_install-3.7│ ├── pip│ ├── pip3│ ├── pip3.7│ ├── python -> python3│ └── python3 -> /usr/bin/python3├── include├── lib│ └── python3.7│ └── site-packages├── lib64 -> lib├── pyvenv.cfg└── share └── python-wheels
~$ cd python3_flask~/python3_flask$ . bin/activate
![Page 8: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/8.jpg)
● Requirements file – requirements.txt.
Requirements, dependencies
~/python3_flask$ cat requirements.txtblinker==1.4Flask==1.0.2itsdangerous==1.1.0Jinja2==2.10MarkupSafe==1.1.0python-dotenv==0.9.1simplejson==3.16.0Werkzeug==0.14.1
~/python3_flask$ pip3 install -r requirements.txt
![Page 9: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/9.jpg)
Create first Flask program
~/python3_flask$ ./hello_world_flask.py * Serving Flask app "hello_world_flask" (lazy loading) * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: on * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 314-551-068
![Page 10: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/10.jpg)
Create first Flask program
~/python3_flask$ cat << EOM > hello_world_flask_2.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
EOM
~/python3_flask$ export FLASK_APP=hello_world_flask_2.py
~/python3_flask$ export FLASK_DEBUG=1
~/python3_flask$ flask run
![Page 11: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/11.jpg)
Jinja2 template
● Python HTML template engine
![Page 12: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/12.jpg)
Jinja2 application
● Jinja application
![Page 13: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/13.jpg)
Jinja2 development troubleshooting
![Page 14: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/14.jpg)
Jinja2 template inheritance
● Jinja base template
![Page 15: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/15.jpg)
Jinja2 template inheritance
● Jinja child template
![Page 16: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/16.jpg)
Jinja2 template inheritance
● View of page generated.
![Page 17: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/17.jpg)
Web Services RESTful API
● Add to the required dependencies.
~/python3_flask$ cat << EOM >> requirements.txt Flask-HTTPAuth==3.2.4Flask-RESTful==0.3.6six==0.3.6EOM
~/python3_flask$ pip3 install -r requirements.txt
![Page 18: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/18.jpg)
Web Services RESTful API
![Page 19: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/19.jpg)
Web Services RESTful API
~$ curl http://localhost:5000
Fibonacci series calculatorThis calculates the fibonacci series for a number of elements.You need to put a number at the end, for example.
$ curl -X GET -H "Authorization: Token <secret>" http://localhost:5000/<number>
~$ curl -X GET http://localhost:5000/5Unauthorized Access
~$ curl -X GET -H "Authorization: Token wrongSecret" http://localhost:5000/5Unauthorized Access
~$ curl -X GET -H "Authorization: Token secret1" http://localhost:5000/5{"user": "Bob", "0": 0, "1": 1, "2": 1, "3": 2, "4": 3, "5": 5}
~$ curl -X GET -H "Authorization: Token secret2" http://localhost:5000/6{"user": "Alice", "0": 0, "1": 1, "2": 1, "3": 2, "4": 3, "5": 5, "6": 8}
![Page 20: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/20.jpg)
Web Services RESTful API
● RESTer for Mozilla
![Page 21: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/21.jpg)
Running Flask in production
~/python3_flask$ pip3 install waitress
~/python3_flask$ ./waitress_serve.py Serving on http://0.0.0.0:8080
![Page 22: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/22.jpg)
Exercise 17
![Page 23: Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries](https://reader030.vdocuments.us/reader030/viewer/2022040216/5f311c53105b26082c2f0aa7/html5/thumbnails/23.jpg)
Thank you