-
Notifications
You must be signed in to change notification settings - Fork 916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use click features for project creation prompts #4387
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Laura Couto <[email protected]>
Signed-off-by: Laura Couto <[email protected]>
Signed-off-by: Laura Couto <[email protected]>
The current problem with this is that when the |
Signed-off-by: Laura Couto <[email protected]>
Signed-off-by: Laura Couto <[email protected]>
Signed-off-by: Laura Couto <[email protected]>
Signed-off-by: Laura Couto <[email protected]>
I confirm that the current branch works at least! IIUC this now purely uses Click. Is there a plan to try to restore the colors later on, maybe using https://click.palletsprojects.com/en/stable/utils/#ansi-colors ? (not necessarily right now, as it would probably complicate fixing the remaining tests) |
Definitely! I want to make sure all of the functionality is as we expect first, but it would be nice to have an option for colors (with the added bonus of not relying on Rich) |
Signed-off-by: Laura Couto <[email protected]>
Signed-off-by: Laura Couto <[email protected]>
Signed-off-by: Laura Couto <[email protected]>
Signed-off-by: Laura Couto <[email protected]>
Signed-off-by: Laura Couto <[email protected]>
Signed-off-by: Laura Couto <[email protected]>
Signed-off-by: Laura Couto <[email protected]>
@lrcouto, could you please clarify if colours should work and how we can check everything works as expected? |
They do not work yet, I'm implementing a color scheme at the moment. It should not affect automated tests, but the visual appearance will also be slightly different, as those prompts from cookiecutter were importing the Prompt class from Rich. I'll post some screenshots with options once I finish implementing it. |
Signed-off-by: Laura Couto <[email protected]>
Signed-off-by: Laura Couto <[email protected]>
Signed-off-by: Laura Couto <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a good start of removing some cookiecutter functionality, but like mentioned in the ticket:
- https://click.palletsprojects.com/en/8.1.x/options/#choice-options
- https://click.palletsprojects.com/en/8.1.x/options/#prompting
These could be used to get rid of/simplify the validation we do for inputs to kedro new options like --tools and --example
I think it would be good to try and simplify the validation/prompting logic by using click functionality.
kedro/framework/cli/starters.py
Outdated
show_default=True, | ||
type=str, | ||
).strip() | ||
|
||
if user_input: | ||
prompt.validate(user_input) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need to use our own custom validation or is it possible to use click for that as well? I saw that was one of the suggestions on the original ticket #3878
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Especially lower down in the code we have pretty complex parsing/validation methods e.g. _parse_tools_input
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm looking into this right now. From what I had seen I think the options that click has might be a bit unspecific for what we need, but there's an option to apply custom validation logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes for the more complicated options it might not be possible, but for --example
and --telemetry
we only need to check yes/no/y/n
. I think you can do something like:
@click.option(
"--example",
"-e",
"example_pipeline",
help=EXAMPLE_ARG_HELP,
type=click.Choice(["yes", "no", "y", "n"], case_sensitive=False),
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That does work for yes/no options. One minor issue though is that the error message is not customizable though. On top of that, the example pipeline option received through a config file would still need to have some validation since it doesn't go through the CLI options, so we'd still need the function.
This is what the error message from click.Choice
looks like:
As opposed to the one we have right now, from _validate_input_with_regex_pattern
:
For interactive prompts, that are happening inside a loop, there would need to be some sort of logic to pass the click.Choice value for the different prompts. Could be done through the prompts.yml file maybe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For that specific validate
we could use one of the click Exception options, though I feel like they were more designed to be used with CLI options and not with prompts. I also personally like having finer control over the appearance of the error messages.
(Also, could we maybe move this user_input parameter to be inside the _Prompt class and validate it as we set it? The function is called only once, I don't think it necessarily needs to be a function)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another possibility is using the callback parameter in click for custom validation. For example, this:
def _validate_yes_no(ctx, param, value) -> None:
if not re.match(r"(?i)^\s*(y|yes|n|no)\s*$", value, flags=re.X):
raise click.BadParameter(f"'{value}' is an invalid value for {param}. It must contain only y, n, YES, or NO (case insensitive).")
return value
@click.option("--telemetry", "-tc", "telemetry_consent", help=TELEMETRY_ARG_HELP, callback=_validate_yes_no)
Results in this:
Does require writing the callback functions though. And the output is the click Exception output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally think the default click exception ("Invalid value for..") is fine. But maybe @astrojuanlu has a different view on that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree the default click exception is fine, considering the tradeoffs
kedro/framework/cli/starters.py
Outdated
"-t", | ||
"selected_tools", | ||
help=TOOLS_ARG_HELP, | ||
) | ||
@click.option("--example", "-e", "example_pipeline", help=EXAMPLE_ARG_HELP) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use Click choice options here https://click.palletsprojects.com/en/stable/options/#choice-options to simplify the prompting and validation logic for example project?
Signed-off-by: Laura Couto <[email protected]>
Signed-off-by: Laura Couto <[email protected]>
Signed-off-by: Laura Couto <[email protected]>
After some exploration of the project creation flow (colloquially referred to as "gazing into the abyss") with the objective of simplifying it in some way, shape or form, we can extract some insights:
For now what could be done, besides the aforementioned replacement of the prompts, is replacing the |
Description
Replaces Cookiecutter prompts with the
click.prompt
method, without affecting functionality inkedro new
.We are still using some methods that Cookiecutter imports from Rich, mainly for exceptions, so at the moment it'll still not allow us to uninstall rich without downgrading Cookiecutter.
Prompt appearance is now defined used the click.style method, so it can be changed if we so desire.
Example appearance:
Development notes
Developer Certificate of Origin
We need all contributions to comply with the Developer Certificate of Origin (DCO). All commits must be signed off by including a
Signed-off-by
line in the commit message. See our wiki for guidance.If your PR is blocked due to unsigned commits, then you must follow the instructions under "Rebase the branch" on the GitHub Checks page for your PR. This will retroactively add the sign-off to all unsigned commits and allow the DCO check to pass.
Checklist
RELEASE.md
file