Skip to content

Commit 4b6495c

Browse files
committed
ob-python: Improvements to :return header argument
* lisp/ob-python.el (org-babel-execute:python): Allow return-val to be non-nil in sessions, and concatenate it after the expanded body.
1 parent 583ecae commit 4b6495c

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

etc/ORG-NEWS

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,59 @@ Earlier, IDs generated using =ts= method had a hard-coded format (i.e. =20200923
2424
The new option allows user to customise the format.
2525
Defaults are unchanged.
2626

27+
** New features
28+
*** =ob-python= improvements to =:return= header argument
29+
30+
The =:return= header argument in =ob-python= now works for session
31+
blocks as well as non-session blocks. Also, it now works with the
32+
=:epilogue= header argument -- previously, setting the =:return=
33+
header would cause the =:epilogue= to be ignored.
34+
35+
This change allows more easily moving boilerplate out of the main code
36+
block and into the header. For example, for plotting, we need to add
37+
boilerplate to save the figure to a file and return the
38+
filename. Instead of doing this within the code block, we can now
39+
handle it through the header arguments as follows:
40+
41+
#+BEGIN_SRC org
42+
,#+header: :var fname="/home/jack/tmp/plot.svg"
43+
,#+header: :epilogue plt.savefig(fname)
44+
,#+header: :return fname
45+
,#+begin_src python :results value file
46+
import matplotlib, numpy
47+
import matplotlib.pyplot as plt
48+
fig=plt.figure(figsize=(4,2))
49+
x=numpy.linspace(-15,15)
50+
plt.plot(numpy.sin(x)/x)
51+
fig.tight_layout()
52+
,#+end_src
53+
54+
,#+RESULTS:
55+
[[file:/home/jack/tmp/plot.svg]]
56+
#+END_SRC
57+
58+
As another example, we can use =:return= with the external [[https://pypi.org/project/tabulate/][tabulate]]
59+
package, to convert pandas Dataframes into orgmode tables:
60+
61+
#+begin_src org
62+
,#+header: :prologue from tabulate import tabulate
63+
,#+header: :return tabulate(table, headers=table.columns, tablefmt="orgtbl")
64+
,#+begin_src python :results value raw :session
65+
import pandas as pd
66+
table = pd.DataFrame({
67+
"a": [1,2,3],
68+
"b": [4,5,6]
69+
})
70+
,#+end_src
71+
72+
,#+RESULTS:
73+
| | a | b |
74+
|---+---+---|
75+
| 0 | 1 | 4 |
76+
| 1 | 2 | 5 |
77+
| 2 | 3 | 6 |
78+
#+end_src
79+
2780
* Version 9.4
2881
** Incompatible changes
2982
*** Possibly broken internal file links: please check and fix

lisp/ob-python.el

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,16 @@ This function is called by `org-babel-execute-src-block'."
8181
(cdr (assq :session params))))
8282
(result-params (cdr (assq :result-params params)))
8383
(result-type (cdr (assq :result-type params)))
84-
(return-val (when (and (eq result-type 'value) (not session))
84+
(return-val (when (eq result-type 'value)
8585
(cdr (assq :return params))))
8686
(preamble (cdr (assq :preamble params)))
8787
(full-body
88-
(org-babel-expand-body:generic
89-
(concat body (if return-val (format "\nreturn %s" return-val) ""))
90-
params (org-babel-variable-assignments:python params)))
88+
(concat
89+
(org-babel-expand-body:generic
90+
body params
91+
(org-babel-variable-assignments:python params))
92+
(when return-val
93+
(format (if session "\n%s" "\nreturn %s") return-val))))
9194
(result (org-babel-python-evaluate
9295
session full-body result-type result-params preamble)))
9396
(org-babel-reassemble-table

0 commit comments

Comments
 (0)