all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: John Kitchin <jkitchin@andrew.cmu.edu>
To: Rodrigo Morales <moralesrodrigo1100@gmail.com>
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 16:52:07 -0500	[thread overview]
Message-ID: <CAJ51ETpv7C2BTKiWTdrsVPsKewbH-e4jmAMH=5L0V83m1=BDww@mail.gmail.com> (raw)
In-Reply-To: <87r1lhaw7d.fsf@gmail.com>

[-- 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 --]

  reply	other threads:[~2021-02-15 21:53 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 [this message]
2021-02-16  2:39   ` Rodrigo Morales

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

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

  git send-email \
    --in-reply-to='CAJ51ETpv7C2BTKiWTdrsVPsKewbH-e4jmAMH=5L0V83m1=BDww@mail.gmail.com' \
    --to=jkitchin@andrew.cmu.edu \
    --cc=emacs-orgmode@gnu.org \
    --cc=moralesrodrigo1100@gmail.com \
    /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 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.