Create Your Own Cookiecutter

Kivy “Hello World”

Basic Application Code

Let’s edit the previously created Python file {{cookiecutter.repo_name}}.py and start off with a basic setup.

We need to have an additional cookiecutter variable for our app as PEP8 demands a CamelCase class name while the repository and package is supposed to have an all lowercase name. Please note the variable  kivy_version  which is mandatory for a kivy app. The kivy installation of your setup must be at least as high as the specified value so that the used features are available and the app works as expected. For any additional information concerning this please visit the official Kivy API.

Entry Point

It is common practice to have a main.py to launch the application. Create it as usual via  touch main.py  and insert the following code:

GUI Code

As the documentation in the snippet above suggests we have a separate file for the GUI. For this purpose Kivy has it’s own language named kvlang to create a user interface in a declarative way.

We create a new file with the kv fileextension and open it in an editor:

The first line should be the used Kivy version as I already mentioned previously. The kvlang docs explain why.

It is important to mention how the Kivy application detects a corresponding kv file. There are several ways of loading a file though:

  • By explicitly calling the Builder and pass the desired kv file to it
  • By using the application name in lower-case without ‘App’ at the end (if it ends with ‘App’ of course) for your kv file

We will stick to the latter to demonstrate another great feature of Cookiecutter namely hooks.

Cookiecutter Advanced Usage: Hooks

It is pretty to easy to tell Cookiecutter to run hooks either before or after generating the project by having scripts in a dedicated folder at the template root, in this particular case cookiedozer.

The following code renames the kv file after the project has been generated to follow the convention mentioned above, so HelloWorldApp.kv becomes helloworld.kv. Although the kivy source code itself accepts a kv file ending with App or app, I encourage you to stick to the docs. Hence our post gen script closely resembles the original implementation of  App.load_kv()  at https://github.com/kivy/kivy/blob/master/kivy/app.py.

For additional information please read the official Advanced Usage docs of Cookiecutter.

Add License File

This cookiecutter template is distributed under the terms of the MIT License and so should be the resulting app. To acquire this we can simply copy the license file to the root of the project.

After that we need to replace the current values by cookiecutter variables, so the beginning of the licence file should read as follows:

Create a Readme File

You may want to have a brief readme for your project. Let’s create a new file at project root level.

As usual you can use Cookiecutter variables to include the given data.

Update cookiecutter.json

As we have introduced a couple of new variables, we need to update  cookiecutter.json respectively. It is possible to template the cookiecutter.json values as well so that the defaults are updated as the user answers the prompts. Feel free to try it yourself, see Templates in Context Values.


Run Cookiecutter

That’s it, we can execute Cookiecutter now and pass our template to it. To do so run the following outside of our template dir:


Launch Application

Now run the app with python from the package dir level and see for yourself. 😉


This should be enough to get you started with Kivy and Cookiecutter. In the next part of this tutorial (http://www.hackebrot.de/python/extending-cookiedozer/) we are going to set up tests and docs along with Buildozer which deploys the app to your android device eventually.

The actual Cookiecutter template at https://github.com/hackebrot/cookiedozer generates a visually appealing app that features a sleek slide show displaying the passed values instead of a simple button. It supports links to be opened in your webbrowser as well as swipe gestures. Feel free to give it a try 🙂

cookiedozer01 cookiedozer02

 

 

Leave a Reply

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

Time limit is exhausted. Please reload the CAPTCHA.