Cookiecutter: A Tool for Project Templates

Overview

Cookiecutter is a Python-based tool designed to create language-agnostic project templates. It allows developers to generate new projects with a consistent structure and configuration by combining static boilerplate code with dynamic user input. This approach ensures that while the core structure of the projects remains the same, certain parts can be customized based on user preferences or project requirements.

Installation

Cookiecutter can be installed globally using pipx, which ensures that it is available across different environments. The installation process is straightforward, and once installed, the cookiecutter command becomes globally accessible:

installed package cookiecutter 1.7.3, Python 3.10.0
  These apps are now globally available
    - cookiecutter
done! ✨ 🌟 ✨

To verify the installation, you can check the version of cookiecutter using the following command:

$ cookiecutter --version

The version output should match the version specified during the installation.

Creating a Template

To create a project template using cookiecutter, you need to define a configuration file named cookiecutter.json. This file contains keys representing dynamic variable names and their corresponding default values. Here is an example of a simple configuration:

Listing 10.1 A small cookiecutter JSON configuration with two variables

{
    "variable_one": "green",   #1
    "variable_two": "blue"     #2
}
#1 variable_one defaults to the value "green".
#2 variable_two defaults to the value "blue".

These variables can be referenced throughout your project template. When generating a new project, cookiecutter will replace these variables with either the default values or user-specified values.

Using Cookiecutter

To generate a project from a template, run the cookiecutter command and provide the path to your project template directory. Cookiecutter will prompt you to accept the default values or enter custom values for the variables defined in the cookiecutter.json file:

Listing 10.2 Example output from running cookiecutter with two variables

$ cookiecutter python-project-template
variable_one [green]: red       #1
variable_two [blue]:            #2
#1 Variable name and default value, with alternative supplied
#2 You can press Enter to accept the default value.

For variables with a string as the default option, you can input any alternative value. Additionally, cookiecutter allows you to specify a list of possible values for a variable, which restricts the options to those listed:

Listing 10.3 Specifying a list of possible options for a cookiecutter variable

{
    "variable_one": "green",                   #1
    "variable_two": "blue",
    "variable_three": ["foo", "bar", "baz"]    #2
}
#1 Uses a string to provide a default value
#2 Uses a list to provide an enumeration of allowed options

When running the cookiecutter command with this configuration, the options for the third variable will be displayed, and you must choose one of them.

Visual Representation

The concept of cookiecutter can be likened to a real cookie cutter, where a template is used to stamp out several similarly shaped items. While the core shape remains consistent, the dynamic parts can be customized, much like decorating cookies with different icings and sprinkles.

Figure 10.1 Using cookiecutter to create new projects from a mix of static boilerplate and dynamic user input Figure 10.1 Using cookiecutter to create new projects from a mix of static boilerplate and dynamic user input