From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id sDNtIj3tKmBzHAAA0tVLHw (envelope-from ) for ; Mon, 15 Feb 2021 21:53:01 +0000 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id sGY+Hj3tKmA2SQAA1q6Kng (envelope-from ) for ; Mon, 15 Feb 2021 21:53:01 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 595D81E3A2 for ; Mon, 15 Feb 2021 22:53:00 +0100 (CET) Received: from localhost ([::1]:59640 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lBlne-0004oW-1U for larch@yhetil.org; Mon, 15 Feb 2021 16:52:58 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:56504) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lBln8-0004oA-1E for emacs-orgmode@gnu.org; Mon, 15 Feb 2021 16:52:26 -0500 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]:37332) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lBln6-0006Cz-3J for emacs-orgmode@gnu.org; Mon, 15 Feb 2021 16:52:25 -0500 Received: by mail-wr1-x429.google.com with SMTP id v15so10824106wrx.4 for ; Mon, 15 Feb 2021 13:52:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=andrew-cmu-edu.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=c6ldctadS87Mb5WgSDBliEFygfTDFCh1tHRT62rSyQ0=; b=cgFXivMO+lCWmFjQWCzDyG1n6X1bORy+0d1DDfdC8/WSQEbvZu1szHX3Dhg8RjS+K6 ufP8pY6uytA3h+H25aF6h3RXIUMhrQLVDtDWH0S3PjNZqB//ghdyAq/AvKh7SAdNDIMW zRYU6e3Z5THqjkzGesaVt8kN6monbnR0may0AvJt/YM9vvEecGbKXviL6YPlOdrgWdkK bxf8vFFkTwKTZg86HY8TcZvVWDzhoTPqIb8Hf1M8HKKpAGKHIr6YBtVvi6G4TsqW+J1o KPy61UeenjhAp6WPfsv/29HI7rJ9dEyJn5I/xh23LXq9PSavydCEXtRwoZYA18qcDW0b Z1dA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=c6ldctadS87Mb5WgSDBliEFygfTDFCh1tHRT62rSyQ0=; b=m7eKsRhUm7ZVqFadm/u6xLWCI+BuVaVowG+4o8gwmrM4R/+qliVxVjxoSKccIaNsKV igrF0z/MaNPSmcOFsp+cKW5rMLZh+zWmyfTwYvflKPrhzIU4uxoNVboeCWtOLmKToL6y G4syoeSQsX1xTa2GeQahKosaEEZwIQ0sb4mVCTlQ9NF3lJ3IPTEDeHlepJ2KMSc23/+Y BbaM+060nYysX9nwlfkOyXxVWOPQhNmU6ggFfBXYsKg87gzr4Bh5ios2w++XCQuIt3+i qJFLjJ8RbefomsW2SEn/jFfktWOJYi7NOzxkZK30UqsjAVD+mKfJnfTCGHLq9o5NUms2 sOLw== X-Gm-Message-State: AOAM53196MMVLhkaiyHNDMzA3aOjt9MvzSMKT1kDNmEYdHU9gVk12ISi 6wR3fOT76sAfktPnAuEhutgst1aBo1SZsB87xAU= X-Google-Smtp-Source: ABdhPJzJbKEc1rE8zyM6RgulI6GMNY/kPh694UcJI1xM4MeqzeHYbkhK1v7ynoMCSMEChoA5cVLEHFQDzfG/y5Ncce4= X-Received: by 2002:adf:aa98:: with SMTP id h24mr20507323wrc.75.1613425938869; Mon, 15 Feb 2021 13:52:18 -0800 (PST) MIME-Version: 1.0 References: <87r1lhaw7d.fsf@gmail.com> In-Reply-To: <87r1lhaw7d.fsf@gmail.com> From: John Kitchin Date: Mon, 15 Feb 2021 16:52:07 -0500 Message-ID: Subject: Re: How to associate a code block to another one so that it is executed beforehand? To: Rodrigo Morales Content-Type: multipart/alternative; boundary="000000000000f52f4205bb66ffd2" Received-SPF: pass client-ip=2a00:1450:4864:20::429; envelope-from=johnrkitchin@gmail.com; helo=mail-wr1-x429.google.com X-Spam_score_int: -13 X-Spam_score: -1.4 X-Spam_bar: - X-Spam_report: (-1.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: org-mode-email Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -1.46 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=andrew-cmu-edu.20150623.gappssmtp.com header.s=20150623 header.b=cgFXivMO; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=andrew.cmu.edu (policy=none); spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Queue-Id: 595D81E3A2 X-Spam-Score: -1.46 X-Migadu-Scanner: scn1.migadu.com X-TUID: 4siuoyR+2PMK --000000000000f52f4205bb66ffd2 Content-Type: text/plain; charset="UTF-8" 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) > > --000000000000f52f4205bb66ffd2 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Here is one way to do it.=C2=A0 You use a :var to &qu= ot;run" the other block.

#+NAME: c= reate-file
#+begin_src bash :results silent
cat << EOF > mai= n.txt
foo
bar
EOF
#+end_src

#+BEGIN_SRC python :var run= =3Dcreate-file
with open('main.txt') as f:
=C2=A0 =C2=A0 prin= t(f.read())
#+END_SRC

#+RESULTS:
: foo
: bar
:=C2=A0
=
=C2=A0
John

-----------------------------------
Professor John = Kitchin=C2=A0
Doherty Hall A207F
Department of Chemical EngineeringCarnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
<= div dir=3D"ltr">@johnkitchin
http://kitchingroup.cheme.cmu.edu



On Mon, Feb 15, 2021 at 4:01 PM Rodrigo Mora= les <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 thi= s
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 beginn= ing 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)

--000000000000f52f4205bb66ffd2--