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

Set up shell command-line tab-completion for jupyter and subcommands #337

Merged
merged 2 commits into from Jan 28, 2023

Conversation

azjps
Copy link
Contributor

@azjps azjps commented Jan 28, 2023

Set up shell command-line tab-completion using argcomplete and ipython/traitlets#811. Completion requires argcomplete to be activated and traitlets>=5.8. Behavior is only changed when $_ARGCOMPLETE is set, so there should be no difference in behavior otherwise.

For subcommand handling, this will use list_subcommands() to try to complete the first argument if it doesn't start with a "-", and when a subcommand is detected, argcomplete will increment the start of the command-line handling past the subcommand and drop into the relevant traitlets.Application's argcomplete handling. All of the major subcommands like jupyter lab directly use traitlets.Application.launch_instance so this works pretty seamlessly. However, completing commands that start with the hyphenated commands, e.g. jupyter-lab, would still require the addition of the PYTHON_ARGCOMPLETE_OK marker in the respective projects' main scripts. Note that argcomplete supports following setuptools console_scripts to the corresponding main script to look for the PYTHON_ARGCOMPLETE_OK marker.

Example:

$ jupyter <TAB>
--config-dir      --runtime-dir     kernel            nbconvert         serverextension
--data-dir        --version         kernelspec        nbextension       troubleshoot
--debug           -h                lab               notebook          trust
--help            bundlerextension  labextension      qtconsole
--json            console           labhub            run
--paths           dejavu            migrate           server
$ jupyter lab <TAB>  # shows flags, subcommands, and files
--Application.               --dev-mode                   .git/
--ConnectionFileMixin.       --expose-app-in-browser      .github/
--ContentsManager.           --gateway-url                .gitignore
--FileContentsManager.       --generate-config            .mypy_cache/
--FileManagerMixin.          --help                       .pre-commit-config.yaml
--GatewayClient.             --ip                         .readthedocs.yaml
--GatewayKernelManager.      --keyfile                    .ruff_cache/
--GatewayKernelSpecManager.  --log-level                  CHANGELOG.md
--JupyterApp.                --no-browser                 CONTRIBUTING.md
--KernelManager.             --no-mathjax                 COPYING.md
--KernelSpecManager.         --no-script                  README.md
--LabApp.                    --notebook-dir               __pycache__/
--MappingKernelManager.      --port                       build
--MultiKernelManager.        --port-retries               clean
--NotebookApp.               --pylab                      codecov.yml
--NotebookNotary.            --script                     docs/
--Session.                   --show-config                examples/
--TerminalManager.           --show-config-json           jupyter.py
--allow-root                 --sock                       jupyter_core/
--app-dir                    --sock-mode                  path
--autoreload                 --transport                  paths
--browser                    --watch                      pyproject.toml
--certfile                   --y                          scripts/
--client-ca                  -h                           workspace
--config                     -y                           workspaces
--core-mode                  .flake8
--debug                      .git-blame-ignore-revs
$ jupyter lab --Kernel<TAB>
--KernelManager.      --KernelSpecManager.
$ jupyter lab --KernelManager.<TAB>
--KernelManager.autorestart         --KernelManager.kernel_cmd
--KernelManager.connection_file     --KernelManager.shell_port
--KernelManager.control_port        --KernelManager.shutdown_wait_time
--KernelManager.hb_port             --KernelManager.stdin_port
--KernelManager.iopub_port          --KernelManager.transport
--KernelManager.ip
$ jupyter lab --KernelManager.transport <TAB>
ipc  tcp

azjps and others added 2 commits January 28, 2023 21:48
Set up shell command-line tab-completion using argcomplete and
ipython/traitlets#811.

For subcommand handling, this will use list_subcommands() to
try to complete the first argument if it doesn't start with a "-",
and when a subcommand is detected, argcomplete will increment the
start of the command-line handling past the subcommand and drop into
the relevant traitlets.Application's argcomplete handling. All of
the major subcommands like "jupyter lab" directly use
traitlets.Application.launch_instance so this works pretty seamlessly.

However, completing commands that start with the hyphenated commands,
e.g. "jupyter-lab", would still require the addition of the
PYTHON_ARGCOMPLETE_OK marker in the respective projects' main scripts.

Note that argcomplete supports following setuptools console_scripts
to the corresponding main script to look for the PYTHON_ARGCOMPLETE_OK
marker.
Copy link
Member

@blink1073 blink1073 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again @azjps! ❤️

@blink1073 blink1073 merged commit fa0d0a0 into jupyter:main Jan 28, 2023
@blink1073
Copy link
Member

I'll release this on Monday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants