emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to associate a code block to another one so that it is executed


From: John Kitchin
Subject: Re: How to associate a code block to another one so that it is executed beforehand?
Date: Mon, 15 Feb 2021 16:52:07 -0500

Here is one way to do it.  You use a :var to "run" the other block.

#+NAME: create-file
#+begin_src bash :results silent
cat << EOF > main.txt
foo
bar
EOF
#+end_src

#+BEGIN_SRC python :var run=create-file
with open('main.txt') as f:
    print(f.read())
#+END_SRC

#+RESULTS:
: foo
: bar

 
John

-----------------------------------
Professor John Kitchin 
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803


On Mon, Feb 15, 2021 at 4:01 PM Rodrigo Morales <moralesrodrigo1100@gmail.com> wrote:

Is it possible to associate a code block (A) to another code block (B)
so that when (A) is executed (B) is executed beforehand? I'm asking this
because I have a bash code block (B) that creates a file that is then
processed by a python code block (A) so before executing (A) block, the
file needs to be created by (B).

I managed to accomplish this only with shell code blocks by creating a
function that gets a code block as an string but now that code blocks
have different languages (bash and python) I can't use this same
approach. Recall that ":prologue" inserts an string at the beginning of
the code block (see minimal working example of this idea below.)

#+NAME: create-file
#+begin_src bash :results silent
cat << EOF > main.txt
foo
bar
EOF
#+end_src

#+HEADER: :prologue (org-babel-get-block-as-string "create-file")
#+begin_src bash
cat main.txt
#+end_src

#+RESULTS:
#+begin_example
foo
bar
#+end_example

--
Rodrigo Morales.
IRC: rdrg109 (freenode)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]