Re: How to associate a code block to another one so that it is executed beforehand?
John Kitchin writes: > Here is one way to do it. You use a :var to "run" the other block. Thank you very much for the suggestion. It indeed works but I think that I don't need what I was requesting now that I've found out about the =:post= header argument.e After reading your answer, I remembered that there exists the =:post= header argument and that fully changed the way I was taking notes. This is how my notes looked like #+begin_src org ,#+NAME: create-file-foo ,#+begin_src dash :results silent cat << EOF > main.txt foo first foo second EOF ,#+end_src ,#+NAME: create-file-bar ,#+begin_src dash :results silent cat << EOF > main.txt bar first bar second EOF ,#+end_src The following code block prints each line the corresponding index and the length of the specified file. # # Here I wanted to call one of the code blocks presented above so that # before the Python code block is executed, the file is created. # ,#+begin_src python with open('main.txt') as f: for line in f: print(line, end='') ,#+end_src #+end_src Now using the =:post= header argument my notes look like #+begin_src org We can print each line together with its index by executing ,#+NAME: read-file-with-index ,#+begin_src python with open('main.txt') as file: for idx,line in enumerate(file): print(idx, line, end='') ,#+end_src ,#+begin_src dash :post read-file-with-index() cat << EOF > main.txt foo first foo second EOF ,#+end_src ,#+RESULTS: ,#+begin_example 0 foo first 1 foo second ,#+end_example ,#+begin_src dash :post read-file-with-index() cat << EOF > main.txt bar first bar second EOF ,#+end_src ,#+RESULTS: ,#+begin_example 0 bar first 1 bar second ,#+end_example #+end_src Note that, in this scenario, when using the =:post= header argument it is only necessary to name the Python code block instead of naming all the code blocks that are meant to be processed by the Python script. -- Rodrigo Morales. IRC: rdrg109 (freenode)
Re: How to associate a code block to another one so that it is executed beforehand?
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 @johnkitchin http://kitchingroup.cheme.cmu.edu On Mon, Feb 15, 2021 at 4:01 PM Rodrigo Morales < moralesrodrigo1...@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) > >
How to associate a code block to another one so that it is executed beforehand?
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)