From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: James Thomas Newsgroups: gmane.emacs.bugs Subject: bug#62783: [PATCH] comint-mime: Add Matplotlib support in the standard interpreter Date: Wed, 12 Apr 2023 16:02:07 +0530 Message-ID: <871qkp1jdk.fsf@gmx.net> References: <873556t3db.fsf@outlook.com> <87fs95ob04.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="27370"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 62783@debbugs.gnu.org To: Augusto Stoffel Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Apr 12 12:33:25 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pmXn2-0006so-NY for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 12 Apr 2023 12:33:24 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pmXmj-0000el-VB; Wed, 12 Apr 2023 06:33:06 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pmXmg-0000eU-Pg for bug-gnu-emacs@gnu.org; Wed, 12 Apr 2023 06:33:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pmXmg-0004LT-FF for bug-gnu-emacs@gnu.org; Wed, 12 Apr 2023 06:33:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pmXmg-0001as-2T for bug-gnu-emacs@gnu.org; Wed, 12 Apr 2023 06:33:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: James Thomas Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 12 Apr 2023 10:33:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 62783 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: notabug patch Original-Received: via spool by 62783-submit@debbugs.gnu.org id=B62783.16812955436070 (code B ref 62783); Wed, 12 Apr 2023 10:33:01 +0000 Original-Received: (at 62783) by debbugs.gnu.org; 12 Apr 2023 10:32:23 +0000 Original-Received: from localhost ([127.0.0.1]:39124 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pmXm2-0001Zn-KN for submit@debbugs.gnu.org; Wed, 12 Apr 2023 06:32:23 -0400 Original-Received: from mout.gmx.net ([212.227.15.18]:35195) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pmXlz-0001ZX-9t for 62783@debbugs.gnu.org; Wed, 12 Apr 2023 06:32:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=s31663417; t=1681295532; i=jimjoe@gmx.net; bh=KAdaT6Pxx7gOPBdUDk/VS/3sA7Pa40JnaMMPBpFtb2c=; h=X-UI-Sender-Class:From:To:Cc:Subject:In-Reply-To:References:Date; b=UgWTr7Oeyc6/kl+k4rnLaAGKX7mq23iSdjNq4Re/7y7uM0fShvRfG9U3/ecHqQukT f4dVbCjTGZElX9admZeNRMriPdFk9gfUgN5q7twqI4n3A6z4E8fGqywT9wXQOX5z9i S1IHGR42nSO/XRkeF1Jx3VVCo41aHw1cWHnRM4eO6D/+GPeDngkPsdM/luxR0C18Ru /e6eO55KxTXmatTBAV0ZUzfVo8lx00kv41NForvTUNu1N1SiJKXMmYY23IYwO87VQT 3sLSA6wxwKHdiFRfRTj5KLglgmHHyAv20gOeLZDLOe3au39xKVyF/o9B0oEsYwDOyP G/assDBCOo0AQ== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Original-Received: from user-Inspiron-15-5518 ([202.88.237.228]) by mail.gmx.net (mrgmx004 [212.227.17.184]) with ESMTPSA (Nemesis) id 1M7Jza-1pjFzD1ukW-007pFE; Wed, 12 Apr 2023 12:32:12 +0200 In-Reply-To: <87fs95ob04.fsf@gmail.com> (Augusto Stoffel's message of "Wed, 12 Apr 2023 08:44:27 +0200") X-Provags-ID: V03:K1:msoFZNDZlI4jTsVQtILKdfe2/5gA3bI8XubWqbc3iaV+twWdGcs hVTEjA0H3XLLvQMu/1TxmIoURUiTxazfJCacgEOfZaCDxt6usBWFhw7g1mvfFxDw6ngG16x JfTYXvNo7D/eIZ7MG2OkPLLEpDIKZcu4Djz/KVI+clt5Zf4I0GZ36QeRE9ySuIAKkZ/dkrM 1YrAqB25ZXySZ63FpHcmQ== UI-OutboundReport: notjunk:1;M01:P0:PgB6bNOqy0o=;lXn1vMA2UQ/g1Cz2CJQQB6nPKyv +ekuP9wVrTgEN2vMq7JfhPTxAiK6y6zwwZ2uP/nG4LqtRVCAGMfpTxOaVb4TTrGlfBhNi9sLd h0IpG1TZebqJk+AiIjphnX5gsO9TU4Cm55gg4pVtnOOatQyC/ffmowZCWtLPu9IIudqUS49zg Kd9g20xdoCRCZo5MQpyUmdWxe278MZoHc9SSNeBM6s8oPq6HduH6AQgQJhRrYJgNCHTatDawW aX+4SYQpNg2VZYGVvAncilRvUDgLcdvOUYBTJeH2qwrMQh8uruufogSR436vhRDfK41V04a8y OTXNPM7VmqVmhJMPqb4SwZ5vltGrB5TEI65KXBzwJFouuD37saXeypC10JDlaaEF5QiKpC64O ZmpjujB1oratIQEjcztPrkBmfhFMsQsWxdXx6Zj9uqT/ORY7PSK9+sNEWnaE+P+//SajSFUUn uPZy5dv27rmz3LEeqH9bpfaVpUmCzO8pPiCHsotgm8uav7VcBn0Hl9OTqt/A0/Pdv1JmDiObu xN5ivoNIXJ2K07lwnBnsbcbvBqncZUzbvjuXumKH82+mV5EE1IZ/ERa1TvkEad7dAwaQN2DQ8 IzPdCso7LRz4Cj4GMUYfTBkcTrd7p16PQ/ePgNjsGjNROmSoIDb4By7VDSU6ygRlKHSpXyRTm 21/jjFZqUO97QNAlc2Bal8TDbdtOzVmL6RBLASsYZsDbBM3JPpxa0okGvHh4O/BUxcZxv0yGT AIdsk627pDTgF5A+j7X9iJpfKIXu4dKmMgSiE1TiSGduPdcuIgGxzOTEf/ttq1aZb7fNZBpF X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:259719 Archived-At: --=-=-= Content-Type: text/plain 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. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Add-Matplotlib-support-in-the-standard-interpreter.patch Content-Transfer-Encoding: quoted-printable =46rom 502ca4a679083a2a5334ed46a1a629001a43a0f2 Mon Sep 17 00:00:00 2001 From: James Thomas 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. =2D-- 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 =2D-- 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 =3D False try: ipython =3D 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 =3D 4000 - MIME_TYPES =3D { - "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 =3D=3D "all": - types =3D MIME_TYPES - else: - types =3D types.split(";") - def print_osc(type, encoder, data, meta): meta =3D meta or {} if encoder: @@ -48,14 +38,78 @@ def __COMINT_MIME_setup(types): payload =3D encodebytes(data).decode() print(f"\033]5151;{header}\n{payload}\033\\") - ipython.enable_matplotlib("inline") - ipython.display_formatter.active_types =3D list(MIME_TYPES.keys()) - for mime, encoder in MIME_TYPES.items(): - ipython.display_formatter.formatters[mime].enabled =3D mime in ty= pes - ipython.mime_renderers[mime] =3D partial(print_osc, mime, encoder= ) + if ipython: + MIME_TYPES =3D { + "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 =3D=3D "all": + types =3D MIME_TYPES + else: + types =3D types.split(";") + + ipython.enable_matplotlib("inline") + ipython.display_formatter.active_types =3D list(MIME_TYPES.keys()= ) + for mime, encoder in MIME_TYPES.items(): + ipython.display_formatter.formatters[mime].enabled =3D mime i= n types + ipython.mime_renderers[mime] =3D partial(print_osc, mime, enc= oder) + + 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"] =3D "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 =3D _api.classproperty(lambda cls: Figu= reManagerEmacsComintMime) + + 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 =3D BytesIO() + self.canvas.print_png(buf) + print_osc("image/png", encoding_workaround, buf.g= etvalue(), None) + + module.FigureCanvas =3D FigureCanvasEmacsComintMime + module.FigureManager =3D FigureManagerEmacsComintMime + + class BackendModuleFinder(MetaPathFinder): + def find_spec(self, fullname, path, target=3DNone): + if fullname =3D=3D 'emacscomintmime': + return ModuleSpec(fullname, BackendModuleLoader()) + else: + return None + + meta_path.append(BackendModuleFinder()) + print("`comint-mime' enabled for Matplotlib") =2D- 2.34.1 --=-=-= Content-Type: text/plain -- --=-=-=--