Using and Mastering Cookiecutter Transcripts
Chapter: Programmatic Cookiecutter: Using the API
Lecture: Executing the template

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


Talk Python's Mastodon Michael Kennedy's Mastodon