From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id GMSnK2oIrWBU+QAAgWs5BA (envelope-from ) for ; Tue, 25 May 2021 16:23:38 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id CGZgJ2oIrWDSIQAAB5/wlQ (envelope-from ) for ; Tue, 25 May 2021 14:23:38 +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 A4134145CD for ; Tue, 25 May 2021 16:23:37 +0200 (CEST) Received: from localhost ([::1]:54444 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1llXy3-0006LT-ME for larch@yhetil.org; Tue, 25 May 2021 10:23:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48014) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llXxB-0006IB-8V for emacs-orgmode@gnu.org; Tue, 25 May 2021 10:22:44 -0400 Received: from se13.route25.eu ([2a00:f10:121:a00:1c00:efff:fe00:1854]:42048) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1llXx7-0004Lf-Lx for emacs-orgmode@gnu.org; Tue, 25 May 2021 10:22:40 -0400 To: emacs-orgmode@gnu.org References: <56b14034-f6c4-0528-9235-e0fb9f3f1f28@karssen.org> From: "Lennart C. Karssen" Subject: Re: Global variables in Org mode document with source blocks Message-ID: <9b777904-f849-83b1-a9b2-87ab5d8bcc3c@karssen.org> Date: Tue, 25 May 2021 16:22:25 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="rOn31fTsIpMEgzPLtAk8xigFbMh7QDEIz" X-Originating-IP: 185.87.184.57 X-SpamExperts-Domain: out.pcextreme.nl X-SpamExperts-Username: 185.87.184.57 X-SpamExperts-Outgoing-Class: ham X-SpamExperts-Outgoing-Evidence: Combined (0.05) X-Recommended-Action: accept X-Filter-ID: Pt3MvcO5N4iKaDQ5O6lkdGlMVN6RH8bjRMzItlySaT9w2Fu9GTD4l9HF+70L+iokPUtbdvnXkggZ 3YnVId/Y5jcf0yeVQAvfjHznO7+bT5xiExUbNqvmdkLSOzf1W2Y3z8hkeSqZT0W1YMl0fRDa99cV PSoHm0W/3adFfiYl2nsBOBXjexVgGxXt9StLQZVW/T4GcPvCLvSpAEEGy7kYxjA4xaKs5cOgbpQu MACSpuI/PGDCiu3095bjBHXzclDEvDPWcAldHgqrH/A+OwNh3R9/DAC5VUXJ2o+/2Ipg3WIKXYRo vaWLJSoRq+4VSwVI/74zHSqk6CcHzqoCKT+E5sE95bF8tPKjnaWlQ6fjTEeg4CZvTGBeutAohO1y UnDCPEg+PVRTiaxPY52n0Pp/86b+Sk5ZBXUgt9/X6plqv8Jl041btgY00t8ZwQGEpPrujd2/KNn9 7NO2ZQWjyOEIHHU8K0rkb5kkiJbpUX9a4LlQef6Ceaw0tyEeHKZjklTreHL+whhAdKuW1jHRpIte tiimkcXcta5lg1BBHrfG9sLNASrPyqZ3YL9752K6v1pQD1e76szsWvBsaS+KViusBg4TfS2su/by ymSuqTyxp/ZExDyRiioQmvGTre37mWh0chGFHjQ6q190o7vmaHflc9txbldtWegMkrWkFxxiuqCC d5iXYJuZDVz9Fg6INCqeU+oP/M2dAvduEoqpNHAK24UZoLNPfATMQSDI9DiIBEIHpf84VwMOuAe0 I/f0DGw/LOjrOwwGI3kKu05JnDlv7nAKet5ATEVyvcD3zonKMui8c+OUp6hNZ2VhCBZKb+A6iPg8 NGrhBSlTYXv+RODac8/VtwX4K95wDii10zBhNl/2lRQJUwpzPUggadoyHg3TCiNc2Hq1hnwyc342 lIbFl5HAAxJk5us7mLz1pH6kplIlCkSujm58a/xB+XC1kCc6OfK0uPIgIyYiAZisW72/tr5sn9lh RYaeJ71uUKsFcX4o031Es3qltHMyHIgweqftMMQrbXgpYquub9AstiyqTxLqNG+ExXegOuvKXeeS ueLL/6ALlMPsF2FDHvuw+O8p81eGAJESwE768d6PWzuAUQ== X-Report-Abuse-To: spam@semaster01.route25.eu Received-SPF: pass client-ip=2a00:f10:121:a00:1c00:efff:fe00:1854; envelope-from=lennart@karssen.org; helo=se13.route25.eu X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, NICE_REPLY_A=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_SBL_A=0.1 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: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1621952618; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post; bh=75oIHRUtnk7PkmaPNoeHpUUGTdbMyVClPw0/m+tvkQw=; b=MnLfDBoNW2cWSKaHd+x60/dP/9aiyjiANkiOdR6j4pZbMe101y4YUvQiw3R2z08Aeb8mLz nL+6dEzQ++MPS7agLMuHQu6m79n/BOx0QIQpx7JbRiDoDqBsSRnf9dJFdKXEmM2KdjC6tB Rv9JgmBcDNP3vG7tvPXLHayIN+HmSiHVDpPefN2xHXgPTFS09zz/B2KN9Istxrta0XRaZg yBCkAvCS1W/ZWWqwmbr6XC9La1BMc2Mo4m4k7NLo6ob6IfSH3dki+zcAsEkREuUiyGziMt j8a5k78+LJAGJbWWSYLA0g6V9/ZmYMoHDrLlZe/oDTBE4kWC91fwZuPPYQCwAg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1621952618; a=rsa-sha256; cv=none; b=Qnb5O59CALYVlDjpfBswd92vJFdi1N/9cF1spPKVPuKEHzubMGmvsKctz4Age/XT8gwjJ7 0bQfIcoOrpBJRzwi/CEvjrwClB0+6+fPg9TO81ZIJeN+ZRCIvX1ScZbSJ9ikSfJXrDc3EU qFEinGU44kkQwsUY3DwmJ8xx2XKvr80fUrv3xC6sASvJPckLeYnrik+aglCPGhzKEgHs8I 0W76ICktk5ekPO+T+Dtretg7dI/iPyy/oGUbIGbKbU+N9xOfGFezbOEpMoP4QUMr5xP389 4iV9CShiyamPF80OkSkpzxeF+aZpW8KE9S/IqeSx7K3eIzsIVh0Md1lkDuSFgw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=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-Spam-Score: -3.03 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=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: A4134145CD X-Spam-Score: -3.03 X-Migadu-Scanner: scn1.migadu.com X-TUID: qP0QBIvAAHHB This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --rOn31fTsIpMEgzPLtAk8xigFbMh7QDEIz Content-Type: multipart/mixed; boundary="uFJN1aZyLRv3dnnNJxCcwxFQTk7ujkNZq"; protected-headers="v1" From: "Lennart C. Karssen" To: emacs-orgmode@gnu.org Message-ID: <9b777904-f849-83b1-a9b2-87ab5d8bcc3c@karssen.org> Subject: Re: Global variables in Org mode document with source blocks References: <56b14034-f6c4-0528-9235-e0fb9f3f1f28@karssen.org> In-Reply-To: --uFJN1aZyLRv3dnnNJxCcwxFQTk7ujkNZq Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: quoted-printable Dear John, Thanks a lot for your quick reply. My apologies for not replying earlier. Some urgent things came up that took my time. I tried your suggestions and settled on a small adaptation of your first suggestion. Because the output of my source blocks is raw Org code (i.e. the report text that explains the failure), I can't directly print =3Dfailure-DIGITS=3D or something similar. My solution to that is to appe= nd =3D# failure-BLOCK=3D to the output text (which is ignored during export)= and use your =3Dcount-matches=3D suggestion to count those. The text =3DB= LOCK=3D is different for each code block and should allow me to specify which blocks fail in the Conclusion section. Thanks a lot! Best regards, Lennart. On 18-05-2021 17:03, John Kitchin wrote: > Given all the different languages involved, I don't think there is a wa= y > to use a common variable.=C2=A0 >=20 > One way might be to have each block output some kind of string if it > fails, and then in the last block you could search for the buffer for > that string. Something=C2=A0like this: >=20 >=20 > * Section 1 >=20 > #+BEGIN_SRC sh > false || echo "failed"-`date +'%s'` > #+END_SRC >=20 > #+RESULTS: > : failed-1621348872 >=20 >=20 > #+BEGIN_SRC python > import time >=20 > if not False: > =C2=A0 =C2=A0 print(f'failed-{time.time()}') > #+END_SRC >=20 > #+RESULTS: > : failed-1621348926.125608 >=20 >=20 > * Final section >=20 > #+BEGIN_SRC emacs-lisp > (format "There were %s failed blocks" (count-matches "failed-[0-9]" > (point-min) (point-max))) > #+END_SRC >=20 > #+RESULTS: > : There were 2 failed blocks >=20 > Some alternatives include writing/appending to a file on error, and the= n > counting the number of lines. >=20 > Another route is to use a :post header and search for the string there.= >=20 > #+BEGIN_SRC emacs-lisp > (setq n-failures 0) > #+END_SRC >=20 > #+RESULTS: > : 0 >=20 > #+name: fail-capture > #+BEGIN_SRC emacs-lisp :var data=3D"" > (when (string-match "failed-[0-9]" data) > =C2=A0 (incf n-failures) > =C2=A0 (message "captured a failure in %s. n-failures=3D%s" data n-fail= ures)) > #+END_SRC >=20 > #+BEGIN_SRC sh :post fail-capture(*this*) > false || echo "failed"-`date +'%s'` > #+END_SRC >=20 > #+RESULTS: > : captured a failure in failed-1621349398. n-failures=3D1 >=20 >=20 > #+BEGIN_SRC python :post fail-capture(*this*) > print('This did not fail') > #+END_SRC >=20 > #+RESULTS: > : nil >=20 > #+BEGIN_SRC python :post fail-capture(*this*) > print('This failed-9') > #+END_SRC >=20 > #+RESULTS: > : captured a failure in This failed-9 > : . n-failures=3D2 >=20 > All these approaches need you to handle and catch the errors. I think > other kinds of errors would stop the export process. e.g. if a block > errors out from division by zero or something. >=20 > I don't know how easy it would be to check if a src block succeeded or > not. If it was easy, you might use the=C2=A0org-babel-after-execute-hoo= k > variable to update something when a failure is detected. Some blocks > create an *Org-Babel Error Output buffer somewhere, but i don't know if= > this is 100% reliable across all blocks. >=20 > John >=20 > ----------------------------------- > Professor John Kitchin (he/him/his) > Doherty Hall A207F > Department of Chemical Engineering > Carnegie Mellon University > Pittsburgh, PA 15213 > 412-268-7803 > @johnkitchin > http://kitchingroup.cheme.cmu.edu >=20 >=20 >=20 > On Tue, May 18, 2021 at 9:58 AM Lennart C. Karssen > wrote: >=20 > Dear list, >=20 > I am working on a dynamic report in Org mode, where I use source bl= ocks > in various languages to process data. Several blocks produce text o= r > tables that become part of the PDF on export. >=20 > The final chapter should state whether all checks passed, or whethe= r one > or more failed (it is not necessary to know which step failed). >=20 > In a single language environment, I would use a variable (called e.= g. > nrChecksFailed) that would be incremented for each failing check. I= n a > single language Org document this could probably be done with a > :session, but given that I mix Awk, Bash, Emacs lisp and R that doe= sn't > look like the way to go. Do Org documents/source blocks have some > concept of a (global) variable that I can pass to my SRC blocks and= > increment inside them? >=20 > E.g. after somehow initialising nrChecksFailed =3D 0, I would like = to do: >=20 > #+header :var nFailed=3DnrChecksFailed :var someData=3DsomeData > #+begin_src R :results raw > do_some_check_here_on_someData >=20 > if (check_results_OK) { > =C2=A0 cat("check A passed\n") > } else { > =C2=A0 cat("check A *failed*\n") > =C2=A0 nFailed <- nFailed + 1 > } > #+end_src >=20 > So that in my conclusion chapter I can do for example: >=20 > #+header: :var nFailed=3DnrChecksFailed > #+begin_src bash=C2=A0 :results raw > if [[ nFailed -eq 0 ]]; then > =C2=A0 echo "All checks passed > else > =C2=A0echo "One or more checks *failed!*" > fi > #+end_src >=20 >=20 > Best regards, >=20 > Lennart. >=20 > --=20 > *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* > L.C. Karssen > The Netherlands >=20 > lennart@karssen.org > http://blog.karssen.org > GPG key ID: A88F554A > -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- >=20 --=20 *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* L.C. Karssen 's-Hertogenbosch The Netherlands lennart@karssen.org http://blog.karssen.org GPG key ID: A88F554A -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- --uFJN1aZyLRv3dnnNJxCcwxFQTk7ujkNZq-- --rOn31fTsIpMEgzPLtAk8xigFbMh7QDEIz Content-Type: application/pgp-signature; name="OpenPGP_signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="OpenPGP_signature" -----BEGIN PGP SIGNATURE----- wnsEABEIACMWIQQyBENSov4Y8H9uObIgAxQL2pzVCQUCYK0IIQUDAAAAAAAKCRAgAxQL2pzVCQH9 AQCIsUJSyPmvQAA3YQuXLHjbybcIQfI+HB3cLiTI8W+soQD9GMCy4jyUTG2b8nD1sSCRjk+aBES0 1+hZVui5IPi3Xcw= =UFVa -----END PGP SIGNATURE----- --rOn31fTsIpMEgzPLtAk8xigFbMh7QDEIz--