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: Rodrigo Morales
Subject: Re: How to associate a code block to another one so that it is executed beforehand?
Date: Mon, 15 Feb 2021 21:39:59 -0500
User-agent: mu4e 1.4.14; emacs 27.1

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)



reply via email to

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