Example #1
0
def set_matplotlib_formats(*formats, **kwargs):
    """Select figure formats for the inline backend. Optionally pass quality for JPEG.

    For example, this enables PNG and JPEG output with a JPEG quality of 90%::

        In [1]: set_matplotlib_formats('png', 'jpeg', quality=90)

    To set this in your config files use the following::
    
        c.InlineBackend.figure_formats = {'png', 'jpeg'}
        c.InlineBackend.print_figure_kwargs.update({'quality' : 90})

    Parameters
    ----------
    *formats : strs
        One or more figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
    **kwargs :
        Keyword args will be relayed to ``figure.canvas.print_figure``.
    """
    from IPython.core.interactiveshell import InteractiveShell
    from IPython.core.pylabtools import select_figure_formats
    from IPython.kernel.zmq.pylab.config import InlineBackend
    # build kwargs, starting with InlineBackend config
    kw = {}
    cfg = InlineBackend.instance()
    kw.update(cfg.print_figure_kwargs)
    kw.update(**kwargs)
    shell = InteractiveShell.instance()
    select_figure_formats(shell, formats, **kw)
Example #2
0
def set_matplotlib_formats(*formats, **kwargs):
    """Select figure formats for the inline backend. Optionally pass quality for JPEG.

    For example, this enables PNG and JPEG output with a JPEG quality of 90%::

        In [1]: set_matplotlib_formats('png', 'jpeg', quality=90)

    To set this in your config files use the following::
    
        c.InlineBackend.figure_formats = {'png', 'jpeg'}
        c.InlineBackend.print_figure_kwargs.update({'quality' : 90})

    Parameters
    ----------
    *formats : strs
        One or more figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
    **kwargs :
        Keyword args will be relayed to ``figure.canvas.print_figure``.
    """
    from IPython.core.interactiveshell import InteractiveShell
    from IPython.core.pylabtools import select_figure_formats
    # build kwargs, starting with InlineBackend config
    kw = {}
    from ipykernel.pylab.config import InlineBackend
    cfg = InlineBackend.instance()
    kw.update(cfg.print_figure_kwargs)
    kw.update(**kwargs)
    shell = InteractiveShell.instance()
    select_figure_formats(shell, formats, **kw)
Example #3
0
 def _figure_formats_changed(self, name, old, new):
     from IPython.core.pylabtools import select_figure_formats
     if 'jpg' in new or 'jpeg' in new:
         if not pil_available():
             raise TraitError("Requires PIL/Pillow for JPG figures")
     if self.shell is None:
         return
     else:
         select_figure_formats(self.shell, new)
def configure_inline_support(shell, backend):
    """Configure an IPython shell object for matplotlib use.

    Parameters
    ----------
    shell : InteractiveShell instance

    backend : matplotlib backend
    """
    # If using our svg payload backend, register the post-execution
    # function that will pick up the results for display.  This can only be
    # done with access to the real shell object.

    cfg = InlineBackend.instance(parent=shell)
    cfg.shell = shell
    if cfg not in shell.configurables:
        shell.configurables.append(cfg)

    if backend == 'module://matplotlib_inline.backend_inline':
        shell.events.register('post_execute', flush_figures)

        # Save rcParams that will be overwrittern
        shell._saved_rcParams = {}
        for k in cfg.rc:
            shell._saved_rcParams[k] = matplotlib.rcParams[k]
        # load inline_rc
        matplotlib.rcParams.update(cfg.rc)
        new_backend_name = "inline"
    else:
        try:
            shell.events.unregister('post_execute', flush_figures)
        except ValueError:
            pass
        if hasattr(shell, '_saved_rcParams'):
            matplotlib.rcParams.update(shell._saved_rcParams)
            del shell._saved_rcParams
        new_backend_name = "other"

    # only enable the formats once -> don't change the enabled formats (which the user may
    # has changed) when getting another "%matplotlib inline" call.
    # See https://github.com/ipython/ipykernel/issues/29
    cur_backend = getattr(configure_inline_support, "current_backend", "unset")
    if new_backend_name != cur_backend:
        # Setup the default figure format
        select_figure_formats(shell, cfg.figure_formats,
                              **cfg.print_figure_kwargs)
        configure_inline_support.current_backend = new_backend_name
Example #5
0
def set_matplotlib_formats(*formats, **kwargs):
    """Select figure formats for the inline backend. Optionally pass quality for JPEG.

    For example, this enables PNG and JPEG output with a JPEG quality of 90%::

        In [1]: set_matplotlib_formats('png', 'jpeg', quality=90)

    To set this in your config files use the following::
    
        c.InlineBackend.figure_formats = {'pdf', 'png', 'svg'}
        c.InlineBackend.quality = 90

    Parameters
    ----------
    *formats : list, tuple
        One or a set of figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
    quality : int
        A percentage for the quality of JPEG figures. Defaults to 90.
    """
    from IPython.core.interactiveshell import InteractiveShell
    from IPython.core.pylabtools import select_figure_formats
    shell = InteractiveShell.instance()
    select_figure_formats(shell, formats, quality=90)
Example #6
0
def set_matplotlib_formats(*formats, **kwargs):
    """Select figure formats for the inline backend. Optionally pass quality for JPEG.

    For example, this enables PNG and JPEG output with a JPEG quality of 90%::

        In [1]: set_matplotlib_formats('png', 'jpeg', quality=90)

    To set this in your config files use the following::
    
        c.InlineBackend.figure_formats = {'pdf', 'png', 'svg'}
        c.InlineBackend.quality = 90

    Parameters
    ----------
    *formats : list, tuple
        One or a set of figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
    quality : int
        A percentage for the quality of JPEG figures. Defaults to 90.
    """
    from IPython.core.interactiveshell import InteractiveShell
    from IPython.core.pylabtools import select_figure_formats
    shell = InteractiveShell.instance()
    select_figure_formats(shell, formats, quality=90)
Example #7
0
 def _update_figure_formatters(self):
     if self.shell is not None:
         from IPython.core.pylabtools import select_figure_formats
         select_figure_formats(self.shell, self.figure_formats, **self.print_figure_kwargs)
Example #8
0
 def _update_figure_formatters(self):
     if self.shell is not None:
         select_figure_formats(self.shell, self.figure_formats, **self.print_figure_kwargs)
Example #9
0
import IPython.core.display
df = DisplayFormatter()


# fake thing which select_figure_formats will work on
class qshell():
    def __init__(self, df=None):
        self.display_formatter = df


qpubcallback = None
qclearcallback = None
qipythoncallback = None
shell = qshell(df)
select_figure_formats(shell, {
    'png'
})  #,'svg','jpeg','pdf','retina'}) /more means multiple output mime types?


def initialise(qpubfunc, qcommfunc, qclearfunc, qipythonfunc):
    """Initialise the backend, the function given will be 
 called with (imagedata;metadata) on show"""
    import traceback
    global qpubcallback
    global qclearcallback
    global qipythoncallback
    qpubcallback = qpubfunc
    qclearcallback = qclearfunc
    qipythoncallback = qipythonfunc
    try:
        import matplotlib.pyplot as plt
Example #10
0
 def _update_figure_formatters(self):
     if self.shell is not None:
         from IPython.core.pylabtools import select_figure_formats
         select_figure_formats(self.shell, self.figure_formats,
                               **self.print_figure_kwargs)