Type Error when running simple Custom Magic

21 views
Skip to first unread message

John Omernik

unread,
Jun 19, 2017, 12:30:56 PM6/19/17
to Project Jupyter
I have been trying to run a Magics Class based on http://ipython.readthedocs.io/en/stable/config/custommagics.html 

I keep getting an error related to a type error but can't figure out where I am going, it should simply print the line from the magic, but instead TypeError. Any help would be appreciated!



Module I load

from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic, line_cell_magic)

@magics_class
class MyClass(Magics):

    @line_magic
    def lmagic(self, line):
        print(line)
        return line



In my IPython Notebook I start things by doing

ip = get_ipython()

ip.register_magics(MyClass)

Then I go to the next cell and type

%lmagic Hello

and get:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-2a6ddce5521d> in <module>()
----> 1 get_ipython().magic('lmagic Hello')

/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py in magic(self, arg_s)
   2156         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2157         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2158         return self.run_line_magic(magic_name, magic_arg_s)
   2159 
   2160     #-------------------------------------------------------------------------

/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line)
   2077                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2078             with self.builtin_trap:
-> 2079                 result = fn(*args,**kwargs)
   2080             return result
   2081 

TypeError: 'str' object is not callable



I am stumped on how this is working... 

Matthias Bussonnier

unread,
Jun 19, 2017, 12:35:00 PM6/19/17
to jup...@googlegroups.com
Hello John,

That is really strange, have you tried restarting your kernel? If you
make modification to your module it might not be reloaded. Doing
exactly what you describe works here (screenshot attached).

Cheers,
--
Matthias
> --
> You received this message because you are subscribed to the Google Groups
> "Project Jupyter" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jupyter+u...@googlegroups.com.
> To post to this group, send email to jup...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jupyter/9d331991-775a-4032-ad28-36808b71a455%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Screen Shot 2017-06-19 at 09.33.42.png

John Omernik

unread,
Jun 19, 2017, 1:03:24 PM6/19/17
to Project Jupyter
So I solved my own problem. I was running my own __init__ function in the Class (I didn't put that in the post because I didn't see it as relevant... it was)  I had to do the instructions further down the page to use my own init function.  Hopefully if someone else runs into this, this post can help them. *Self High Five

John
Reply all
Reply to author
Forward
0 new messages