* How to associate a code block to another one so that it is executed beforehand? @ 2021-02-15 20:54 Rodrigo Morales 2021-02-15 21:52 ` John Kitchin 0 siblings, 1 reply; 3+ messages in thread From: Rodrigo Morales @ 2021-02-15 20:54 UTC (permalink / raw) To: emacs-orgmode 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) ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: How to associate a code block to another one so that it is executed beforehand? 2021-02-15 20:54 How to associate a code block to another one so that it is executed beforehand? Rodrigo Morales @ 2021-02-15 21:52 ` John Kitchin 2021-02-16 2:39 ` Rodrigo Morales 0 siblings, 1 reply; 3+ messages in thread From: John Kitchin @ 2021-02-15 21:52 UTC (permalink / raw) To: Rodrigo Morales; +Cc: org-mode-email [-- Attachment #1: Type: text/plain, Size: 1642 bytes --] 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 < 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) > > [-- Attachment #2: Type: text/html, Size: 2502 bytes --] ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: How to associate a code block to another one so that it is executed beforehand? 2021-02-15 21:52 ` John Kitchin @ 2021-02-16 2:39 ` Rodrigo Morales 0 siblings, 0 replies; 3+ messages in thread From: Rodrigo Morales @ 2021-02-16 2:39 UTC (permalink / raw) To: John Kitchin; +Cc: org-mode-email John Kitchin <jkitchin@andrew.cmu.edu> 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) ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-02-16 2:49 UTC | newest] Thread overview: 3+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2021-02-15 20:54 How to associate a code block to another one so that it is executed beforehand? Rodrigo Morales 2021-02-15 21:52 ` John Kitchin 2021-02-16 2:39 ` Rodrigo Morales
Code repositories for project(s) associated with this external index https://git.savannah.gnu.org/cgit/emacs.git https://git.savannah.gnu.org/cgit/emacs/org-mode.git This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.