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 04:48:08 +0530 Message-ID: <873556t3db.fsf@outlook.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="11370"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: arstoffel@gmail.com To: 62783@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Apr 12 01:19:16 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 1pmNGc-0002m7-K1 for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 12 Apr 2023 01:19:14 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pmNGS-0000aW-NQ; Tue, 11 Apr 2023 19:19:04 -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 1pmNGR-0000aG-53 for bug-gnu-emacs@gnu.org; Tue, 11 Apr 2023 19:19:03 -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 1pmNGQ-0003hy-Q6 for bug-gnu-emacs@gnu.org; Tue, 11 Apr 2023 19:19:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pmNGQ-00056J-AC; Tue, 11 Apr 2023 19:19:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: James Thomas Original-Sender: "Debbugs-submit" Resent-CC: arstoffel@gmail.com, bug-gnu-emacs@gnu.org Resent-Date: Tue, 11 Apr 2023 23:19:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 62783 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: notabug patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org X-Debbugs-Original-Xcc: arstoffel@gmail.com Original-Received: via spool by submit@debbugs.gnu.org id=B.168125510519562 (code B ref -1); Tue, 11 Apr 2023 23:19:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 11 Apr 2023 23:18:25 +0000 Original-Received: from localhost ([127.0.0.1]:38490 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pmNFo-00055Q-P3 for submit@debbugs.gnu.org; Tue, 11 Apr 2023 19:18:25 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:43564) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pmNFk-00055F-0A for submit@debbugs.gnu.org; Tue, 11 Apr 2023 19:18:23 -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 1pmNFi-0000Ya-SS for bug-gnu-emacs@gnu.org; Tue, 11 Apr 2023 19:18:19 -0400 Original-Received: from mout.gmx.net ([212.227.17.20]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pmNFg-0003bl-1y for bug-gnu-emacs@gnu.org; Tue, 11 Apr 2023 19:18:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=s31663417; t=1681255092; i=jimjoe@gmx.net; bh=FxsazpeAngn3tlcUYMp/iW5eZZBI1AULIInmRznsICQ=; h=X-UI-Sender-Class:From:To:Subject:Date; b=keSHpCRJXmwqTcnSlwEp3EpkrIFO4Wms9eJgzZgbITHo05Stm8sKxn1JoGhI02VAD 9Ix7wvh6CQo3AK6vwJoGY4NPKQV5ulP4PjsaCzM8UBl8PrXPt5cl0C8BhLqZGlP2qd Yfr0ShQGnr9o6bP0YdlqkVmVN4kt5gTzt9s3HKmUEPeZhVw5j32BY1a5HZUz75blUt r7NUeap+7YhI8AQ9omtEkPr4C44UG+4YTmkzZsUdD5DxjJSK4ZatmCEtfNReLJanlq PRiVinHD3BbtcW1LoC5Ei6S7IgMGBiZDd6xAVadaoSPwfEeg+pRG1MkrMU830clYG8 JlieR1Mfpbjng== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Original-Received: from user-Inspiron-15-5518 ([117.194.153.45]) by mail.gmx.net (mrgmx104 [212.227.17.174]) with ESMTPSA (Nemesis) id 1M42jQ-1pmNFb19OK-0005qG for ; Wed, 12 Apr 2023 01:18:12 +0200 X-Provags-ID: V03:K1:VP+t6zpdmLb89uSouJWGUhlEc/tUDwf+4NWqh7+U58vcYUIZgPY Hej5WPfCm4j0OI6N5PPancUrwTfSXhqj+G+6pVyjmXQJypYu0d2yaYEDIOd+IU+bWw6GCNh iw7x6o/hMRj19y3wZxHfbNR9Wv/19gMjafiXVUdXDXnNv9/vWrF6A+KDURN7gR6v2wT4SqX by+PhzLKRHr68o4V0oXQQ== UI-OutboundReport: notjunk:1;M01:P0:Vm3YdwY8K44=;gHPO7cEDG05ICp0OAFqBHV0t8FE KUiiqb5BdIa0HL2NZ+M8F0zolrNsxfIRGi8vawVJJq0zEr7S88Q3Yyv7xWur3T2V//l/cVETF o+h7IE40xRt6zxtPjz0rld7Nu7VF1gqD/xD36+HXDL4I1V3uCsAn5pewwsMKrcfGV5a9W/rTV 0xT9fN8OLOlPDzwFix/7g7JPm9Y9DJ4x/A0mY2pKUuw/59PIvd9EueBYMeYucHR84T1LEPJiy 1aiyRpmjUVPXX0xbS1ZImIWlWDmnGurUzUQpfXbR0loZ/q+WAqgo5PSX4muOwofPPKvTAeqRa C07VB+OsVO/0m/YWVIYTHE9Ybp3D0hUJNPwEgJTDMkWyeTpxrelfu24rvf7JH5pN2oyJ7eL7E 85Qo4P0Ll8S5g1WDB3cobWgAyB2GRn3dRW5n3yFyxh6K3oagvSI97I5I01R19HBuNFU46Cnv1 lvib7rszGGpbSeS0KTXA5v3D9Ey45aP2E39CrHi3m23+KfGzO3ltl47tZyZs5rX9mi4hQmJS7 /NVAeujN8ElaPOZz8eIleU3l98T72uHH8GQv9Mt+IDmI7JWqWxeP0sgl6aZecCAgN1mF4Eh9g pqtICkvGTdTdlvyIthWvqGbcvqGo3CwoCYSxoarLE/d6HDtAxO8phjueEKBPA+9YR5W+Z/WQS uYLShhuFlUC36tWHw6GLhleQbMqOAyFHe+3avDckzF6HiYLBsdIvNF0AcpLNokPxQsKVHfMT9 MhWGTLfFr6yIBoZKttN1RU+n2j4yV9SEmsgEpEzkP4igkuE+C29ZWNIJSg9YE4qevGijftFo Received-SPF: pass client-ip=212.227.17.20; envelope-from=jimjoe@gmx.net; helo=mout.gmx.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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:259685 Archived-At: --=-=-= Content-Type: text/plain Severity: wishlist Tags: notabug, patch This adds support for Matplotlib using the standard interpreter (rather than IPython). It was tested like this: M-x run-python M-x comint-mime-setup >>> import matplotlib.pyplot as plt >>> fig, ax = plt.subplots() >>> ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) >>> plt.show() I haven't tested whether this breaks the existing IPython functionality - but see no reason why it should. In GNU Emacs 29.0.60 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.33, cairo version 1.16.0) of 2023-04-01 built on user-Inspiron-15-5518 Repository revision: 6419d78fa6f8a7794893da5a8a5d65f75a5a29fa Repository branch: master Windowing system distributor 'The X.Org Foundation', version 11.0.12101003 System Description: Ubuntu 22.04.2 LTS --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-Add-Matplotlib-support-in-the-standard-interpreter.patch Content-Transfer-Encoding: quoted-printable =46rom 1da8d5f35fb9009af3bded259f47e3ae07b26fd9 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 | 100 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 76 insertions(+), 24 deletions(-) diff --git a/comint-mime.py b/comint-mime.py index d55698c..6c7425c 100644 =2D-- a/comint-mime.py +++ b/comint-mime.py @@ -1,12 +1,16 @@ # 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 + try: + from matplotlib import _api + except: + print("`comint-mime' error: IPython or Matplotlib is required= ") + return from base64 import encodebytes from functools import partial @@ -21,19 +25,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 +39,75 @@ 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 matplotlib.backend_bases import FigureManagerBase + from matplotlib.backends.backend_agg import FigureCanvasAgg + from matplotlib import use + + 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()) + use("module://emacscomintmime") + print("`comint-mime' enabled for Matplotlib") =2D- 2.34.1 --=-=-= Content-Type: text/plain (This is a duplicate message to the maintainer; because I suspect that a previous direct follow-up of mine to him has gone to spam) -- --=-=-=--