aboutsummaryrefslogtreecommitdiff

flask-tutorial

https://flask.palletsprojects.com/en/3.0.x/tutorial

https://flask.palletsprojects.com/en/3.0.x/tutorial/next/

Installation

python -m venv .venv
source .venv/bin/activate

pip install -r requirements.txt
flask --app ustayml run --debug

Build

pip install build
python -m build --wheel

Deploy: https://flask.palletsprojects.com/en/3.0.x/deploying/

Learning resources

Concepts:

  • flask.g: Store data. Unique for each request
  • flask.current_app: Link to Flask app
  • flask.open_resource: From app package path
  • request: HTML request???
  • Factory function > registered functions and blueprints.
  • Blueprint: groups views & other code
  • View: function that returns HTML
  • flask.session: dict that stores data across requests (cookies) (securely signed with SECRET_KEY)
  • endpoint: name associated with a view, <blueprint_name>.<view_function_name>
  • Templates: Used to render HTML. Uses Jinja template library. Autoscapes any data that is rendered in HTML templates, so it's safe to render user input.
  • {{ }} denotes expressions (output) (similar to python)
  • {% %} denotes control flow statements (similar to pseudo-code)
  • {# #} denotes a comment
  • Automatically available: g, url_for, request, and more
  • In base.html you define "blocks" placeholder which are later defined in other templates that extend base.html

Gotchas:

  • Remember to call db.commit() after modifying DB (DML).

Browser warnings and errors:

  • https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#samesitesamesite-value

Discussion

Pros:

  • Flask is AWESOME, it's simple, it's elegant, it's enjoyable to code with. Following the tutorial provides you all the basic tools to begin creating projects with flask.
  • It's secure, provides you with cryptographic signature for cookies and escapes HTML transparently to the developer.

Cons:

  • Maybe scalability and performance is lower compared to PHP or Javascript web applications.

Next Steps

  • https://www.sqlalchemy.org/
  • https://stackoverflow.com/questions/10434599/get-the-data-received-in-a-flask-request