Org-mode code evaluation seems to be a great tool for literate programming. Unfortunately, I couldn't find many examples on that online. I would like to pass variables (in my case python) between code blocks without the use of the session argument in the header. I know that using sessions you can do it, e.g.

#+BEGIN_SRC python :session sample :results none
x = 12

#+BEGIN_SRC python :results output :session sample
print 'x=' + str(x)

Is it possible to pass variables by using the "var" header argument?

  • If you can make the first block return the value you want to assign to the variable in the second, then yes, but it's not possible for arbitrary variables you use in the code block.
    – wvxvw
    Dec 25 '15 at 12:29
  • org-babel provides an extensive reference card with various examples. some reusable code can be found in the /doc/library-of-babel.org of your orgmode installation.
    – mutbuerger
    Dec 25 '15 at 19:02

If you name the block you can use its output as input into another block using the :var variable=block_name option:

#+name: first_block
#+BEGIN_SRC python
x = 12
return x

#+BEGIN_SRC python :var x=first_block
return int(x)+1

The second block will always get x as a string when the first block returns a basic types like int and float or as a list of tuples if it returns a table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.