22.3. Chapter Practice

Just like last chapter, we will create a small project and update it as we go along. Follow the instructions below to get started.

  1. Open your local_practice folder in Visual Studio code. Add a new directory called flask_logic.

  2. From the File menu, open flask_logic in your workspace. Initialize the directory as a new Git repository.

  3. Use the terminal to set up and activate a new virtual environment for the project. This time, we will name the environment flask-env.

    Mac:
    $ python3 -m venv flask-env
    $ . flask-env/bin/activate
    
    Windows:
    $ py -3 -m venv flask-env
    $ . flask-env/Scripts/activate
    
  4. Install Flask with pip install flask.

  5. Use the buttons in the left panel to add the files and folders shown in the filetree below. For now, leave the Python, HTML, and CSS files empty.

    Filetree with the static, templates, and flask-env folders. Also, files style.css, main.py, and checkbox_form.html.

22.3.1. Pro Tip!

Since we are using version control, Git tracks every file we put into the project folder. This includes the thousands of small files created when we set up the virtual environment and install Flask. Since we won’t change any of these ourselves, tracking them clutters up our repository. These files should be ignored when we save and commit changes. Fortunately, Git allows us to create a list of items that can be skipped.

Tip

As a general rule, we should NOT track files that we didn’t write ourselves.

  1. In Visual Studio Code, add one more file to the project folder. Call it .gitignore. Any file or directory names included in this document will be left out of the version control.

  2. Open .gitignore in the workspace, then paste the following items.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    flask-env/
    
    *.pyc
    __pycache__/
    
    instance/
    
    .pytest_cache/
    .coverage
    htmlcov/
    
    dist/
    build/
    *.egg-info/
    
  3. Line 1 is the name of the directory for the virtual environment. Once we save, flask-env turns gray in the file tree. The folder will no longer be tracked by Git.

    Project filetree indicating that flask-env is no longer being tracked.
  4. For different projects, change line 1 to match the name used for the virtual environment.

  5. Save and commit your work.