Methods to Spin Up a Project Structure with Cookiecutter

-

you’re anything like me, “procrastination” might as well be your middle name. There’s at all times that nagging hesitation before starting a brand new project. Just serious about establishing the project structure, creating documentation, or writing a good README is sufficient to trigger yawns. It looks like looking at a blank page for a dreaded school essay. But remember how much easier it gets once some helpful LLM (like ChatGPT) provides a starting template? The identical magic can apply to your coding projects. That’s where Cookiecutter steps in.

What Is Cookiecutter? 

Cookiecutter is an open-source tool that helps you create project templates. It’s language-agnostic and works with virtually any programming language (and even outside coding, must you need a standardized folder and file structure). With Cookiecutter, you may arrange all of the boilerplate files (like READMEs, Dockerfiles, project directories, or anything), then quickly generate latest projects based on that structure.

The Cookiecutter workflow consists of three fundamental steps:

  1. You define your project template.
  2. The user enters values for the variables you specify.
  3. Cookiecutter generates a brand new project, robotically filling in files, folders, and variable values based on the user’s input.

The next image illustrates this process:

Workflow of a cookiecutter template usage for “baking” latest projects in line with the predefined template. image by writer

1. Basic Computer Setup

You would like minimal programming skills to put in and use Cookiecutter. In case you can open a command line window, you’re good to go.

• On Windows, type “cmd” within the search bar and open the “Command Prompt.” 

• In case you haven’t already, install pipx with:

pip install pipx

Test your installation by running: 

pipx --version

In case you get a “command not found” error, add pipx to your PATH. First, find where pipx was installed: python -m site –user-base.

This might return something like /home/username/.local. Search for the folder containing pipx.exe (on Windows) or pipx (on macOS or Linux). If you might have no admin rights, the directory could be C:UsersusernameAppDataRoamingPythonPythonxxxScripts.

I had so as to add pipx to my path and in the event you don’t have admin rights, you will want to do it every time you begin a brand new terminal window. It’s due to this fact beneficial so as to add the situation permanently to your Environment Variables settings. Nevertheless, if this setting is behind admin privileges, you continue to can add

set PATH=C:UsersusernameAppDataRoamingPythonPythonxxxScripts;%PATH%

Or

set PATH=/home/username/.local/bin;%PATH%

Hopefully, you get a meaningful response for pipx --version now.

2. Installing and Setting Up Cookiecutter

Cookiecutter is distributed as a Python package, so you may install it with pipx:

  pipx install cookiecutter 

Or just run it on the fly with: 

  pipx run cookiecutter ...

Let’s walk through making a project template. In this instance, we’ll arrange a template for Streamlit apps (cookiecutter_streamlit_ml). 

3. Creating the Template Structure

Inside your cookiecutter_streamlit_ml folder, you would like these two key components:

• cookiecutter.json – a JSON file that defines the variables you would like users to fill in (project name, writer, Python version, etc.). 

• {{ cookiecutter.directory_name }} – A placeholder folder name defined using curly braces. This directory will contain your project’s structure and files. When the user creates a brand new project out of your template, Cookiecutter will replace this placeholder with the name they provided.

Example content of a Cookiecutter template folder. image by writer

Your cookiecutter.json might look something like this:

Example cookiecutter.json file. image by writer

First, you define variables in cookiecutter.json which are used throughout the generated project. At a minimum, you’ll need a variable for the project name.

For instance, I often reference my GitHub repository in documentation. Relatively than entering it repeatedly, I set a variable once and let Cookiecutter populate every instance robotically. Similarly, I don’t want to put in writing out my name in each readme or documentation file, so I set it firstly.

To avoid issues with Docker and ensure that the right Python version is specified, I prompt for the Python version at project creation, ensuring it’s utilized in the generated Dockerfile.

You may define default values for every field in cookiecutter.json. Cookiecutter will robotically replace every instance of {{ cookiecutter.variable }} in your template files with the user’s input. You too can use transformations like lower() or replace(‘ ‘, ‘_’) to avoid issues with spaces in directory names.

In my template, I prefer providing detailed instructions to users relatively than setting default values. This helps guide those that might skip reading the README and jump straight into project creation.

4. Constructing Out Your Template

Now begins the fun part, namely defining your template. You’re doing it once and for all, so it is worth it to spend a while on it, which can drastically reduce your project setup time in the long term.

First, create the folder structure on your project. This includes creating all folders that you simply expect to make use of in your project. Don’t worry, whatever is missing or seems to be superfluous might be edited within the actual project. For now, you’re merely creating the blueprint; the whistles and bells can be project-specific.

Example of predefined folder structure for future projects. This folder structure and all files can be instantiated once the user executes the cookiecutter command. image by writer

Once you might have your folders ready, you may populate them with files. These might be either empty and even have some content that you simply might otherwise continually copy-paste from other documents. In these files, seek advice from your cookiecutter variables wherever something must be set dynamically (e.g., the project name or the GitHub repo). Cookiecutter will robotically replace those placeholders with user inputs, which can be asked for during project setup. This spares you lots of tedious copy-paste work, particularly in your documentation files.

A part of the content of the Readme file, which can be instantiated if you “unbox” your cookiecutter template within the project. Within the spinoff project, the fields {{ cookiecutter. }} can be populated with the values provided during “unboxing”. image by writer

Lastly, deposit the entire cookiecutter_py_streamlit folder in your GitHub account, zip it, or leave it because it is. Either way, you may now …

5. Use your template

Once your template is prepared, making a latest project becomes a snap:

1. Open your terminal and navigate to where you’d wish to create the project. 

2. Run one among the next commands:

   • From GitHub: 

     pipx run cookiecutter gh:ElenJ/cookiecutter_streamlit_ml  (replace together with your repo)

   • From an area folder: 

     pipx run cookiecutter /path/to/template_folder 

   • From a zipper: 

     pipx run cookiecutter /path/to/template.zip 

3. Cookiecutter will ask you the questions defined in cookiecutter.json. Provide answers—or simply press enter in the event you’ve set default values. 

You’re asked to supply answers to the questions you defined within the cookiecutter template. The answers you provide can be used because the respective variables within the fields defined by {{ cookiecutter.variable }}. image by writer

4. Voilà 🎉 your latest project folder is generated, complete with folders, files, and references customized to your inputs.

Instantiated Readme with variables the user provided during setup. image by writer

You may synchronize your latest project with GitHub by either pushing it directly out of your IDE’s built-in Git functionality or by making a latest repo on GitHub (ensuring it’s empty and doesn’t include a Readme) after which moving your generated project folder there.

And that’s it! You’ve turned what was once a day-long chore right into a swift process and have immediately generated a lot of files waiting to be filled in together with your ideas. the brand new project, you actually must have a sense of a productive day. In case you’re still on the lookout for guidance on best practices, try the official Cookiecutter templates here.

And as at all times: Completely happy coding!

ASK ANA

What are your thoughts on this topic?
Let us know in the comments below.

0 0 votes
Article Rating
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Share this article

Recent posts

0
Would love your thoughts, please comment.x
()
x