16

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?

mar tin
  • 9,266
  • 23
  • 72
  • 97

1 Answers1

25

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')
creanion
  • 2,319
  • 2
  • 13
  • 17
minrk
  • 37,545
  • 9
  • 92
  • 87
  • 1
    Fantastic. Our of curiosity, how did you find this API? I'd searched in the docs and it didn't come up easily. – mar tin Mar 06 '17 at 21:16
  • 1
    I cheated: I already knew what it was called, so I could search the sphinx docs for the exact name. Adding more descriptive text to the docstring [here](https://github.com/ipython/ipython/blob/5.3.0/IPython/core/display.py#L951) would help others find it. Notably: 'retina' is not listed there, but it is one of the available options. – minrk Mar 07 '17 at 04:32
  • 5
    lol...probably because @minrk is one of the core developer in Jupter project – moiaussi06 Jun 06 '18 at 22:14
  • I tried doing this and I actually get bigger images if I open them in a new tab, but they're being displayed scaled down in the notebook. Any easy way to get them at 100% zoom? – filippo Mar 24 '19 at 11:05
  • @filippo you can try `mpl.rc("figure", dpi=192)` without setting `IPython.display`. You will get enlarged figures both in notebook and outside. – Syrtis Major Apr 29 '20 at 11:46
  • Still works fine. Might be prompted to import from elsewhere now: `from matplotlib_inline.backend_inline import set_matplotlib_formats` – webelo Nov 15 '22 at 20:24
  • Updated to the new incantation – creanion Jul 27 '23 at 13:40