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.

Question: how to programmatically bootstrap a cookiecutter

See original GitHub issue
  • Cookiecutter version: cookiecutter==1.7.2 (current stable)
  • Python version: Python==3.8.6

Description:

I am spooling up a cookiecutter repository called my-cookiecutter (not the real name), and have it checked out locally.

cookiecutter.json

{
    "foo": "",
}

I have another repo called instance-01 (not the real name) where I want to have pre-supplied parameters for cookiecutter.json in a file.

instance-01-cookiecutter-config.json

{
    "foo": "bar",
}

I would like to run cookiecutter ../path/to/my-cookiecutter and somehow take in the instance-01-cookiecutter-config.json (instead of having to manually fill out prompts).

I have read through most of the cookiecutter docs and many issues, and can’t figure out if this is something cookiecutter can do.

In other words, how can I programmatically bootstrap a cookiecutter? Is it possible?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
edeszcommented, Jan 19, 2021

Hi @jamesbraza, unless I’m misunderstanding the issue, this is exactly what cookiecutter’s --config-file (docs) command line argument is designed to address. This is a YAML file that accepts a default-context block consisting of the template parameter’s name-value pairs (specified in cookiecutter.json) that will be injected as default values when you generate a templated project with cookiecutter. This partly supports an input-less workflow. So, for example if you had {"foo": ""} in your cookiecutter.json, then you could specify {"foo": "bar"} as one of the name-value pairs in the default-context block of a configuration file.

With this, the template’s default values from this config file are set to be used when rendering the template. If you now run cookiecutter --config-file <your-config-file>.yaml, cookiecutter will still prompt for input, but now the values from <your-config-file>.yaml will be injected as the default values at the corresponding prompts. With this setup, you could use cookiecutter in two ways

  1. If you generate your cookiecutter template as usual with
    $ cookiecutter
    
    then you would get
    foo []:
    
    as the prompt for foo but you have to manually enter a value before you can press Enter.
  2. On the other hand, if you generate the template with
    $ cookiecutter --config-file <your-config-file>.yaml
    
    then you would get
    for [bar]:
    
    as the prompt for foo. Now, you can simply press Enter without needing to supply any value because the value for foo will be automatically set to bar (which comes from <your-config-file>.yaml).

So, what’s left to do now, is to inform cookiecutter to actually skip all prompts. To avoid any user input, the CLI argument --no-input (docs) is required. This skips any prompts and uses the values from cookiecutter.json. However, since a custom configuration file is also being specified, those config file settings are treated like the defaults in cookiecutter.json and so those parameters are used in the template.

To summarize all this, here’s a simple example of the overall workflow

  1. Set up /home/<username>/my-cookiecutter/cookiecutter.json
    {
        "foo": ""
    }
    
  2. Set up the config file, at /home/<username>/my-cookiecutter/config.yaml, which will allow you to automatically specify a value for foo when generating a template
    default_context:
        foo: "bar"
    
  3. Use the template to render a project with no user inputs from the CLI
    $ cookiecutter my-cookiecutter \
           --no-input \
           --config-file /home/<username>/my-cookiecutter/config.yaml
    
    and your project is rendered from the template with no prompts.

Notes

  1. Values in the default_context block of the user config file are injected into all cookiecutter projects. So, after generating your first project template (but before you generate your second project template), you would need to replace these config file values with a new set of defaults that are specific to the second template to be rendered.

Hope this helps with your question.

Additional Links

  1. Relevant stackoverflow question
1reaction
tpoindessouscommented, Nov 27, 2020

I was looking at last commits and I saw this one :

https://github.com/cookiecutter/cookiecutter/commit/d74b085260248f5ab180f9e582ea973108fa3e2b

Cookiecutter authors added a new option for selecting replay file.

This is not exactly what we want 😃 but it can be an alternative or the beginning of a new option 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

cookiecutter 1.7.0 documentation - Read the Docs
A command-line utility that creates projects from cookiecutters (project templates), e.g. creating a Python package project from a Python package project ...
Read more >
Compare Cookiecutter to Yeoman
Cookiecutter has a few questions to personalize the template — users specify how to name your project, what the license is, directory names...
Read more >
Cookiecutter - Vue - Django: bootstrap your project with the ...
Great question. Properties can be passed directly from Django templates to top-level Vue components, without REST. There's an example: https:// ...
Read more >
cookiecutter 0.9.1 - PyPI
cookiecutter -django: A bleeding edge Django project template with Bootstrap 3, customizable users app, starter templates, and working user registration.
Read more >
Bootstrap Your Next Python Project With Cookiecutter - PyBites
I finally did it! I bootstrapped my first project with Cookiecutter. There is a lot to discover but wow this tool can save...
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