Ihor Radchenko <yanta...@posteo.net> 于2023年7月3日周一 17:28写道:
> This might be a useful feature, but it will break the existing > conventions, if used as in the patch. Currently, the above combination > of :file and :results is treated as the following: > > ‘graphics’ > When used along with ‘file’ type, the result is a link to the file > specified in ‘:file’ header argument. However, unlike plain ‘file’ > type, code block output is not written to the disk. The block is > expected to generate the file by its side-effects only, as in the > following example: > > #+begin_src shell :results file link :file "org-mode-unicorn.svg" > wget -c "https://orgmode.org/resources/img/org-mode-unicorn.svg" > #+end_src > > #+RESULTS: > [[file:org-mode-unicorn.svg]] > > If ‘:file’ header argument is omitted, interpret source block > result as the file path. I have updated the patch and ‘:file’ header argument can be omitted now, e.g. #+begin_src python :results graphics file import matplotlib.pyplot as plt plt.plot([1,2,3,4,5]) plt.savefig('test.png') return 'test.png' #+end_src In this case, `graphics' can be removed too. > What if the user wants to use, for example, pyplot instead of > matplotlib? pyplot is a module of matplotlib. If you mean users want to use other graphics libraries, they may advise `org-babel-python-save-graphics' if they want to use `graphics' in the header argument. If necessary, ob-python may support such libraries too.
From 8ea69d66552ee0217b7f5c8089b91424b92c3b5a Mon Sep 17 00:00:00 2001 From: Liu Hui <liuhui1...@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 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lisp/ob-python.el b/lisp/ob-python.el index 0e0539d7a..6ee52313c 100644 --- a/lisp/ob-python.el +++ b/lisp/ob-python.el @@ -72,6 +72,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 +84,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 +154,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