emacsorg-modeorg-babel

Org-babel: referring to variables from different code block


I want to check out if org-mode could be a good replacement for Jupyter notebooks. Is there a way to export all variables from a code block to other code blocks?

Example:

#+BEGIN_SRC python
a = 1
b = 2
#+END_SRC

How could I use this in other code block? For example I'd like the following to evaluate so that c = 3

#+BEGIN_SRC python
c = a + b
#+END_SRC

Solution

  • It can be achieved using session feature:

    #+NAME b1
    #+BEGIN_SRC python :session example :results output
    a = 1
    b = 2
    #+END_SRC
    
    #+BEGIN_SRC python :session example :results output
    c = a + b
    print(c)
    #+END_SRC
    

    Will return

    #+RESULTS:
    : 3