thoughtSpace
TwitterGithubRSS Feed

Note Space

Hints, cheat sheets and notes on code.

Home

Flask Crash Course

Posted on February 14, 2022
flask-crash-course

Flask 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.

To create instances of our Project class, we’re going to have to use the Django shell. The Django shell is similar to the Python shell but allows you to access the database and create entries. To access the Django shell, we use another Django management command:

Setting up a new Flask project, in conda prompt:

  1. mkdir development_folder
  2. cd development_folder
  3. python -m venv venv
  4. venv\Scripts\activate
  5. mkdir and cd into project_folder
  6. pip install Flask
  7. create init.py
  8. set FLASK_APP=flaskr
  9. set FLASK_ENV=development
  10. flask run
  11. Visit http://127.0.0.1:5000/
  12. flask init-db to initialize DB
  13. pip install -e . to install project in virtual environment
  14. When installed, can run from anywhere
  15. pip install pytest coverage install testing packages
  16. pytest to run tests
  17. coverage run -m pytest -- measure the code coverage of your tests
  18. coverage report --see coverage report in html

Project Structure

  • init.py tells Python to treat the directory as a Python package.
  • admin.py contains settings for the Django admin pages.
  • apps.py contains settings for the application configuration.
  • models.py contains a series of classes that Django’s ORM converts to database tables.
  • tests.py contains test classes.
  • views.py contains functions and classes that handle what data is displayed in the HTML templates.

Useful commands

    python manage.py shell
    python manage.py createsuperuser

Note Space © 2022 — Published with Nextjs

HomeTopicsLinksDefinitionsCommandsSnippetsMy works