Using and Mastering Cookiecutter Transcripts
Chapter: Programmatic Cookiecutter: Using the API
Lecture: Concept: Cookiecutter API

Login or purchase this course to watch this video and the rest of the course contents.
0:01 The Cookiecutter API is quite rich, but also simple to use. Basically, the way it works is you ship your app with a template in some way,
0:12 or I suppose if you care to depend on the internet you can even ship it straight off GitHub, right, just use the URL.
0:18 And we want to call one function, cookicutter.main.cookiecutter So, we just call this, we need to pass it an absolute path to our template
0:27 so it knows where that is and in Python it's really easy, you just use the dunder file and you can sort of work out a relative path
0:33 with os.path module; now you want to be sure to set no_input to True. Otherwise Cookiecutter is going to ask the questions
0:41 that are in cookiecutter.json well, you don't want that, right, this is your application, it can ask those questions
0:48 and it can be much smarter and conditional and retrying about the answers that it gets. You also need to specify where this is going to output the data
0:57 so we're going to set-up a directory here we've already computed this actually we asked the user where that was and verified it,
1:03 and then you need to pass along the questions that Cookiecutter itself would ask in the extra context here.
1:10 So we've gathered all of the questions that would have been asked what is a project_name, full_name and game_type we've asked the user
1:17 what that was, and then we just passed it along, so Cookiecutter just takes those and applies them directly and doesn't ask any questions.
1:26 And then what comes back is the project directory and we can open that up, we can execute code in there, whatever.
1:31 We also want to make sure that we're leveraging the defaults, so if somebody is using Cookiecutter already, and they have a default file,
1:38 we want to be sure to use that, so we can import cookiecutter.config
1:42 and then call .get_user_config(), alright, and that will give us this whole dictionary back, everything about them as far as Cookiecutter is concerned,
1:51 Cookiecutetr directory, the replay directory, even their aliases which are not shown in here, but the pert that matters to us is the default context,
1:58 so grab that and then you can use that before we even start asking questions
2:03 propose that as a default, or even don't ask the question if you find it here. We've just scratched the surface of the API here
2:09 there is quite a bit going on so jump over to the read the docs for the Cookicutter package and you can explore the full API here.


Talk Python's Mastodon Michael Kennedy's Mastodon