emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] ob-python: support header argument `:results file graphics'
@ 2023-07-03  4:31 Liu Hui
  2023-07-03  9:28 ` Ihor Radchenko
  2023-07-05  5:13 ` Jack Kamm
  0 siblings, 2 replies; 23+ messages in thread
From: Liu Hui @ 2023-07-03  4:31 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 550 bytes --]

Hi,

This patch adds graphics output support for ob-python via matplotlib.
Specifically, it allows to use header argument `:results file
graphics' as follows:

#+begin_src python :file "test.png" :results graphics file
import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5])
#+end_src

The feature is described in the documentation as follows and has been
supported by ob-R, ob-julia, etc.

> ‘graphics’
>      When used along with ‘file’ type, the result is a link to the file
>      specified in ‘:file’ header argument.

[-- Attachment #2: 0001-ob-python-support-header-argument-results-file-graph.patch --]
[-- Type: text/x-patch, Size: 2311 bytes --]

From ee8d236310cd4bde4e33610b2b794022861fbd6a Mon Sep 17 00:00:00 2001
From: Liu Hui <liuhui1610@gmail.com>
Date: Mon, 3 Jul 2023 11:13:58 +0800
Subject: [PATCH] ob-python: support header argument `:results file graphics'

* lisp/ob-python.el (org-babel-execute:python): Save current figure to
file when the result type is `file graphics'.
(org-babel-python-save-graphics): New helper function to generate code
to save graphics.
---
 lisp/ob-python.el | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index 0e0539d7a..9fae49a4d 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -72,6 +72,8 @@ (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)
+                             (org-babel-graphical-output-file params)))
 	 (return-val (when (eq result-type 'value)
 		       (cdr (assq :return params))))
 	 (preamble (cdr (assq :preamble params)))
@@ -81,6 +83,8 @@ (defun org-babel-execute:python (body params)
 	   (org-babel-expand-body:generic
 	    body params
 	    (org-babel-variable-assignments:python params))
+           (when graphics-file
+             (org-babel-python-save-graphics graphics-file params))
 	   (when return-val
 	     (format (if session "\n%s" "\nreturn %s") return-val))))
          (result (org-babel-python-evaluate
@@ -149,6 +153,21 @@ (defun org-babel-python-table-or-string (results)
                 res)
       res)))
 
+(defun org-babel-python-save-graphics (out-file _)
+  "Return the code for saving graphics to `OUT-FILE'."
+  (format "
+try:
+    import matplotlib.pyplot
+    if len(matplotlib.pyplot.get_fignums()) > 0:
+        matplotlib.pyplot.gcf().savefig('%s')
+    else:
+        raise RuntimeError('No figure is found. You need to use matplotlib\
+ functions, such as plot and imshow, to produce the graphics.')
+except ModuleNotFoundError:
+    raise RuntimeError('ob-python depends on matplotlib for saving graphics')
+"
+          out-file))
+
 (defvar org-babel-python-buffers '((:default . "*Python*")))
 
 (defun org-babel-python-session-buffer (session)
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2023-07-14  2:48 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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).