Returning multiple files from src block in babel

Often, when creating a plot I would like several plots with the data. It's possible to return the name of one file as output, using results: file, but what about returning several?

For reference, I'd like a solution that works for any language, but I'm working with Python at the moment.

1 Answer

This is a solution for python, using noweb syntax. It saves all open matplotlib figures, but the idea can be extended easily. First off, make sure noweb is enabled in settings. Then, define a function which returns a list of filenames.

#+name: plt-save
#+begin_src python :exports results :results verbatim
idx = get_fignums()
files = []

for i in idx:
figure(i)
files.append('{0}_{1}.png'.format(path, i))
savefig(numbered[-1], bbox_inches='tight')
return "\n".join(["[[file:{0}]]".format(f) for f in files])
#+end_src


Then, in the block where the figures are to be saved, define a variable path, call <<plt-save>>, and set the results to verbatim.

#+begin_src python :results verbatim

# Do processing here#
path='features_heatmap'
<<plt-save>>
#+end_src