> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wrenn.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Handle Rich Output from Code Interpreter: Charts and Images

> Wrenn's code interpreter captures rich outputs like matplotlib charts, pandas DataFrames, and HTML — available as typed MIME fields on each Result.

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:

| Field        | MIME type                | Notes                                        |
| ------------ | ------------------------ | -------------------------------------------- |
| `text`       | `text/plain`             | Plain text representation.                   |
| `html`       | `text/html`              | HTML representation.                         |
| `markdown`   | `text/markdown`          | Markdown representation.                     |
| `svg`        | `image/svg+xml`          | SVG markup string.                           |
| `png`        | `image/png`              | Base64-encoded PNG.                          |
| `jpeg`       | `image/jpeg`             | Base64-encoded JPEG.                         |
| `pdf`        | `application/pdf`        | Base64-encoded PDF.                          |
| `latex`      | `text/latex`             | LaTeX string.                                |
| `json`       | `application/json`       | Parsed as a `dict`.                          |
| `javascript` | `application/javascript` | JavaScript 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.

```python theme={null}
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

```python theme={null}
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

```python theme={null}
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)
```

<Tip>
  `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.
</Tip>

<Note>
  String expression values have their surrounding quotes stripped automatically. If a cell evaluates to `'hello'`, `result.text` is `hello`, not `'hello'`.
</Note>
