Home
Flask Crash Course
Posted on February 14, 2022
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:
- mkdir development_folder
- cd development_folder
- python -m venv venv
- venv\Scripts\activate
- mkdir and cd into project_folder
- pip install Flask
- create init.py
- set FLASK_APP=flaskr
- set FLASK_ENV=development
- flask run
- Visit http://127.0.0.1:5000/
- flask init-db to initialize DB
- pip install -e . to install project in virtual environment
- When installed, can run from anywhere
- pip install pytest coverage install testing packages
- pytest to run tests
- coverage run -m pytest -- measure the code coverage of your tests
- 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