marimo icon indicating copy to clipboard operation
marimo copied to clipboard

`mo.mpl.interactive` Figure doesn't load when tab is not in focus

Open Haleshot opened this issue 7 months ago • 0 comments

Describe the bug

Interactive matplotlib figures don't render when switching browser tabs during notebook launching (from CLI) — empty space appears where figures should be. Figures only render properly when the notebook tab remains in focus during launch.

  1. Create a new notebook with the minimal code given to reproduce
  2. Launch the notebook from the CLI and immediately switch to another tab/window
  3. Switch back to the notebook tab

You'll notice something like this:

Screenshot

Image

Getting this error thrown in the browser console (infinite loop):

Image

Expected behavior:

The plot should load independent of whether the notebook page is in focus or not during launch.

Actual behavior:

The plot doesn't render — you see empty space where the figure should be. Running the cell again causes the figure to appear.

Environment

  "marimo": "0.12.1",
  "OS": "Windows",
  "OS Version": "11",
  "Processor": "AMD64 Family 25 Model 80 Stepping 0, AuthenticAMD",
  "Python Version": "3.11.4",
  "Binaries": {
    "Browser": "132.0.6834.111",
    "Node": "v22.12.0"
  },
  "Dependencies": {
    "click": "8.1.8",
    "docutils": "0.21.2",
    "itsdangerous": "2.2.0",
    "jedi": "0.19.2",
    "markdown": "3.7",
    "narwhals": "1.26.0",
    "packaging": "24.2",
    "psutil": "6.1.1",
    "pygments": "2.19.1",
    "pymdown-extensions": "10.14.3",
    "pyyaml": "6.0.2",
    "ruff": "0.9.6",
    "starlette": "0.45.3",
    "tomlkit": "0.13.2",
    "typing-extensions": "4.12.2",
    "uvicorn": "0.34.0",
    "websockets": "14.2"
  },
  "Optional Dependencies": {
    "anywidget": "0.9.16",
    "pandas": "2.2.3"
  },
  "Experimental Flags": {
    "rtc": false,
    "chat_sidebar": true,
    "tracing": true,
    "multi_column": true
  }
}

Code to reproduce

# /// script
# requires-python = ">=3.11"
# dependencies = [
#     "matplotlib==3.10.1",
#     "numpy==2.2.4",
#     "marimo",
# ]
# ///

import marimo

__generated_with = "0.12.5"
app = marimo.App(width="medium")


@app.cell
def _():
    import marimo as mo
    return (mo,)


@app.cell
def _():
    import matplotlib.pyplot as plt
    import numpy as np
    return np, plt


@app.cell
def _(mo, np, plt):
    fig, ax = plt.subplots(figsize=(10, 6))

    # x values and sigmoid function
    x = np.linspace(-10, 10, 1000)
    sigmoid = lambda z: 1 / (1 + np.exp(-z))
    y = sigmoid(x)

    # plot the sigmoid
    ax.plot(x, y, 'b-', linewidth=2)
    ax.set_title('Sigmoid Function')
    ax.grid(True, alpha=0.3)

    # Display with mo.mpl.interactive
    mo.mpl.interactive(fig)
    return ax, fig, sigmoid, x, y


if __name__ == "__main__":
    app.run()

Haleshot avatar Apr 08 '25 11:04 Haleshot