I have an org-mode
file containing a table of data and two Python code blocks to extract different summaries from it.
I would like to share some common constants and functions between these two code blocks. Ideally, I'd do this by factoring out the common code into a separate code block, which would be automatically included and evaluated whenever either of the other two blocks is evaluated. In made-up syntax, it would look something like the following:
#+NAME: init_block
#+BEGIN_SRC python
... common constants and functions here ...
#+END_SRC
#+NAME: summary_1
#+BEGIN_SRC python :prepend init_block
... data-processing code depending on init code goes here ...
#+END_SRC
#+NAME: summary_2
#+BEGIN_SRC python :prepend init_block
... more processing which also depends on init code ...
#+END_SRC
I suppose I could use the :session
option, but I would prefer not to, for two reason. First, it sets up a stateful system, rather than one which runs from scratch each time I use C-c C-c
on a code block. Second, and relatedly, I now have to remember to manually evaluate the common initialization code each time I open the file: I can't just update the data table, go to one of the summary blocks and hit C-c C-c
to update it.
Is there a good way to do this?