IPython custom tab-completion for user magic function

Romain G

In IPython, it is fairly easy to provide tab-completion for user-defined object: simply define a __dir__ method that returns a list of strings to the object.

IPython also provide us with a way to define our own custom magic functions using the handy register_line_magic utility. In some ~/.ipython/profile_default/startup/magictest.py:

from IPython.core.magic import register_line_magic

@register_line_magic
def show(dataType):
    # do something depending on the given `dataType` value

Now my question is: how to provide auto-completion to this magic function?

According to this email, one should look into IPython.core.interactiveshell.InteractiveShell.init_completer() for an example of magic function completers such as %reset, '%cd', etc...

However, in the same startup file as the one in which my magic function is defined, the following code didn't work:

from IPython.core.interactiveshell import InteractiveShell

def show_complete():
     return ['dbs', 'databases', 'collections']

InteractiveShell._instance.set_hook(
    'complete_command', show_complete, str_key='%show')

In the IPython shell, typing %show TAB triggers nothing (print statements in the function show that the function is not even called).

Could somebody point me out on some documentation or examples on how to define such user-magic command parameters completion from within the Ipython startup files?

Thanks!

dwjbosman

You can use this:

def load_ipython_extension(ipython):
    def apt_completers(self, event):
        """ This should return a list of strings with possible completions.

        Note that all the included strings that don't start with event.symbol
        are removed, in order to not confuse readline.
        """

        return ['update', 'upgrade', 'install', 'remove']

    ipython.set_hook('complete_command', apt_completers, re_key = '%%apt')

%%apt is the magic keyword

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

IPython custom tab-completion for user magic function

From Dev

Tab completion in ipython for list elements

From Dev

No tab completion for IPython in Emacs textmode

From Dev

No tab completion for IPython in Emacs textmode

From Dev

Custom bash tab completion

From Dev

Capture the result of an IPython magic function

From Dev

iPython autoreload magic function not found

From Dev

Forcing automatic parentheses in ipython loses tab completion

From Dev

default tab completion like ipython in python console

From Dev

Emacs + ipython tab completion not working ("no match")

From Dev

jquery terminal custom tab completion

From Dev

Custom magic in ipython notebooks - Code does not execute

From Dev

IPython: Alias which uses a magic function itself

From Dev

ipython notebook R cell magic as a python function

From Dev

Pycharm IPython tab completion not working (within python console)

From Dev

piping /dev/tty to ipython without losing formatting or tab completion

From Dev

What does tab completion in ipython or jupyter seek in order to suggest alternatives?

From Dev

Get tab completion in custom comint mode

From Dev

Get tab completion in custom comint mode

From Dev

define tab completion helper function to use in several completion files (zsh)

From Dev

zsh: Tab completion for function with Git commands

From Dev

function name + tab does not return docstring in IPython

From Dev

function name + tab does not return docstring in IPython

From Dev

What is the pure Python equivalent to the IPython magic function call %matplotlib inline?

From Dev

Custom bash tab completion showing possible completions, but not actually completing input

From Dev

How to get tab completion for a user defined command in Vim?

From Dev

Custom zsh completion for a function based on default arguments

From Dev

Ignore IPython magic in python

From Dev

How to allow custom Class parameter to be tab-completed in ipython?

Related Related

  1. 1

    IPython custom tab-completion for user magic function

  2. 2

    Tab completion in ipython for list elements

  3. 3

    No tab completion for IPython in Emacs textmode

  4. 4

    No tab completion for IPython in Emacs textmode

  5. 5

    Custom bash tab completion

  6. 6

    Capture the result of an IPython magic function

  7. 7

    iPython autoreload magic function not found

  8. 8

    Forcing automatic parentheses in ipython loses tab completion

  9. 9

    default tab completion like ipython in python console

  10. 10

    Emacs + ipython tab completion not working ("no match")

  11. 11

    jquery terminal custom tab completion

  12. 12

    Custom magic in ipython notebooks - Code does not execute

  13. 13

    IPython: Alias which uses a magic function itself

  14. 14

    ipython notebook R cell magic as a python function

  15. 15

    Pycharm IPython tab completion not working (within python console)

  16. 16

    piping /dev/tty to ipython without losing formatting or tab completion

  17. 17

    What does tab completion in ipython or jupyter seek in order to suggest alternatives?

  18. 18

    Get tab completion in custom comint mode

  19. 19

    Get tab completion in custom comint mode

  20. 20

    define tab completion helper function to use in several completion files (zsh)

  21. 21

    zsh: Tab completion for function with Git commands

  22. 22

    function name + tab does not return docstring in IPython

  23. 23

    function name + tab does not return docstring in IPython

  24. 24

    What is the pure Python equivalent to the IPython magic function call %matplotlib inline?

  25. 25

    Custom bash tab completion showing possible completions, but not actually completing input

  26. 26

    How to get tab completion for a user defined command in Vim?

  27. 27

    Custom zsh completion for a function based on default arguments

  28. 28

    Ignore IPython magic in python

  29. 29

    How to allow custom Class parameter to be tab-completed in ipython?

HotTag

Archive