Skip to content
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

Migrate to click #188

Merged
merged 3 commits into from
May 30, 2023
Merged

Migrate to click #188

merged 3 commits into from
May 30, 2023

Conversation

dlqqq
Copy link
Member

@dlqqq dlqqq commented May 23, 2023

Description

Uses click to parse arguments. Notable features:

  • Declarative handling of subcommands. Adding new subcommands is done easily by using the @line_magic_parser.command() decorator.
  • Support for subcommand arguments. Use the @click.option() or @click.argument() decorators to add new arguments to a subcommand.
  • Automatically generated help strings.
  • Mitigates the issues of Python version compatibility involved with using argparse, which may include breaking changes version-to-version.

Demo

Screen.Recording.2023-05-23.at.4.00.26.PM.mov

Breaking changes

Language models can no longer be invoked by a line magic. The reason for this is that parsers do not handle variadic arguments correctly when using subcommands. Both argparse and click fail for such a use-case. So for example, %ai list openai will make click think that you want to execute the list model with the prompt openai, rather than dispatching to openai. I really, really tried getting this to work, with one hacky workaround after another. Unfortunately, in the current landscape of CLI parsers in Python, it looks like there's no way of doing this while also parsing subcommands declaratively.

However, I believe this is a good breaking change to make. Users rarely have enough horizontal space to fit their entire prompt into a single line, along with the magic command itself and its arguments. This helps keep the first line of each magic short, which also avoids clipping the cell toolbar in JupyterLab. This also makes building the parsers easier, since it allows us to have two separate parsers: one for the cell magic and one for the line magic.

@dlqqq dlqqq added the enhancement New feature or request label May 23, 2023
@JasonWeill
Copy link
Collaborator

Please modify the user docs to reflect the breaking change. You might need to modify (and check in changes to) the example workbooks, too.

dlqqq and others added 2 commits May 26, 2023 22:36
Co-authored-by: Jason Weill <93281816+JasonWeill@users.noreply.github.com>
@dlqqq dlqqq marked this pull request as ready for review May 26, 2023 22:55
@dlqqq
Copy link
Member Author

dlqqq commented May 27, 2023

I've updated the documentation to reflect this change. 👍

@dlqqq dlqqq merged commit d0ecc47 into jupyterlab:main May 30, 2023
@dlqqq dlqqq deleted the use-click branch May 30, 2023 22:48
dbelgrod pushed a commit to dbelgrod/jupyter-ai that referenced this pull request Jun 10, 2024
* migrate to click

* prefer "for options" over "for more" in subcommand help

Co-authored-by: Jason Weill <93281816+JasonWeill@users.noreply.github.com>

* update magics documentation

---------

Co-authored-by: Jason Weill <93281816+JasonWeill@users.noreply.github.com>
Marchlak pushed a commit to Marchlak/jupyter-ai that referenced this pull request Oct 28, 2024
* migrate to click

* prefer "for options" over "for more" in subcommand help

Co-authored-by: Jason Weill <93281816+JasonWeill@users.noreply.github.com>

* update magics documentation

---------

Co-authored-by: Jason Weill <93281816+JasonWeill@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use argument parsers to run AI commands
2 participants