Added support for providing human-readable prompts to the different variables#1881
Merged
ericof merged 7 commits intocookiecutter:mainfrom Jul 5, 2023
Merged
Conversation
… variable in the cookiecutter.json. Human readable questions are shown instead of variable names to the user when possible. cf. cookiecutter#1835
for more information, see https://pre-commit.ci
Member
|
Thanks, @vemonet! As soon as the tests pass, I will merge this! |
Contributor
|
Does this support customizing choice prompts? I'd like to show something more helpful for selecting choices, but don't see how this fits into this structure. If I provided a dict here, how would I customize the top level prompt? Maybe a special key? {
"__prompts__": {
"backend": {
"__prompt__": "Select a backend to build your package:",
"setuptools": "Classic setuptools configuration",
"setuptools621": "Modern pyproject.toml setuptools configuration"
}
}
}? (This is great, by the way!) |
Contributor
Author
|
Hi @henryiii , it does not support this, but is an interesting suggestion And the solution you proposed is nice, and the easiest to implement I think. I'll take a look into it when I will have time A complete JSON would look like this: {
"package": "my-package",
"backend": [
"setuptools",
"setuptools621"
],
"__prompts__": {
"package": "Your package slug",
"backend": {
"__prompt__": "Select a backend to build your package:",
"setuptools": "Classic setuptools configuration",
"setuptools621": "Modern pyproject.toml setuptools configuration"
}
}
} |
vemonet
added a commit
to vemonet/cookiecutter
that referenced
this pull request
Jul 11, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi @pydanny @jensens @ericof @audreyfeldroy, I added support for providing human-readable questions to the different variables
Many issues mentioned it:
cookiecutter.json#1835A PR has been attempted but abandonned: #848
There have been lengthy discussions about wherever the json format should completely be changed, introduce breaking change, etc... I took the simplest approach, to introduce the least amount of technical debt and keep complete backward compatibility . So that we can merge this feature, really much needed for a templating library, and discuss about changing the JSON format later!
It was quite straightforward to implement, I just added a
__prompts__object, which takes the variable names as key and the questions to ask the user as values.Example of a config file:
{ "package_name": "my-package", "module_name": "{{ cookiecutter.package_name.replace('-', '_') }}", "short_description": "A nice python package", "__prompts__": { "package_name": "Select your package name:", "module_name": "Select your module name:" } }The 2 first questions will use the description provided, the 3rd one will use the variable name since there is no description provided
This format stays easy to read, and easy to write. Once the dev defined all its variables with their default they can copy them, paste them in the
__prompts__block, and replace the default by the questions one by one. Also wrap something around 2 underscores is a pythonic way to say "this is special" (__init__,__main__)In my opinion when you have many variables it is more readable than a nested dict where some keys are repeated all other the place
{ "package_name": { "default": "my-package", "prompts": "Your package name", }, "module_name": { "default": "{{ cookiecutter.package_name.replace('-', '_') }}", "prompts": "Your package name", }, }It is 100% backward compatible with the previous
cookiecutter.jsonformat 🎉 so it will not break existing template, it will only make templates that have defined__prompts__better!tox, and I added a few tests for the new feature, to reach 💯% of code coverageI hope there is no more excuses to not merge this long-awaited feature 😄 I am already using it for my templates and will continue in the future since those templates are fully compatible!
There are no breaking changes so this feature can be pushed to the next minor release without any concern 🥳
Please let me know if we should use something else instead of
__prompts__, I can make the changes required in a few minutes (maybe_prompts_with only 1 underscore? that might be less prone to user mistakes, but the double underscore seems more pythonic)