jupyter / jupyter_core

Core Jupyter functionality
https://jupyter-core.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
195 stars 181 forks source link

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

Closed azjps closed 1 year ago

azjps commented 1 year ago

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
blink1073 commented 1 year ago

I'll release this on Monday.