Lightweight and limited components
- Jinja 2 template engine ( Building HTML ) - similar to DJANGO
- Werkzeug ( HTTP and routing )
- Development server and debugger
- Unit testing support
Installing the flask dependency
python -m pip install flask
-
Which module can my find my app(Unix) :
export FLASK_APP=flashcards.py
-
What environment ( dev enables debugger )
export FLASK_ENV=development
-
Start Command
flask run
subfolder static/
-
View - Functions already defined by @app.route functions
-
Jinja Templates
- Display data to users
- Generating HTML
- Calling templates from views(func)
- Passing Data from view to template
- Jinja variables
-
Data Model ( DB side )
- model layer
-
import the render_template and pass the template name
return render_template("welcome.html")
-
/templates folder is where the scan happens
-
{{message}} : Jinja variable in the html for eval
Value passed from the view(function)return render_template("welcome.html" , message="")
Refer to the model.py module
-
View(Function) Logic
-
Take Arg from Url
@app.route('/card/int:index')
-
Return HTTP Error
import abort -- abort(404)
-
Serving Data as REST API
-
-
Template Logic - Jinja For and IF statements
refer to card.html
- Creating Links to Cards
refer to @app.route('/api/**) in flashcards.py refer how jsonify is used on List obejcts
-
Not to Use flask server Use popular pythin httpserver : gunicorn
-
Run behind a Reverse Proxy (nginx)
apt install the following packages - nginx - python3 , python3-pip
- git clone
- sudo apt install gunicorn3 ( at system level and not pip )
pip3 install -r requirements.txt ( use Venv ) - gunicorn3 flashcards:app
This makes the server listen on port 8000 , but we want it to be on 80 on public ip
- Use nginx ( webserver runs on port 80 ) ( prevents DDOS )
Now run in Daemon mode after logout as well
gunicorn3 -D flashcards:app
Remove the default home page of the webserver at :
cd /etc/nginx/sites-available
sudo rm default
Create a new Site ( refer to e.g config from gunicorn.org)
vi default
server {
listen 80;
server_name example.org;
access_log /var/log/nginx/example.log;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Restart the nginx service
sudo service nginx restart
In your venv install the prometheus-flask-exporter
pip install prometheus-flask-exporter