emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Rodrigo Morales <moralesrodrigo1100@gmail.com>
To: John Kitchin <jkitchin@andrew.cmu.edu>
Cc: org-mode-email <emacs-orgmode@gnu.org>
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	[thread overview]
Message-ID: <87tuqcka68.fsf@gmail.com> (raw)
In-Reply-To: <CAJ51ETpv7C2BTKiWTdrsVPsKewbH-e4jmAMH=5L0V83m1=BDww@mail.gmail.com>


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	other threads:[~2021-02-16  2:49 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87tuqcka68.fsf@gmail.com \
    --to=moralesrodrigo1100@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=jkitchin@andrew.cmu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).