question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access the Context dict in the Pre/Post-Generate Hook scripts

See original GitHub issue

Hey, just want to start off by saying thanks for making this awesome project!

The title says it all–is there any way to access the context dict provided by the cli cookiecutter command in the pre_gen_project.py and post_gen_project.py hook scripts? I want to be able to add an extra context variable (a timestamp just like the example), but without having to write my own script that wraps the cookiecutter.main.cookiecutter function. I also wanted to be able to reassign the context variables if validation fails (instead of just raising an exception).

Any help is greatly appreciated. Keep up the good work! 😃

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:35
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

11reactions
detroitcodercommented, Sep 13, 2017

Asking a similar question. I wanted to use cookie cutter to deploy applications that require a lot of configuration and was hoping to use cookiecutter to templatize the deployment. I want to add variables that are like {{cookiecutter.current_directory}} or {{cookiecutter.current_user}} without needing to pass these in from the command line. I was hoping there was a hook somewhere that I could set these but I guess not.

I could pass these as extra context using the python api, but that requires a user calling a python script that calls out to the cookiecutter function which requires them to do more pre-installation. Would be great if there was a hook where I could modify the cookiecutter jinja variables.

4reactions
samj1912commented, Aug 22, 2020

See my reply at - https://github.com/cookiecutter/cookiecutter/issues/364#issuecomment-678683237 and the demo repo https://github.com/samj1912/cookiecutter-advanced-demo

That allows you to modify the cookiecutter context from pre gen hooks on cookiecutter v0.7+ with some advanced jinja hackery.

Advanced context modification for cookiecutter.

The above repo is an advanced example of a cookiecutter that can modify cookiecutter’s context values from hooks. Read the pre_gen_project.py for examples and a demo on how to modify context.

To run this demo cookiecutter do -

cookiecutter https://github.com/samj1912/cookiecutter-advanced-demo

This demo shows how to do the following -

  1. Add values to the cookiecutter context.

The above is useful for a bunch of cases -

  • Being able to add values to the context independent of the user input - for eg. creating a valid project slug from a project name by escaping the values.
  • When you are sharing files between different cookiecutter directories and you want to define defaults in order to avoid Jinja from complaining the certain values are not defined.
  1. Modify existing values cookiecutter context values even after user input-

This is useful when you want to handle bad user input and instead of raising an error, set the cookiecutter context values correct. For eg modifying the project_slug from the user to be valid.

  1. Conditionally modify the above based on user input.

Note - You can also do this to change the name of the variable that is used to define the generated project directory. This is useful when handling inputs from the user that you want to escape.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Access the Context dict in the Pre/Post-Generate Hook scripts
The title says it all--is there any way to access the context dict provided by the cli cookiecutter command in the pre_gen_project.py and ......
Read more >
Using Pre/Post-Generate Hooks - Cookiecutter - Read the Docs
This makes it easy for a post-generate hook to find generated files using relative paths. Template variables are rendered in the script¶. Just...
Read more >
How do I access a dictionary key in a context variable that gets ...
You have added new variable in a context called categories so you can access its value through iterating categories {% for key, value...
Read more >
Cookiecutter Documentation: Release 1.7.2 | PDF - Scribd
Direct access to the Cookiecutter API allows for injection of extra context. • Pre- and post-generate hooks: Python or shell scripts to run...
Read more >
Awesome Bookmarks: My Collection - Cody Hiar
Ruslan's Blog · Looking up words in a Dictionary using Python ... Incrementally improving the performance of a python script, until nothing ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found