-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
987a5ea
commit 62173fc
Showing
6 changed files
with
79 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# patch implemented in Pyolite | ||
# https://github.com/jupyterlite/jupyterlite/blob/0d563b9a4cca4b54411229128cb51ac4ba333c8f/packages/pyolite-kernel/py/pyolite/pyolite/patches.py | ||
def patch_matplotlib(): | ||
import os | ||
from io import BytesIO | ||
|
||
# before importing matplotlib | ||
# to avoid the wasm backend (which needs `js.document`, not available in worker) | ||
os.environ["MPLBACKEND"] = "AGG" | ||
|
||
import matplotlib.pyplot | ||
from IPython.display import display | ||
|
||
from .display import Image | ||
|
||
_old_show = matplotlib.pyplot.show | ||
assert _old_show, "matplotlib.pyplot.show" | ||
|
||
def show(): | ||
buf = BytesIO() | ||
matplotlib.pyplot.savefig(buf, format="png") | ||
buf.seek(0) | ||
display(Image(buf.read())) | ||
matplotlib.pyplot.clf() | ||
|
||
matplotlib.pyplot.show = show | ||
|
||
|
||
def patch_pillow(): | ||
import base64 | ||
|
||
from PIL import Image as PILImage | ||
|
||
_old_repr_png = PILImage.Image._repr_png_ | ||
assert _old_repr_png | ||
|
||
def _repr_png_(self): | ||
byte = _old_repr_png(self) | ||
return base64.b64encode(byte).decode("utf-8") | ||
|
||
PILImage.Image._repr_png_ = _repr_png_ | ||
|
||
|
||
ALL_PATCHES = [ | ||
patch_pillow, | ||
patch_matplotlib, | ||
] | ||
|
||
|
||
def apply_patches(): | ||
import warnings | ||
|
||
for patch in ALL_PATCHES: | ||
try: | ||
patch() | ||
except Exception as err: | ||
warnings.warn("faield to apply patch", patch, err) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters