5

I'm wondering how to access the :file header argument passed to a src block, from inside the src block itself?

I'm wondering specifically for python, and in this case ipython with %matplotlib inline as the example below (here I use the ob-ipython library)

#+begin_src ipython :session :file /tmp/py87133eo.png :exports results
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

#+end_src
Sparx
  • 1,111
  • 9
  • 20
  • 1
    You could add it via `:var file=/tmp/py87133eo.png` according to http://orgmode.org/manual/var.html#var – Konstantin Morenko Dec 14 '16 at 11:22
  • 1
    The issue with that is that I also want to produce the figure as a file link ones the data has been plotted, which suggests to me that I still would need the :file argument to specify the output. Also, python and I guess other languages knows about the :file header argument somehow. – Sparx Dec 14 '16 at 12:53
  • 2
    I don't think you can access the header in the body. For one, it is python in the body, and at best you might hope for some kind of preprocessing that would do some variable expansion. If what you want is inline figures, see http://kitchingroup.cheme.cmu.edu/blog/2017/01/29/ob-ipython-and-inline-figures-in-org-mode/ for some code I use for that purpose. – John Kitchin May 13 '17 at 18:53
  • Thanks @JohnKitchin for your suggestion! Posted this so long ago that I can't remember my use case anymore. Why I felt I needed to do this. – Sparx May 15 '17 at 23:10

1 Answers1

1

You could wrap the output with next code and create file and link after block

#+name: attr_wrap
#+begin_src sh :session :var file=/tmp/py87133eo.png
  echo "$data" > /tmp/$file
  ln -s /tmp/$file /tmp/link
#+end_src

#+begin_src ipython :session :exports results :post attr_wrap(data=*this*) :results output
  %matplotlib inline
  import matplotlib.pyplot as plt
  import numpy as np
#+end_src
Konstantin Morenko
  • 1,407
  • 9
  • 19