Skip to content

Sphinx directive raises exception with magic #6131

@sontek

Description

@sontek

If you try to use something like %matplotlib inline in sphinx with the ipython directive it raises the following exception:

Exception in /home/sontek/.virtualenvs/sontek_net/src/sontek_net/deploy/src/sontek_net/blog/2014/03/30/hello_world.rst at block ending on line 30
Specify :okexcept: as an option in the ipython:: block to suppress this message
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-4-2b1da000a957> in <module>()
----> 1 get_ipython().magic(u'matplotlib inline')

/home/sontek/.virtualenvs/sontek_net/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s)
   2203         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2204         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2205         return self.run_line_magic(magic_name, magic_arg_s)
   2206 
   2207     #-------------------------------------------------------------------------

/home/sontek/.virtualenvs/sontek_net/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line)
   2124                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2125             with self.builtin_trap:
-> 2126                 result = fn(*args,**kwargs)
   2127             return result
   2128 

/home/sontek/.virtualenvs/sontek_net/lib/python2.7/site-packages/IPython/core/magics/pylab.pyc in matplotlib(self, line)

/home/sontek/.virtualenvs/sontek_net/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

/home/sontek/.virtualenvs/sontek_net/lib/python2.7/site-packages/IPython/core/magics/pylab.pyc in matplotlib(self, line)
     78         """
     79         args = magic_arguments.parse_argstring(self.matplotlib, line)
---> 80         gui, backend = self.shell.enable_matplotlib(args.gui)
     81         self._show_matplotlib_backend(args.gui, backend)
     82 

/home/sontek/.virtualenvs/sontek_net/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in enable_matplotlib(self, gui)
   2946         # Now we must activate the gui pylab wants to use, and fix %run to take
   2947         # plot updates into account
-> 2948         self.enable_gui(gui)
   2949         self.magics_manager.registry['ExecutionMagics'].default_runner = \
   2950             pt.mpl_runner(self.safe_execfile)

/home/sontek/.virtualenvs/sontek_net/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in enable_gui(self, gui)
   2905 
   2906     def enable_gui(self, gui=None):
-> 2907         raise NotImplementedError('Implement enable_gui in a subclass')
   2908     
   2909     def enable_matplotlib(self, gui=None):

NotImplementedError: Implement enable_gui in a subclass
numpy: 1.8.1
scipy: 0.14.0
matplotlib: 1.3.1
scikit-learn: 0.14.1
<<<-------------------------------------------------------------------------

This happens because the sphinx directive isn't actually using inheritance:
https://github.com/ipython/ipython/blob/master/IPython/sphinxext/ipython_directive.py#L297

You can add enable_gui to it via IP.enable_gui = lambda x: False and it makes everything pass but then you don't actually get inline matplotlib images either.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions