Research on Flask Framework - bounswe/bounswe2022group7 GitHub Wiki
Flask Framework
Basic Description
Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. However, Flask supports extensions that can add application features as if they were implemented in Flask itself.
Known companies that use Flask are Pinterest and LinkedIn.
More info about Flask can be found in its documentation page.
Advantages
- Flask is easier to understand and implement
- Integrated support for testing
- Flexible, you can only have the features you want thanks to its microframework approach
- Scalable, you can build a simple app and grow it later
- Helpful and well written documentation
Disadvantages
- Harder to maintain with extensions
- Community is smaller compared to Django
- Not suitable for big applications
- Need to manually add extensions about ORM, Authentication
Tutorials
We will create a simple flask app for tutorials, with external tutorial links added for more coverage of features. We will follow Flask's own documentation and tutorials but omit the details, for that its documentation page or other resources can be checked.
Other Resources:
Setting up Flask
When setting up/installing Flask it is strongly recommended to use virtualenv
. To install virtualenv:
pip install virtualenv
Then after installation, we will create a virtual environment named testenv
:
mkdir test-env
cd test-env
virtualenv testenv
Then to activate the environment
source testenv/bin/activate
After creating and activating our environment we will install flask
pip install flask
Creating the application
We will create a simple "Hello, World!" application to show how to populate/create your app. We will name our file as hello.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
Running
To run our app we will export the FLASK_APP environment variable ass our app's name hello
and run the framework.
export FLASK_APP=hello
flask run
Also we can set FLASK to development mode to unlock debug/development features with:
export FLASK_ENV=development
More info can be found in Flask's quick start page
Testing
For testing, generally pytest
framework is used which can be installed with:
pip install pytest
After installing pytest we will create a new file called test_targetfilename.py
which pytest will find automatically. For this tutorial we will use test_hello.py
.
from hello import app
def test_hello():
response = app.test_client().get('/')
assert response.status_code == 200
assert response.data == b'Hello, World!'
After saving the test_hello.py
file, running the command py.test
will automatically run the tests.
More info about testing can be found in this Flask documentation page
Example apps
There are many example applications implementing Flask framework in github. These are some repositories implementing simple Flask apps: