Skip to content

fix: refresh mpl interactive plots when browser tab becomes visible#8287

Merged
dmadisetti merged 1 commit intomainfrom
ms/mpl-interactive
Feb 12, 2026
Merged

fix: refresh mpl interactive plots when browser tab becomes visible#8287
dmadisetti merged 1 commit intomainfrom
ms/mpl-interactive

Conversation

@mscolnick
Copy link
Contributor

@mscolnick mscolnick commented Feb 12, 2026

Closes #2092
Closes #4435

Problem

When mo.mpl.interactive() plots are rendered while the browser tab is in the
background (minimized or switched to another tab), the plots appear blank.
This is because browsers throttle or skip canvas rendering in background tabs,
so the image data sent by matplotlib's WebAgg server over WebSocket is
effectively lost.

Fix

Added a visibilitychange event listener in the matplotlib iframe's HTML
template. When the browser tab becomes visible again, it sends a refresh
message to the matplotlib WebAgg server. This triggers a full figure redraw
(_force_full = True + draw_idle()) that sends fresh canvas data back to
the browser, ensuring the plot is always up-to-date when the user returns.

Closes #2092

## Problem

When `mo.mpl.interactive()` plots are rendered while the browser tab is in the
background (minimized or switched to another tab), the plots appear blank.
This is because browsers throttle or skip canvas rendering in background tabs,
so the image data sent by matplotlib's WebAgg server over WebSocket is
effectively lost.

## Fix

Added a `visibilitychange` event listener in the matplotlib iframe's HTML
template. When the browser tab becomes visible again, it sends a `refresh`
message to the matplotlib WebAgg server. This triggers a full figure redraw
(`_force_full = True` + `draw_idle()`) that sends fresh canvas data back to
the browser, ensuring the plot is always up-to-date when the user returns.
@vercel
Copy link

vercel bot commented Feb 12, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment Feb 12, 2026 10:27pm

Request Review

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where mo.mpl.interactive() plots appear blank when rendered while the browser tab is in the background. The fix adds a visibilitychange event listener that refreshes the plot when the tab becomes visible again.

Changes:

  • Added JavaScript event listener in the matplotlib iframe template to detect when the browser tab becomes visible
  • Added test coverage to verify the visibilitychange listener is present in the generated HTML

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
marimo/_plugins/stateless/mpl/_mpl.py Added visibilitychange event listener to refresh matplotlib figures when browser tab becomes visible
tests/_plugins/stateless/test_mpl.py Added regression test to verify the visibilitychange listener is present in the template

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@dmadisetti dmadisetti added the bug Something isn't working label Feb 12, 2026
Copy link
Collaborator

@dmadisetti dmadisetti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failing test unrelated

@dmadisetti dmadisetti merged commit a4fedb0 into main Feb 12, 2026
38 of 53 checks passed
@dmadisetti dmadisetti deleted the ms/mpl-interactive branch February 12, 2026 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

3 participants