Skip to content

Commit

Permalink
feat: select matplotlib inline backend by default in server
Browse files Browse the repository at this point in the history
We do this by checking if the env var 'MPLBACKEND' is not set, and if so, we set it to
module://matplotlib_inline.backend_inline
  • Loading branch information
maartenbreddels committed Mar 10, 2024
1 parent 6e376a3 commit a6c43e4
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
3 changes: 1 addition & 2 deletions solara/components/matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ def Page():
You should also avoid drawing using the pyplot interface, as it is not thread-safe. If you do use it,
your drawing might be corrupted due to another thread/user drawing at the same time.
If you still must use pyplot to create the figure, make sure you call `plt.switch_backend("agg")`
before creating the figure, to avoid starting an interactive backend.
When running under solara-server, we by default configure the same 'inline' backend as in the Jupyter notebook.
For performance reasons, you might want to pass in a list of dependencies that indicate when
the figure changed, to avoid re-rendering it on every render.
Expand Down
9 changes: 9 additions & 0 deletions solara/server/patch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os
import pdb
import sys
import threading
Expand All @@ -25,6 +26,7 @@
if patch_display is not None and sys.platform != "emscripten":
patch_display()
ipywidget_version_major = int(ipywidgets.__version__.split(".")[0])
ipykernel_version_major = int(ipykernel.__version__.split(".")[0])


class FakeIPython:
Expand Down Expand Up @@ -331,6 +333,13 @@ def patch():
else:
patch_ipyreact()

if "MPLBACKEND" not in os.environ:
if ipykernel_version_major < 6:
# changed in https://github.com/ipython/ipykernel/pull/591
os.environ["MPLBACKEND"] = "ipykernel.pylab.backend_inline"
else:
os.environ["MPLBACKEND"] = "module://matplotlib_inline.backend_inline"

# the ipyvue.Template module cannot be accessed like ipyvue.Template
# because the import in ipvue overrides it
template_mod = sys.modules["ipyvue.Template"]
Expand Down
5 changes: 0 additions & 5 deletions solara/website/components/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
import solara.toestand
from solara.components.markdown import ExceptionGuard

if solara._using_solara_server():
from matplotlib import pyplot as plt

plt.switch_backend("agg")

HERE = Path(__file__).parent


Expand Down
3 changes: 0 additions & 3 deletions solara/website/pages/examples/general/live_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

import solara

# ensure that an interactive backend doesn't start when plotting with matplotlib
plt.switch_backend("agg")


@solara.component
def Page():
Expand Down

0 comments on commit a6c43e4

Please sign in to comment.