[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 20:55:16 +0000 |
User-agent: |
mu4e 1.3.5; emacs 26.3 |
Ken Mankoff <address@hidden> writes:
> Hi Sebastian,
>
> I'm not getting the results I expect from your MWE either. Perhaps I
> gave too much code and asked X when what I really want is Y. I think
> I've distilled it to this:
>
> What is the most elegant Org way to get a table into a Python array?
I do not know whether it is the most elegant way, but the following
works, and probably is at least close to shortes possible way:
#+NAME: table_foo
| foo |
|-----|
| 42 |
| 100 |
#+BEGIN_SRC python :var table=table_foo :tangle table.py
#+END_SRC
After tangling, table.py contains "table=[[42], [100]]".
Did you know that you can have several source code blocks tangling to
the same file? Maybe you do not really need noweb stuff at all. If you
add a second block, e.g.
#+BEGIN_SRC python :tangle table.py
mangle_table
#+END_SRC
then, after tangling, table.py contains "table=[[42], [100]]" and
"mangle_table".
> I can code it directly:
>
> #+BEGIN_SRC python
> <<setup>>
> print(foo)
> #+END_SRC
>
>
> And now I can hide <<setup>> in a section at the bottom of the
> document. If it looks like this, everything works:
>
> #+NAME: setup
> #+BEGIN_SRC python
> foo = np.array([42,43,44])
> #+END_SRC
>
> But is there a more elegant method? Can I get the same behavior if the
> data I want is in an Org table rather than hard-coded directly in
> Python?
This use-case is covered by what I wrote above. You can put the named
Org table at the bottom of the file.
Another note: You can tangle to different files from one Org file. You
may e.g. add an additional source block:
#+BEGIN_SRC python :tangle use_table.py
include_table_py
use_table
#+END_SRC
where include_table_py must be replaced by the Python way of saying:
include the file table.py. (I do not know Python.)
> Ideally, I'd like to have:
>
> #+NAME: setup
> #+BEGIN_SRC python
> <<setup(table="foo_data" varname="foo")>>
> <<setup(table="bar_data" varname="bar")>>
> #+END_SRC
>
> And a #+NAME: setup block that takes a :var table and sticks it in the
> :var varname variable.
I strongly suspect that you do not really need noweb stuff and still
struggle to understand the very basics of tangling.
> And then after calling <<setup>> be able to use variable "foo" and
> "bar" that are generated from column or 2D Org tables elsewhere in the
> document. Can I do this in Org?
>
> Thanks,
>
> -k.
- [O] noweb and :var statements, Ken Mankoff, 2019/10/06
- Re: [O] noweb and :var statements, Sebastian Miele, 2019/10/06
- Re: [O] noweb and :var statements, Sebastian Miele, 2019/10/06
- Re: [O] noweb and :var statements, Ken Mankoff, 2019/10/06
- Re: [O] noweb and :var statements, Sebastian Miele, 2019/10/06
- Re: [O] noweb and :var statements, Sebastian Miele, 2019/10/06
- Re: [O] noweb and :var statements, Ken Mankoff, 2019/10/07
- Re: [O] noweb and :var statements, Sebastian Miele, 2019/10/07
Re: [O] noweb and :var statements, Ken Mankoff, 2019/10/06
- Re: [O] noweb and :var statements,
Sebastian Miele <=