Org-babel: referring to variables from different code block

864 Views Asked by At

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
1

There are 1 best solutions below

0
Jakub Bartczuk On BEST ANSWER

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