emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] noweb and :var statements


From: Sebastian Miele
Subject: Re: [O] noweb and :var statements
Date: Sun, 06 Oct 2019 19:39:08 +0000
User-agent: mu4e 1.3.5; emacs 26.3

Hi Ken!

Ken Mankoff <address@hidden> 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
> <<import()>>
> #+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.
<<NAME(var=value)>> (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 <<import()>> 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
<<import>>
#+END_SRC

Best wishes
Sebastian



reply via email to

[Prev in Thread] Current Thread [Next in Thread]