Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a...
Transcript of Python Programming€¦ · Flask is a micro web framework written in Python. It is classified as a...
Python Programming
Dr Diarmuid Ó Briain
Web developmentwith Flask
● 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
● 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
● 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
● 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
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
● 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
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
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
Jinja2 template
● Python HTML template engine
Jinja2 application
● Jinja application
Jinja2 development troubleshooting
Jinja2 template inheritance
● Jinja base template
Jinja2 template inheritance
● Jinja child template
Jinja2 template inheritance
● View of page generated.
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
Web Services RESTful API
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}
Web Services RESTful API
● RESTer for Mozilla
Running Flask in production
~/python3_flask$ pip3 install waitress
~/python3_flask$ ./waitress_serve.py Serving on http://0.0.0.0:8080
Exercise 17
Thank you