From mboxrd@z Thu Jan 1 00:00:00 1970 From: Sebastian Miele Subject: Re: noweb and :var statements Date: Sun, 06 Oct 2019 19:39:08 +0000 Message-ID: <87k19hirz7.fsf@gmail.com> References: <87k19hg7g1.fsf@gmail.com> Reply-To: sebastian.miele@gmail.com Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:56554) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iHCN7-0005F1-Gc for emacs-orgmode@gnu.org; Sun, 06 Oct 2019 15:39:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iHCN6-0001HU-Av for emacs-orgmode@gnu.org; Sun, 06 Oct 2019 15:39:13 -0400 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]:40000) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iHCN6-0001H8-2l for emacs-orgmode@gnu.org; Sun, 06 Oct 2019 15:39:12 -0400 Received: by mail-wr1-x42e.google.com with SMTP id h4so4004701wrv.7 for ; Sun, 06 Oct 2019 12:39:12 -0700 (PDT) Received: from tisch ([2a02:908:175c:4260:5ffc:7882:6024:ca5b]) by smtp.gmail.com with ESMTPSA id 13sm10384472wmj.29.2019.10.06.12.39.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Oct 2019 12:39:09 -0700 (PDT) In-reply-to: <87k19hg7g1.fsf@gmail.com> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org Hi Ken! Ken Mankoff writes: > I'm having with noweb and variables. Can someone explain what I'm > doing wrong? For example, if I have this table: > > #+NAME: table_foo > | foo | > |-----| > | 42 | > | 100 | > > And I want to import it into Python and use it, I can do that like this: > > #+NAME: import > #+BEGIN_SRC python :var table=table_foo :session foo > import numpy as np > table = np.array(table).astype(np.float).flatten() > #+END_SRC > > Eval of this block works, and if I tangle it, I see: > >> table=[[42], [100]] >> import numpy as np >> table = np.array(table).astype(np.float).flatten() > > But if I want to use that block elsewhere via noweb, it doesn't seem to work: > > #+BEGIN_SRC python :results output drawer :noweb yes :session foo :tangle import_noweb.py > <> > #+END_SRC > > The code runs and in a clean *foo* session I do have my table > variable, but I *also* get an error. The buffer contains the text at > the bottom of this message, and the tangled code in import_noweb.py is > only "nil". > > How can I 1) run noweb blocks with variables and 2) tangle noweb > blocks with variables (i.e. tangle tables into source files). Section 15.10 (Noweb Reference Syntax) of the manual says that e.g. <> (a noweb reference with parentheses) executes the NAMEd block with the specified binding and inserts the results of the execution during tangling. That probably is why the table variable is there in the session. But the results of executing <> at that place were not what was intended. If it were something like #+NAME: import #+BEGIN_SRC python :var table=table_foo :results output import numpy as np table = np.array(table).astype(np.float).flatten() CODE #+END_SRC where CODE is replaced by some Python code that prints the wanted Python code, it would work. (Note the omission of the :session and the addition of :results output. See section 15.5 (Results of Evaluation) of the manual for the reasons.) As far as I understand the manual there may be no way to directly attain what you are trying. However, something like the following may suit your use case. (For the header-args property see section 15.2 (Using Header Arguments) of the manual.) * A Heading :PROPERTIES: :header-args: :var table=table_foo :END: #+NAME: table_foo | foo | |-----| | 42 | | 100 | #+NAME: import #+BEGIN_SRC python import numpy as np table = np.array(table).astype(np.float).flatten() #+END_SRC #+BEGIN_SRC python :noweb yes :tangle import_noweb.py <> #+END_SRC Best wishes Sebastian