From: James Thomas <jimjoe@gmx.net>
To: Augusto Stoffel <arstoffel@gmail.com>
Cc: 62783@debbugs.gnu.org
Subject: bug#62783: [PATCH] comint-mime: Add Matplotlib support in the standard interpreter
Date: Wed, 12 Apr 2023 16:02:07 +0530 [thread overview]
Message-ID: <871qkp1jdk.fsf@gmx.net> (raw)
In-Reply-To: <87fs95ob04.fsf@gmail.com> (Augusto Stoffel's message of "Wed, 12 Apr 2023 08:44:27 +0200")
[-- Attachment #1: Type: text/plain, Size: 232 bytes --]
Augusto Stoffel wrote:
> Thanks, I will have a look at this soon.
Please prefer this slightly modified patch. It makes it possible for
users to switch backends at the beginning with matplotlib.use even if
comint-mime is enabled.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-Matplotlib-support-in-the-standard-interpreter.patch --]
[-- Type: text/x-diff, Size: 5397 bytes --]
From 502ca4a679083a2a5334ed46a1a629001a43a0f2 Mon Sep 17 00:00:00 2001
From: James Thomas <jimjoe@gmx.net>
Date: Tue, 11 Apr 2023 19:29:29 +0530
Subject: [PATCH] Add Matplotlib support in the standard interpreter
* comint-mime.py (__COMINT_MIME_setup): Add Matplotlib backend module
and loader.
---
comint-mime.py | 102 +++++++++++++++++++++++++++++++++++++------------
1 file changed, 78 insertions(+), 24 deletions(-)
diff --git a/comint-mime.py b/comint-mime.py
index d55698c..7739671 100644
--- a/comint-mime.py
+++ b/comint-mime.py
@@ -1,12 +1,15 @@
# This file is part of https://github.com/astoff/comint-mime
def __COMINT_MIME_setup(types):
+ ipython = False
try:
ipython = get_ipython()
assert ipython
except:
- print("`comint-mime' error: IPython is required")
- return
+ import importlib.util
+ if not importlib.util.find_spec("matplotlib"):
+ print("`comint-mime' error: IPython or Matplotlib is required")
+ return
from base64 import encodebytes
from functools import partial
@@ -21,19 +24,6 @@ def __COMINT_MIME_setup(types):
SIZE_LIMIT = 4000
- MIME_TYPES = {
- "image/png": encoding_workaround,
- "image/jpeg": encoding_workaround,
- "text/latex": str.encode,
- "text/html": str.encode,
- "application/json": lambda d: to_json(d).encode(),
- }
-
- if types == "all":
- types = MIME_TYPES
- else:
- types = types.split(";")
-
def print_osc(type, encoder, data, meta):
meta = meta or {}
if encoder:
@@ -48,14 +38,78 @@ def __COMINT_MIME_setup(types):
payload = encodebytes(data).decode()
print(f"\033]5151;{header}\n{payload}\033\\")
- ipython.enable_matplotlib("inline")
- ipython.display_formatter.active_types = list(MIME_TYPES.keys())
- for mime, encoder in MIME_TYPES.items():
- ipython.display_formatter.formatters[mime].enabled = mime in types
- ipython.mime_renderers[mime] = partial(print_osc, mime, encoder)
+ if ipython:
+ MIME_TYPES = {
+ "image/png": encoding_workaround,
+ "image/jpeg": encoding_workaround,
+ "text/latex": str.encode,
+ "text/html": str.encode,
+ "application/json": lambda d: to_json(d).encode(),
+ }
- if types:
- print("`comint-mime' enabled for",
- ", ".join(t for t in types if t in MIME_TYPES.keys()))
+ if types == "all":
+ types = MIME_TYPES
+ else:
+ types = types.split(";")
+
+ ipython.enable_matplotlib("inline")
+ ipython.display_formatter.active_types = list(MIME_TYPES.keys())
+ for mime, encoder in MIME_TYPES.items():
+ ipython.display_formatter.formatters[mime].enabled = mime in types
+ ipython.mime_renderers[mime] = partial(print_osc, mime, encoder)
+
+ if types:
+ print("`comint-mime' enabled for",
+ ", ".join(t for t in types if t in MIME_TYPES.keys()))
+ else:
+ print("`comint-mime' disabled")
else:
- print("`comint-mime' disabled")
+ from importlib.abc import Loader, MetaPathFinder
+ from importlib.machinery import ModuleSpec
+ from sys import meta_path
+ from os import environ
+
+ environ["MPLBACKEND"] = "module://emacscomintmime"
+
+ from matplotlib import _api
+ from matplotlib.backend_bases import FigureManagerBase
+ from matplotlib.backends.backend_agg import FigureCanvasAgg
+
+ class BackendModuleLoader(Loader):
+ def create_module(self, spec):
+ return None
+ def exec_module(self, module):
+ from io import BytesIO
+ from base64 import encodebytes
+ from json import dumps as to_json
+ from pathlib import Path
+
+ class FigureCanvasEmacsComintMime(FigureCanvasAgg):
+ manager_class = _api.classproperty(lambda cls: FigureManagerEmacsComintMime)
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+
+ class FigureManagerEmacsComintMime(FigureManagerBase):
+
+ def __init__(self, canvas, num):
+ super().__init__(canvas, num)
+
+ def show(self):
+ self.canvas.figure.draw_without_rendering()
+ buf = BytesIO()
+ self.canvas.print_png(buf)
+ print_osc("image/png", encoding_workaround, buf.getvalue(), None)
+
+ module.FigureCanvas = FigureCanvasEmacsComintMime
+ module.FigureManager = FigureManagerEmacsComintMime
+
+ class BackendModuleFinder(MetaPathFinder):
+ def find_spec(self, fullname, path, target=None):
+ if fullname == 'emacscomintmime':
+ return ModuleSpec(fullname, BackendModuleLoader())
+ else:
+ return None
+
+ meta_path.append(BackendModuleFinder())
+ print("`comint-mime' enabled for Matplotlib")
--
2.34.1
[-- Attachment #3: Type: text/plain, Size: 4 bytes --]
--
next prev parent reply other threads:[~2023-04-12 10:32 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-11 23:18 bug#62783: [PATCH] comint-mime: Add Matplotlib support in the standard interpreter James Thomas
2023-04-12 6:44 ` Augusto Stoffel
2023-04-12 10:32 ` James Thomas [this message]
2023-04-16 10:55 ` Augusto Stoffel
2023-04-20 5:17 ` James Thomas
2023-04-21 12:43 ` Augusto Stoffel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=871qkp1jdk.fsf@gmx.net \
--to=jimjoe@gmx.net \
--cc=62783@debbugs.gnu.org \
--cc=arstoffel@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).