Project Template
A project template is a predefined structure and set of configurations that facilitate the creation of new projects with a consistent setup and organization. It includes both static boilerplate and dynamic values that can be customized for each new project. This ensures that projects maintain a uniform structure, making them easier to manage and understand.
Overview of Project Template Structure
The structure of a project template is crucial for its functionality. A typical project template, such as a Python package template, is organized as follows:
Listing 10.8 project-template-tree
python-package-template #1
├── cookiecutter.json #2
└── {{cookiecutter.package_distribution_name}} #3
├── <package files>
└── ...
#1 The project template root directory
#2 The cookiecutter configuration lives in the template root.
#3 The package root directory lives inside the project template root directory.
In this structure:
- The root directory contains the
cookiecutter.jsonfile, which is the configuration file for the template. - The package root directory is nested within the project template root and contains the actual package files.
Creating a Cookiecutter Configuration
Creating a cookiecutter configuration involves setting up a cookiecutter.json file. This file contains keys as dynamic variable names and values as default values. When generating a new project, users are prompted to provide input for these variables, allowing customization of the project.
Prompting for User Input
To create a project from a template, the cookiecutter command is used. This command prompts users to accept default values or provide custom values for the dynamic variables defined in the cookiecutter.json file. Here is an example of how this process works:
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 list of values, users must select from the available options:
Listing 10.4 Example output from running cookiecutter with a mix of variable types
$ cookiecutter python-project-template
variable_one [green]:
variable_two [blue]:
Select variable_three: #1
1 - foo #2
2 - bar
3 - baz
Choose from 1, 2, 3 [1]: #3
#1 Indicates you must make a selection
#2 Displays all available options
#3 Enter the index of the option, or default to the first one.
Building on Previous Values
One of the powerful features of cookiecutter is the ability to build on previous values. This means using one value to generate another related value. For instance, a distribution package name can be used to generate import package name options:
Listing 10.6 Output from a cookiecutter configuration with dependent variables
$ cookiecutter python-project-template
distribution_package_name [my-python-package]: #1
Select import_package_name: #2
1 - mypythonpackage
2 - my_python_package
Choose from 1, 2 [1]:
#1 The value chosen here is used to generate later values.
#2 These values are generated from the first variable.
Extracting a Project Template from an Existing Project
To create a project template from an existing project, you need to set up a directory structure that includes a cookiecutter.json file and a copy of the original project. Dynamic values in the project should be replaced by placeholders, allowing for customization when the template is used to generate new projects.
Exercise: Creating a Cookiecutter Template
An exercise in creating a cookiecutter template involves configuring the cookiecutter.json file to prompt for various project-specific values and replacing hard-coded references with placeholders. This process helps in understanding the flow of variables and the use of Jinja2’s syntax in templates.
FAQ (Frequently asked questions)
How do you extract a project template from an existing project?
What is a project template?
What is the structure of a project template in cookiecutter?
How do you create a cookiecutter configuration?
How does cookiecutter prompt for user input?
How can you build on previous values in a project template?
What does Exercise 10.1 involve in creating a cookiecutter template?