Create Your Own Cookiecutter

Are you doing the same steps over and over again every time you start a new programming project?

More particularly when creating a PyPI compliant application which features a test suite along with auto-generated documentation?

If so, you should definitely check out Cookiecutter. It works like a charm! πŸ˜‰

Cookiecutter is a cross-platform CLI tool which queries the user for input such as a project name and uses this data to fill the template specified on invocation regardless of the target programming language. There are already plenty of cookiecutters available online including a fully-fledged PyPackage template by the creator of Cookiecutter @audreyr herself.

The following is a tutorial on how to create your very own template. The resulting project will be a fully functional Kivy app, ready to be deployed to your android mobile device.

I am using Python 2.7 on Ubuntu 14.04 (Trusty Tahr) 64-bit but it should work with slight adaptations on your OS of choice. Please note though that albeit Kivy being cross-platform the tool at hand for creating the apk named Buildozer is not, from what I know. It is in early development stage and only supports Linux at the time of this writing. For debugging purposes feel free to check out the Kivy Launcher, it is somewhat of an alternative.


 

Install Cookiecutter

Given that you have pip installed you can simply run the following:

Please see the official docs for additional information on how to set up Cookiecutter.


 

Create a Cookiecutter Project

GitHub Repository

Cookiecutter is capable of dealing with remote templates, which is why I tend to create a GitHub repo as a starting point.

https://github.com/hackebrot/cookiedozer

I like to leave it to GitHub to add a license file (MIT in this particular case), a readme as well as a gitignore for python projects.

If you decide to use a local template instead, go ahead and create a directory via:

Project Root

After cloning the repo to your machine, you can inspect the current folder structure. I use the following command to that end:

You should see following in your terminal:

Since I personally prefer reStructuredText over markdown, I convert the README file accordingly. For now it only contains the description as provided during the repo creation.

I will leave out additional git commands in this tutorial for the sake of simplicity.


 

Template Structure

Cookiecutter searches for a specific pattern inside of text files as well as in directory and file names. It will replace any occurrence with the value specified by the user based on a certain variable name.

Create Package

You need to create two nested folders. One will be the project root and the other one is the Python package root containing the application code.

Next we want to create the compulsory init to have a package as well as a module for our application:

Feel free to set the encoding to unicode for both of the files and insert meta data into __init__.pyΒ :

Cookiecutter.json

The variables we used in the previous section, such asΒ  repo_name, need to be registered in a file namedΒ  cookiecutter.json which is a simple config based on which the user will be questioned later on. You can set reasonable default values for the variables while you’re at it.

Open the resulting file in your favourite editor and insert the following:


On the next page of this tutorial, we are going to implement a simple Kivy application and learn how to setup post generation hooks with Cookiecutter.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.