From: Liu Hui <liuhui1610@gmail.com>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: Jack Kamm <jackkamm@gmail.com>, emacs-orgmode@gnu.org
Subject: Re: [PATCH] ob-python: support header argument `:results file graphics'
Date: Wed, 5 Jul 2023 16:09:26 +0800 [thread overview]
Message-ID: <CAOQTW-PUmrO_Qit_qDDdnbC35y8SUJAxAwP6sXh_jR9PaG1tSg@mail.gmail.com> (raw)
In-Reply-To: <877crfvrsf.fsf@localhost>
[-- Attachment #1: Type: text/plain, Size: 815 bytes --]
Ihor Radchenko <yantar92@posteo.net> 于2023年7月4日周二 19:29写道:
> That said, your patch should still work fine even with these
> considerations. But we may need to sort out this undocumented behaviour.
> Probably, an error like in `org-babel-graphical-output-file' should be
> thrown by `org-babel-generate-file-param'.
Yes. The main concern is that some users may rely on the undocumented
behavior.
> What about
>
> #+begin_src python :file "test.png" :results graphics file
> import matplotlib.pyplot as plt
> plt.plot([1,2,3,4,5])
> plt.savefig('test.png')
> plt.plot([5,4,3,2,1])
> plt.show()
> #+end_src
OK, I think that it is better to turn off the feature by default and I
have updated the patch. Inspired by ob-R, some extra header arguments
are supported. Thanks.
[-- Attachment #2: 0001-ob-python-Graphics-output-enhancement.patch --]
[-- Type: text/x-patch, Size: 3367 bytes --]
From 5f3fc8602c0eb1f6d0c072bd0c1a6bb883b8c9f9 Mon Sep 17 00:00:00 2001
From: Liu Hui <liuhui1610@gmail.com>
Date: Wed, 5 Jul 2023 12:48:36 +0800
Subject: [PATCH] ob-python: Graphics output enhancement
* lisp/ob-python.el (org-babel-python-graphics-output-function): New
variable.
(org-babel-execute:python): Allow to save graphics output with a
user-defined function when the result type is `file graphics'.
(org-babel-python-matplotlib-graphics-output): New helper function to
generate code to save matplotlib-based graphics.
---
lisp/ob-python.el | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index 0e0539d7a..4c304f83b 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -62,6 +62,14 @@ (defcustom org-babel-python-None-to 'hline
:package-version '(Org . "8.0")
:type 'symbol)
+(defvar org-babel-python-graphics-output-function nil
+ "A function generating Python code for producing graphics output.
+Specifically, for src blocks with `:results file graphics' header
+argument, the code returned by the function will be appended to
+the end of the src block and should produce the graphics file.
+It can be set to `org-babel-python-matplotlib-graphics-output'
+for matplotlib-based graphics.")
+
(defun org-babel-execute:python (body params)
"Execute a block of Python code with Babel.
This function is called by `org-babel-execute-src-block'."
@@ -72,6 +80,9 @@ (defun org-babel-execute:python (body params)
(cdr (assq :session params))))
(result-params (cdr (assq :result-params params)))
(result-type (cdr (assq :result-type params)))
+ (graphics-file (and (member "graphics" result-params)
+ (ignore-errors
+ (org-babel-graphical-output-file params))))
(return-val (when (eq result-type 'value)
(cdr (assq :return params))))
(preamble (cdr (assq :preamble params)))
@@ -81,6 +92,10 @@ (defun org-babel-execute:python (body params)
(org-babel-expand-body:generic
body params
(org-babel-variable-assignments:python params))
+ (when graphics-file
+ (if (functionp org-babel-python-graphics-output-function)
+ (funcall org-babel-python-graphics-output-function
+ graphics-file params)))
(when return-val
(format (if session "\n%s" "\nreturn %s") return-val))))
(result (org-babel-python-evaluate
@@ -149,6 +164,24 @@ (defun org-babel-python-table-or-string (results)
res)
res)))
+(defun org-babel-python-matplotlib-graphics-output (out-file params)
+ "Return the Python code for saving graphics to `OUT-FILE'."
+ (let* ((allowed-args '(:dpi :format :backend))
+ (args (mapconcat
+ (lambda (pair)
+ (if (member (car pair) allowed-args)
+ (format ",%s=%S"
+ (substring (symbol-name (car pair)) 1)
+ (cdr pair)) ""))
+ params "")))
+ (format "
+import matplotlib.pyplot
+if len(matplotlib.pyplot.get_fignums()) > 0:
+ matplotlib.pyplot.gcf().savefig('%s'%s)
+else:
+ raise RuntimeWarning('No figure is found')"
+ out-file args)))
+
(defvar org-babel-python-buffers '((:default . "*Python*")))
(defun org-babel-python-session-buffer (session)
--
2.25.1
next prev parent reply other threads:[~2023-07-05 8:10 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-03 4:31 [PATCH] ob-python: support header argument `:results file graphics' Liu Hui
2023-07-03 9:28 ` Ihor Radchenko
2023-07-03 10:40 ` Liu Hui
2023-07-03 11:41 ` Ihor Radchenko
2023-07-03 13:23 ` Liu Hui
2023-07-04 11:29 ` Ihor Radchenko
2023-07-05 5:23 ` Jack Kamm
2023-07-05 11:05 ` Ihor Radchenko
2023-07-06 2:49 ` Jack Kamm
2023-07-07 10:53 ` Ihor Radchenko
2023-07-08 13:55 ` Jack Kamm
2023-07-09 9:12 ` Ihor Radchenko
2023-07-12 5:10 ` Jack Kamm
2023-07-12 8:38 ` Ihor Radchenko
2023-07-14 2:47 ` Jack Kamm
2023-07-05 8:09 ` Liu Hui [this message]
2023-07-05 4:55 ` Jack Kamm
2023-07-07 10:56 ` Ihor Radchenko
2023-07-05 5:13 ` Jack Kamm
2023-07-05 8:11 ` Liu Hui
2023-07-06 3:49 ` Jack Kamm
2023-07-06 9:54 ` Liu Hui
2023-07-08 13:59 ` Jack Kamm
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.orgmode.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAOQTW-PUmrO_Qit_qDDdnbC35y8SUJAxAwP6sXh_jR9PaG1tSg@mail.gmail.com \
--to=liuhui1610@gmail.com \
--cc=emacs-orgmode@gnu.org \
--cc=jackkamm@gmail.com \
--cc=yantar92@posteo.net \
/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/org-mode.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).