Tuesday, July 30, 2013

IPython %helloworld extension

At Monday's after-PyOhio sprint, I changed ipython-sql from an IPython Plugin to an Extension; this makes it compatible with IPython 1.0. Fortunately, this was really easy; mostly I just deleted Plugin code I didn't understand anyway.

But I do feel like "Writing Extensions" docs are lacking a "Hello World" example. Here's mine.

from IPython.core.magic import Magics, magics_class, line_magic, cell_magic

class HelloWorldMagics(Magics):
    """A simple Hello, <name> magic.

    @line_magic  # or ``@line_magic("hi")`` to make ``%hi`` the name of the magic
    def helloworld(self, line='', cell=None):
        """Virtually empty magic for demonstration purposes.

          In [1]: %load_ext helloworld

          In [2]: %helloworld Catherine
          Out[2]: u'Hello, Catherine'

        return "Hello, %s\n%s" % (line, cell or "")
def load_ipython_extension(ip):

No comments: