@@ -24,6 +24,59 @@ Earlier, IDs generated using =ts= method had a hard-coded format (i.e. =20200923
2424The new option allows user to customise the format.
2525Defaults 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
0 commit comments