Using and Mastering Cookiecutter Transcripts
Chapter: Programmatic Cookiecutter: Using the API
Lecture: Executing the template
0:00 We've welcomed the user, we've gathered their inputs
0:03 about how they want to create the game and now it's time to come down here
0:06 and actually use our game info to create the game.
0:09 So first of all, let's start by saying something like this,
0:12 hey we're building the game with info.project_name and info.game_type
0:19 and notice how cool it is to be using named tuples, game type and so on.
0:25 Right, that's why we did that at the beginning.
0:28 Also, it can be important to use flush=True because if we get the system really busy
0:32 waiting on something it might actually not put that message out straight away,
0:35 so this is kind of like a hey we're busy hold on,
0:38 so if we want to make sure that gets straight up so we use a flush here,
0:41 now the first thing we need to do is locate the template, that folder right there.
0:46 Actually that folder right there, so our game is here, let me collapse templates,
0:50 so our game is here we want to go into templates and we want to go there.
0:54 Now in Python, we can say we can hold the directory using os.path.abspath()
0:58 and let's say we want the absolute path of os.path.dirname(), of this particular file.
1:05 Wherever this file lives on the hard drive, we want to get that directory and then,
1:11 we're going to say os.path.join(), and we're going to join the working directory
1:15 with the templates directory. Now it's easy to type /cookiecutter-use-api
1:21 but don't do that, because that will prohibit it from being cross-platform.
1:25 so you want to just let the join, put all the pieces together
1:28 so down here we'll say cookiecutter-use-api
1:33 Okay, so now we have our template and it's time to actually execute this
1:38 so it should be really straightforward to do, it's just one function call
1:43 but in order to do that, we have to come up here and import cookiecutter.main
1:48 so we're going to come down here and we're going to actually get back the project directory
1:52 so we'll say cookiecutter.main.cookiecutter kind of annoying,
1:58 all the cookiecutters in there but this is the create function and this is the package.
2:04 So first thing we're going to give it is the path to the template,
2:07 the next thing we're going to give it is to say no_input=True
2:12 don't ask the users anything, take either the defaults or what I've collected
2:16 I am about to give you here. We'll say output_directory=info.working_dir, right,
2:21 remember we asked the user where do you want to create the project,
2:24 we could have some defaults for them like active directory or whatever,
2:29 but this is what we did, so we have to give it the output directory
2:32 and then we're going to say the extra_context,
2:35 now this is the data that we've gathered about them,
2:38 so this is going to be a dictionary of things like project_name
2:41 this is what is going to be in here. So we need to specify project_name,
2:46 full_name and game_type. Okay, so we'll come down here
2:49 and say put something for that in a moment,
2:52 game type we'll put something and we're going to have full_name.
2:56 so we can get this from our little info, so we have full_name,
2:59 we've already collected that, up here we have info.project_name,
3:03 and that was a little funky, wasn't it, down here we'll have info.game_type,
3:09 little format, okay, it looks like we're about ready let's return project directory.
3:15 Okay, so if I run this, we should be able to- let's say that is not misspelled,
3:22 let's go ahead and run it. it's going to ask us for our location,
3:25 so let me find the location here, maybe we can make a thing called projects,
3:30 and I'll put it in there so I'll move the other one in there that my game as well,
3:35 okay this is what we're going to give it for the working directory, alright,
3:40 now notice, empty, let's go over here and run this, so the game maker runs,
3:46 it says welcome to the game creator, woohoo, we're going to create the game
3:49 either Hi-Lo, Pacman, or pong for you which one do you want?
3:52 Oh I want Hi-Lo, definitely Hi-Lo, I am going to call it super hi and kind of low.
3:57 Full path is going to be like this, but let's test it if I get it wrong,
4:01 remember, it will ask again, oh no that doesn't exist, let's try it again,
4:05 okay now we'll give it the path, boom, building the game super hi and kind of low,
4:12 of type Hi-Lo just a moment, and there it is,
4:16 so if we cd in there we could actually run our game as we saw before,
4:22 right, my name is Michael, I am going to guess it's going to be 43, 42, too high,
4:28 22, man I am lucky these days, okay, so our thing is working really well,
4:33 one final step that we could take is we could actually ask it to start our game
4:39 so hey we just created for you, we just started the game we created it, you want to play it?
4:43 So let's do that, let's go over here and up at the top,
4:48 so we have our project directory, and let's just give a little message here,
4:52 so we can say do you want to run, yes or no, we'll say we've just created the game
4:57 and let's wrap that around here create the game,
5:01 whatever the project name is /game.py, do you want to run it yes or no,
5:05 and then, we can of course generate the path and if they said yes,
5:09 or they just hit Enter to take the default, we could run that.
5:12 So because it's just a bunch of subprocessed mumbo jumbo
5:17 I am just going to paste it here so we are going to need to import subprocess
5:20 we are going to need to import stdin and stdout
5:24 because this is a console game and we want to take the input from our console
5:28 and feed it over to that process and as well take the input or the output of that
5:33 and show it to our users and the final trick is for this to work shell=False.
5:37 Okay, so let's try this one more time, and I am going to do it like this,
5:42 I am going to copy this path and I'll run it, copy this path
5:46 and I am going to run it in its full glory over here so we'll say python3 this,
5:50 woohoo welcome to the game creator,
5:53 we can create either Hi-Lo or Pacman or Pong, alright,
5:56 so let's create Hi-Lo again, I am going to call it way high
6:02 the full path is here, building the game just a moment,
6:05 alright we've created way high at this crazy long location here,
6:09 do you want to run it, I'll say yes so I'll take the default, boom,
6:13 straight into the game, just like that, right, you can't do that with Cookiecutter
6:16 but you can with the Cookiecutter API, awesome,
6:19 my name is Michael, and let's see if my luck continues,
6:23 oh too high, 11, 15, 18, 19, 20, 21, excellent, 21, now our app is done.
6:31 So our game is done and that was the creation.
6:34 Okay, so I am not necessarily saying this is the best experience we could create,
6:38 we could do way better, but by wrapping up the API,
6:42 by wrapping up the Cookiecutter API here
6:45 and by shipping with a template installed locally we can of course
6:49 run even if there is no internet connection,
6:51 we can run in a way that asks the users for all sorts of interesting data and inputs
6:56 in like you saw we could verify that you entered the correct thing
7:00 when it came to the directory like no no that directory doesn't exist try again,
7:05 things like that, that's is just not possible there,
7:08 so I think this is a really cool way if you're creating a project
7:11 and you are going to install some library
7:13 and say somebody is going to use the library
7:16 and they are going to say I'd like to start with some scaffold,
7:18 some starter thing and you've got an app that will do that,
7:21 I recommend you integrate Cookiecutter
7:23 as long as having the requirements to execute Cookiecutter
7:26 that is basically Python around
7:28 this is a really easy thing to do, it's great idea.