ipythonjupyter-notebookretina-displayipython-magic

Configure the backend of Ipython to use retina display mode with code


I am using code to configure Jupyter notebooks because I have a repo with plenty of notebooks and want to keep style consistency across all without having to write lengthy setting at the start of each. This way, what I do is having a method to configure the CSS, one to set up Matplotlib and one to configure Ipython.

The reasons I configure my notebooks this way rather than relying on a configuration file as per docs are two:

  1. I am sharing this repo of notebooks publicly and I want all my configs to be visible
  2. I want to keep these configs specific to just this repo I'm creating

As an example, the method to set the CSS looks like

def set_css_style(css_file_path='../styles_files/custom.css'):

    styles = open(css_file_path, "r").read()
    return HTML(styles)

and I call it at the start of each notebook with set_css_style(). Similarly, I have this method to configure the specifics of Ipython:

def config_ipython():

    InteractiveShell.ast_node_interactivity = "all"

Both the above use imports

from IPython.core.display import HTML
from IPython.core.interactiveshell import InteractiveShell

At the moment, as can be seen, the method to configure Ipython only contains the instruction to make it so that when I type the name of variables in multiple lines in a cell I don't need to add a print to make them all be printed.

My question is how to transform the Jupyter magic command to obtain retina-display quality for figures into code. Such command is

%config InlineBackend.figure_format = 'retina'

From the docs of Ipython I can't find how to call this instruction in a method, namely can't find where InlineBackend lives.

I'd just like to add this configuration line to my config_ipython method above, is it possible?


Solution

  • There is a Python API for this:

    import matplotlib_inline
    matplotlib_inline.backend_inline.set_matplotlib_formats('retina')
    

    The older (IPython 7 or 8) method is to use the following, but it is now deprecated:

    from IPython.display import set_matplotlib_formats
    set_matplotlib_formats('retina')