How does Jupyter detect Matplotlib plots in a cell?

I am sorry - I realize this is a very ignorant question - but how does Jupyter know to generate Matplotlib plots in a code cell?

I am currently working on a pull-request to the Reticulate R framework that needs a good and robust method to detect whether a particular code cell (“chunk”) has generated a Matplotlib plot, so it can call plt.show() suitably, and display the plot in rendered document. How does Jupyter do this?

Sorry again for my lack of knowledge - thanks for any pointers.

ipykernel (the default kernel) checks the output for certain patterns to recognize the file formats.

# constants for identifying png/jpeg data
PNG = b"\x89PNG\r\n\x1a\n"
# front of PNG base64-encoded
PNG64 = b"iVBORw0KG"
JPEG = b"\xff\xd8"
# front of JPEG base64-encoded
JPEG64 = b"/9"
# constants for identifying gif data
GIF_64 = b"R0lGODdh"
GIF89_64 = b"R0lGODlh"
# front of PDF base64-encoded
PDF64 = b"JVBER"

Copied from here: ipykernel/ipykernel/jsonutil.py at 04eb1b99b4f0387ab352cc1ad092eeaf7c49e001 · ipython/ipykernel · GitHub

Thanks for the reply! I couldn’t immediately see where those constants were used - grep just picked up a test. Do you know where and how they are deployed to do the work of detecting plot outputs?

no, but i’d test for the type of an output and if it is bytes check for known mime headers/footers.
maybe those constants aren’t used anymore and are just old code, maybe they are used by some other module.
i don’t know how jupyter/ipykernel does it but for the simple ipykernels i built i used exactly the same method.

Ah - but the problem is - I don’t have easy access to all the outputs. For example, if the code includes res = plt.plot(range(10)) - I’d have to go in, analyze the code, find the variable name, and inspect the result - and that in the simplest case. So I was hoping to do something like:

n_figures = len(get_figures())
run_the_code(the_code)
if len(get_figures() > n_figures):
    # Do something