Using and Mastering Cookiecutter Transcripts
Chapter: Creating Cookiecutter templates
Lecture: A bare template

Login or purchase this course to watch this video and the rest of the course contents.
0:01 So let's start this chapter by building some kind of template.
0:05 And this won't really be so much programming language template,
0:08 it's just to manage some files, we'll just distribute text files
0:11 as you saw on the data science repeatable data science thing,
0:14 these can be all sorts of stuff, HTML files, and LaTeX and Whatnot,
0:19 so just to keep things simple and neutral,
0:21 we'll just sort of manage a bunch of files here,
0:23 so notice there is nothing in this directory, but it's chapter 5
0:26 from the source code repository so you will be able to go and check this out.
0:29 So let's start over here, by just creating some structure,
0:34 so the first thing that we're going to need to do, so we want to create a folder,
0:37 and in this folder, this has a subset as a subdirectory and subcontent of this folder,
0:44 this is where the thing we give to the users goes.
0:47 Now, we have to give it a funky name,
0:50 it has to be {{cookiecutter.something}}
0:53 and notice this project name here is going to appear again.
0:56 Okay, and then next to this, we're going to need, right next to this one,
1:00 we are going to need to create a cookiecutter.json so we can say touch okay,
1:08 so here we have a cookiecutter.json and let me put this into Visual Studio Code,
1:17 we want to make some more files in here, so let's go in here
1:20 and we're going to make a file, I'll call this colors.txt, we'll play with that
1:26 throughout this section and we'll make another file called summary
1:29 and this called readme.md, md for Markdown, it's pretty common,
1:34 so the first thing we need to pay attention to is this cookicutter.json
1:38 so here what we put is basically a key value JSON object.
1:45 Okay, and the JSON object is going to contain some keys,
1:49 those keys both specify the default values
1:51 and the prompt we're going to actually run,
1:54 so let's start like this, and it already knows it's JSON, okay, that's good,
1:59 so we're going to come in here and we'll put things like project_name and then,
2:03 we could leave it blank, which is probably not a great idea,
2:06 let's give it a little default here, I'll call it
2:09 my_proj for now that is going to be the default,
2:12 they can change this name but that is what the default is,
2:16 let's also put in here creator, and I'll just put something like this, like your name.
2:20 We saw that we could put this into our default file,
2:23 and have this change and insert our name there,
2:28 so we can go ahead and do that in a little bit.
2:30 Let's just have one more thing, favorite_color, and let's just say
2:37 everybody's favorite color is blue but they can enter whatever they want.
2:40 So we have our project name here, let's go back to our readme,
2:43 and we'll say project and then in here we can use what is in our template,
2:48 so we can use these names as you can see in file structure,
2:52 and here is a directory, we can have a file named, whatever that is,
2:56 let's actually do that, that would be cool, so if we come in here,
2:58 create a file and call this project_name, we'd like to have it this way but of course,
3:03 it's going to be {{cookiecutter.project_name}}.txt and it will also be
3:10 project summary or something like that and again, inside these files,
3:14 we can have {{cookiecutter.project_name}}, project is like this
3:19 creator it's going to be similar but it's going to be creator, and so on.
3:25 So this projects will get named based on what we call the project name,
3:28 this file will be named as well, its contents will be changed so I'll have this over here,
3:33 let's put this like that in our readme and let's make this our little header
3:37 and we'll have this is the description of the project.
3:42 The creator, we'll put a little creator here, favorite color is,
3:52 and we'll do favorite color, okay, so I think we're pretty good,
3:57 I don't have anything in colors let's just put the favorite color here again,
4:00 just to make it appear in some places, you can have content that are unchanged,
4:04 you can just have plain text like this is plain text, so whatever we put here,
4:08 it'll just get copied over but it finds a Jinja2 expression
4:11 it's going to grab that and evaluate it.
4:14 Okay, so I think we have our basic project, let's have a look.
4:20 So if we look at it like this, we've got our cookicutter.json.
4:23 we've in our main project, now our main project is called just this,
4:28 is that right, let's move that really quick, that is probably not the best place,
4:32 so let's call this cookiecutter that is a convention to start a cookiecutter
4:36 and we'll call this colors, okay, and put that as a subdirectory,
4:40 okay, so this would be the root of our git repo,
4:44 we could point to it or we could actually just work with the project structure here,
4:49 so it looks like this and if we have let's say go to that cookicutter thing,
4:53 and just copy this directory here, we should be able to run this,
5:01 alright, so let's- so you don't want to run this in the cookicutter template,
5:05 that's not going to be good at all, what we got to do is we can run it,
5:09 actually I could create the project right next to it,
5:12 not very common but that is what we're going to do,
5:14 so we come over here and say cookiecutter and then
5:17 I just give it the path to the template, so I could say ./cookicutter-colors
5:21 I could give it that full directory, I copied if it was somewhere remote like on GitHub,
5:25 I could give it the URL and then we'd clone it
5:28 and then I wouldn't have to know its path anymore.
5:30 So let's go ahead and run this, clear the screen, go,
5:33 it's going to ask what is my project let's just call it the_first_color,
5:37 okay, that is going to be the name of our project, my name I'll type it in for now,
5:43 we'll see that we can change this creator here, and then,
5:47 let's just go with the default blue, I could put something else, but blue seems fine.
5:50 Great, now if we look at the tree, you can see
5:53 we have two sort of top subdirectories here,
5:57 and we have this one which has this Jinja expression project name,
6:02 colors, readme and this file whose name is also the Jinja expression
6:06 for the project name, and look what we actually created,
6:09 here is the directory named like that, and here is that file.txt that it was named.
6:13 So, let's have a look at the first, so if we come down, here, if we look at colors,
6:17 you can see this says blue and this is plain text
6:19 so blue is the expression I put that is the default value,
6:22 and this is plain that was unmodified text, this one was just an empty file,
6:27 and this one if we look at it, it will say project the_first_name,
6:31 we could even preview, project the first color, this is the description of project,
6:34 the creator Michael Kenendy's favorite color is blue.
6:37 So you see how all this works, so let's go back here to our JSON file and notice,
6:41 if I put this next to the thing we ran here, notice the three values we ask here,
6:47 project name, creator and favorite color, and notice right here,
6:50 those were the three prompts and their values with the default we were given,
6:54 so it's really this cookicutter.json that derives the user input and then after that,
6:59 it's handed off to basically just the folder structure
7:02 and the static files themselves, now, it turns out,
7:05 that we can actually have like in here you saw we had a Jinja expression,
7:09 for the creator and the project name, you could actually have expressions
7:13 that are evaluated in these things as well,
7:16 so you could do sort of conditional stuff that you can do with Jinja
7:19 and so we can get more advanced in what happens inside those files
7:22 and have further control over that,
7:25 But, even already, we have a very powerful creation mechanism, right,
7:30 we can create these files and rename them, move them around,
7:33 fill in default values, conditionally include or exclude fragments of code
7:37 using Jinja conditional statements, things like that.
7:41 So there you go, we've created our first template,
7:45 I suggest that if you want to practice this, go and create a template
7:48 for some project that you typically do, try to create this folder
7:52 it always has cookicutter-the name of the template and then the cookicutter.json
7:56 and then the subdirectory is always named as something to do with the project
8:01 or very often is named something to do with the project
8:04 and then you have all of your pieces underneath.
8:07 Okay, so give this a shot and we'll keep digging into this
8:10 to see many more cool features that Cookiecutter has in store for us.