Skip to content

Commit

Permalink
fix: mirror matplotlib bug in our virtual kernel patch
Browse files Browse the repository at this point in the history
Our .clear on RcParams was working, while matplotlib was a no-op:
   matplotlib/matplotlib#25855

We now mirror this behaviour.

Fixes #606
  • Loading branch information
maartenbreddels committed Jan 31, 2025
1 parent cff542a commit 318567e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions solara/server/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,13 @@ def _get(self, key):
# same as _get
return self[key]

def clear(self):
# in matplotlib .clear is effectively a no-op
# see https://github.com/matplotlib/matplotlib/issues/25855
pass
# in the future, we may want to clear the context dict if this is fixed
# self._get_context_dict().clear()

def _get_context_dict(self) -> dict:
if not self._was_initialized:
# since we monkey patch the class after __init__ was called
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/matplotlib_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
from matplotlib.figure import Figure
from matplotlib import pyplot as plt
import matplotlib
from pathlib import Path


import solara
import solara.server.patch
from solara.server import kernel
from solara.server.app import AppScript

HERE = Path(__file__).parent


@solara.component
Expand Down Expand Up @@ -75,3 +82,14 @@ def test_pylab(no_kernel_context):
cleanup()
context_1.close()
context_2.close()


def test_clear_bug_in_matplotlib(no_kernel_context, kernel_context):
name = str(HERE / "solara_test_apps" / "single_file.py")
app = AppScript(name)
app.init()
with kernel_context:
# see https://github.com/matplotlib/matplotlib/issues/25855
with plt.style.context({}, after_reset=True):
pass
matplotlib.get_backend().lower()

0 comments on commit 318567e

Please sign in to comment.