Skip to main content
A Jupyter cell can produce more than one output. When you call plt.show() or display(), or when the last line of a cell evaluates to a value, the kernel emits an output message. Each of those messages becomes a Result object inside execution.results. The Result holds all the MIME representations that the kernel provided for that output.

The Result object

execution.results is a list[Result]. Each Result exposes known MIME types as named fields:
FieldMIME typeNotes
texttext/plainPlain text representation.
htmltext/htmlHTML representation.
markdowntext/markdownMarkdown representation.
svgimage/svg+xmlSVG markup string.
pngimage/pngBase64-encoded PNG.
jpegimage/jpegBase64-encoded JPEG.
pdfapplication/pdfBase64-encoded PDF.
latextext/latexLaTeX string.
jsonapplication/jsonParsed as a dict.
javascriptapplication/javascriptJavaScript string.
extra(any other MIME type)dict[str, str] of unrecognized MIME types.
Fields that are not present in the kernel output are None. Check a field before using it.

Checking available formats

Call result.formats() to get a list of the field names that are populated for a given result.
for r in execution.results:
    print(r.formats())  # e.g. ["text", "png"]

The main result flag

Each Result has an is_main_result boolean. It is True for the execute_result message — the value of the last expression in the cell. It is False for display_data messages produced by plt.show(), display(), and similar calls. The execution.text convenience property returns the text field of the main result.

Matplotlib example

from wrenn.code_interpreter import Capsule

with Capsule(wait=True) as capsule:
    result = capsule.run_code("""
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.show()
""")

    for r in result.results:
        print(r.formats())     # ["text", "png"]
        if r.png:
            print(f"Got PNG image ({len(r.png)} chars base64)")

Iterating and decoding outputs

import base64

with Capsule(wait=True) as capsule:
    result = capsule.run_code("""
import matplotlib.pyplot as plt
plt.figure()
plt.plot([1, 2, 3])
plt.show()
plt.figure()
plt.bar([1, 2, 3], [4, 5, 6])
plt.show()
""")

    for r in result.results:
        if r.png:
            image_bytes = base64.b64decode(r.png)
            # write to file, send over HTTP, etc.
        elif r.html:
            print(r.html)
        elif r.text:
            print(r.text)
png and jpeg fields are base64-encoded strings, exactly as the Jupyter kernel delivers them. Decode with base64.b64decode(r.png) to get raw bytes you can write to a file or pass to an image library.
String expression values have their surrounding quotes stripped automatically. If a cell evaluates to 'hello', result.text is hello, not 'hello'.