* Sharing variables between source blocks without session
@ 2021-03-16 8:56 Loris Bennett
2021-03-16 9:16 ` Loris Bennett
2021-03-16 13:28 ` Eric S Fraga
0 siblings, 2 replies; 13+ messages in thread
From: Loris Bennett @ 2021-03-16 8:56 UTC (permalink / raw)
To: Org Mode Mailing List
Hi,
I have a 'sh' source block which produces a table which I then want to
plot with a 'python' source block:
#+NAME: code;raw_data
#+HEADER: :var user="loris"
#+BEGIN_SRC sh
ps -u loris -o etimes=
#+END_SRC
#+NAME: tab;raw_data
#+RESULTS: code;raw_data...
#+HEADER: :var df=tab;raw_data
#+HEADER: :var user="loris"
#+BEGIN_SRC python :results file :var f="process_times.pdf"
import numpy as np
import matplotlib.pyplot as plt
# flatten list
x = [item for sublist in df for item in sublist]
plt.hist(x, bins=10)
plt.xlabel("time [s]")
plt.ylabel("number of processes")
plt.title("user: " + user)
plt.savefig(f)
return f
#+END_SRC
How can I avoid having to declare the variable 'user' for both blocks?
Cheers,
Loris
--
This signature is currently under construction.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Sharing variables between source blocks without session
2021-03-16 8:56 Sharing variables between source blocks without session Loris Bennett
@ 2021-03-16 9:16 ` Loris Bennett
2021-03-16 13:28 ` Eric S Fraga
1 sibling, 0 replies; 13+ messages in thread
From: Loris Bennett @ 2021-03-16 9:16 UTC (permalink / raw)
To: emacs-orgmode
"Loris Bennett" <loris.bennett@fu-berlin.de> writes:
> Hi,
>
> I have a 'sh' source block which produces a table which I then want to
> plot with a 'python' source block:
>
> #+NAME: code;raw_data
> #+HEADER: :var user="loris"
>
> #+BEGIN_SRC sh
> ps -u loris -o etimes=
> #+END_SRC
Oops, that should be
#+NAME: code;raw_data
#+HEADER: :var user="loris"
#+BEGIN_SRC sh
ps -u $user -o etimes=
#+END_SRC
> #+NAME: tab;raw_data
> #+RESULTS: code;raw_data...
>
> #+HEADER: :var df=tab;raw_data
> #+HEADER: :var user="loris"
>
> #+BEGIN_SRC python :results file :var f="process_times.pdf"
> import numpy as np
> import matplotlib.pyplot as plt
> # flatten list
> x = [item for sublist in df for item in sublist]
> plt.hist(x, bins=10)
> plt.xlabel("time [s]")
> plt.ylabel("number of processes")
> plt.title("user: " + user)
> plt.savefig(f)
> return f
> #+END_SRC
>
> How can I avoid having to declare the variable 'user' for both blocks?
>
> Cheers,
>
> Loris
--
Dr. Loris Bennett (Hr./Mr.)
ZEDAT, Freie Universität Berlin Email loris.bennett@fu-berlin.de
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Sharing variables between source blocks without session
2021-03-16 8:56 Sharing variables between source blocks without session Loris Bennett
2021-03-16 9:16 ` Loris Bennett
@ 2021-03-16 13:28 ` Eric S Fraga
2021-03-18 13:21 ` Loris Bennett
1 sibling, 1 reply; 13+ messages in thread
From: Eric S Fraga @ 2021-03-16 13:28 UTC (permalink / raw)
To: Loris Bennett; +Cc: Org Mode Mailing List
On Tuesday, 16 Mar 2021 at 09:56, Loris Bennett wrote:
> How can I avoid having to declare the variable 'user' for both blocks?
I imagine you could use a property, as in
#+property: header-args :var user=loris
or even make it specific for the particular language.
(untested)
--
: Eric S Fraga via Emacs 28.0.50, Org release_9.4.4-254-g37749c
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Sharing variables between source blocks without session
2021-03-16 13:28 ` Eric S Fraga
@ 2021-03-18 13:21 ` Loris Bennett
2021-03-18 15:56 ` Eric S Fraga
2021-03-18 17:59 ` Cook, Malcolm
0 siblings, 2 replies; 13+ messages in thread
From: Loris Bennett @ 2021-03-18 13:21 UTC (permalink / raw)
To: Org Mode Mailing List
Eric S Fraga <e.fraga@ucl.ac.uk> writes:
> On Tuesday, 16 Mar 2021 at 09:56, Loris Bennett wrote:
>> How can I avoid having to declare the variable 'user' for both blocks?
>
> I imagine you could use a property, as in
>
> #+property: header-args :var user=loris
>
> or even make it specific for the particular language.
>
> (untested)
Thanks for point out using 'header-args;' as property. However, if I do
the following, the variable is unset in the shell script:
#+properties: header-args :var user=loris
#+begin_src sh
echo user: ${user}
#+end_src
#+RESULTS:
: user:
Is that supposed to work, or am I doing something wrong?
Cheers,
Loris
--
This signature is currently under construction.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Sharing variables between source blocks without session
2021-03-18 13:21 ` Loris Bennett
@ 2021-03-18 15:56 ` Eric S Fraga
2021-03-19 6:50 ` Loris Bennett
2021-03-18 17:59 ` Cook, Malcolm
1 sibling, 1 reply; 13+ messages in thread
From: Eric S Fraga @ 2021-03-18 15:56 UTC (permalink / raw)
To: Loris Bennett; +Cc: Org Mode Mailing List
On Thursday, 18 Mar 2021 at 14:21, Loris Bennett wrote:
> Thanks for point out using 'header-args;' as property. However, if I do
> the following, the variable is unset in the shell script:
Works for me.
Make sure you reload properties by hitting C-c C-c on the property line
(or some other #+ meta line in the file). Also, you will need to put
the value of the user variable in "quotes", i.e. user="loris", for some
reason.
--
: Eric S Fraga via Emacs 28.0.50, Org release_9.4.4-254-g37749c
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Sharing variables between source blocks without session
2021-03-18 15:56 ` Eric S Fraga
@ 2021-03-19 6:50 ` Loris Bennett
0 siblings, 0 replies; 13+ messages in thread
From: Loris Bennett @ 2021-03-19 6:50 UTC (permalink / raw)
To: Org Mode Mailing List
Eric S Fraga <e.fraga@ucl.ac.uk> writes:
> On Thursday, 18 Mar 2021 at 14:21, Loris Bennett wrote:
>> Thanks for point out using 'header-args;' as property. However, if I do
>> the following, the variable is unset in the shell script:
>
> Works for me.
>
> Make sure you reload properties by hitting C-c C-c on the property line
> (or some other #+ meta line in the file). Also, you will need to put
> the value of the user variable in "quotes", i.e. user="loris", for some
> reason.
OK, works for me, too now. The quotes were the important thing, thanks.
For future me and the future in general, would it be a good idea to add
an example to
https://orgmode.org/manual/Using-Header-Arguments.html
which illustrates the difference between strings and numbers,
e.g.
#+property: header-args:sh :var user="loris" fails=3 temp=17.1
#+begin_src sh
echo user ${user}
echo fails ${fails}
echo temp ${temp}
#+end_src
#+RESULTS:
| user | loris |
| fails | 3 |
| temp | 17.1 |
?
Cheers,
Loris
--
This signature is currently under construction.
^ permalink raw reply [flat|nested] 13+ messages in thread
* RE: Sharing variables between source blocks without session
2021-03-18 13:21 ` Loris Bennett
2021-03-18 15:56 ` Eric S Fraga
@ 2021-03-18 17:59 ` Cook, Malcolm
2021-03-19 6:38 ` Loris Bennett
1 sibling, 1 reply; 13+ messages in thread
From: Cook, Malcolm @ 2021-03-18 17:59 UTC (permalink / raw)
To: Loris Bennett, Org Mode Mailing List
Eric S Fraga <mailto:e.fraga@ucl.ac.uk> writes:
>
>> On Tuesday, 16 Mar 2021 at 09:56, Loris Bennett wrote:
>>> How can I avoid having to declare the variable 'user' for both blocks?
>>
>> I imagine you could use a property, as in
>>
>> #+property: header-args :var user=loris
>>
>> or even make it specific for the particular language.
>>
>> (untested)
>
>Thanks for point out using 'header-args;' as property. However, if I do
>the following, the variable is unset in the shell script:
>
>#+properties: header-args :var user=loris
>
>#+begin_src sh
>echo user: ${user}
>#+end_src
>
>#+RESULTS:
>: user:
>
>Is that supposed to work, or am I doing something wrong?
I am unfamiliar with using the plural form of property, as you are trying, at buffer-level nor do I see documented possible here:
https://orgmode.org/manual/Property-Syntax.html#Property-Syntax
try this:
#+property: header-args:sh :var user="loris"
#+begin_src sh
echo user: ${user}
#+end_src
Important: After adding or modifying the #+property line, you will need to instruct org to "refresh your local setup" which I typically accomplish by positioning the point on that line and typing C-c C-c
>
>Cheers,
>
>Loris
>
>--
>This signature is currently under construction.
>
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Sharing variables between source blocks without session
2021-03-18 17:59 ` Cook, Malcolm
@ 2021-03-19 6:38 ` Loris Bennett
2021-03-19 7:04 ` Loris Bennett
2021-03-19 13:26 ` Eric S Fraga
0 siblings, 2 replies; 13+ messages in thread
From: Loris Bennett @ 2021-03-19 6:38 UTC (permalink / raw)
To: Org Mode Mailing List
"Cook, Malcolm" <MEC@stowers.org> writes:
> Eric S Fraga <mailto:e.fraga@ucl.ac.uk> writes:
>>
>>> On Tuesday, 16 Mar 2021 at 09:56, Loris Bennett wrote:
>>>> How can I avoid having to declare the variable 'user' for both blocks?
>>>
>>> I imagine you could use a property, as in
>>>
>>> #+property: header-args :var user=loris
>>>
>>> or even make it specific for the particular language.
>>>
>>> (untested)
>>
>>Thanks for point out using 'header-args;' as property. However, if I do
>>the following, the variable is unset in the shell script:
>>
>>#+properties: header-args :var user=loris
>>
>>#+begin_src sh
>>echo user: ${user}
>>#+end_src
>>
>>#+RESULTS:
>>: user:
>>
>>Is that supposed to work, or am I doing something wrong?
>
> I am unfamiliar with using the plural form of property, as you are trying, at buffer-level nor do I see documented possible here:
> https://orgmode.org/manual/Property-Syntax.html#Property-Syntax
>
> try this:
>
> #+property: header-args:sh :var user="loris"
> #+begin_src sh
>
> echo user: ${user}
> #+end_src
>
> Important: After adding or modifying the #+property line, you will need to instruct org to "refresh your local setup" which I typically accomplish by positioning the point on that line and typing C-c C-c
This doesn't work for me - there is no error, but I get the same result
as above, i.e. the variable ${users} is not set. Does it work for you?
What does work for me is using a section property rather than a buffer
property:
* Dummy Section
:PROPERTIES:
:header-args: :R :var user="loris"
:END:
#+begin_src sh
echo user: ${user}
#+end_src
#+RESULTS:
: user: loris
However, the restriction to source blocks of a particular language does
not seem to work like this, but maybe I have got the syntax wrong
(again).
>
>>
>>Cheers,
>>
>>Loris
>>
>>--
>>This signature is currently under construction.
>>
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Sharing variables between source blocks without session
2021-03-19 6:38 ` Loris Bennett
@ 2021-03-19 7:04 ` Loris Bennett
2021-03-19 13:26 ` Eric S Fraga
1 sibling, 0 replies; 13+ messages in thread
From: Loris Bennett @ 2021-03-19 7:04 UTC (permalink / raw)
To: emacs-orgmode
Loris Bennett <loris.bennett@fu-berlin.de> writes:
> "Cook, Malcolm" <MEC@stowers.org> writes:
>
>> Eric S Fraga <mailto:e.fraga@ucl.ac.uk> writes:
>>>
>>>> On Tuesday, 16 Mar 2021 at 09:56, Loris Bennett wrote:
>>>>> How can I avoid having to declare the variable 'user' for both blocks?
>>>>
>>>> I imagine you could use a property, as in
>>>>
>>>> #+property: header-args :var user=loris
>>>>
>>>> or even make it specific for the particular language.
>>>>
>>>> (untested)
>>>
>>>Thanks for point out using 'header-args;' as property. However, if I do
>>>the following, the variable is unset in the shell script:
>>>
>>>#+properties: header-args :var user=loris
>>>
>>>#+begin_src sh
>>>echo user: ${user}
>>>#+end_src
>>>
>>>#+RESULTS:
>>>: user:
>>>
>>>Is that supposed to work, or am I doing something wrong?
>>
>> I am unfamiliar with using the plural form of property, as you are trying, at
>> buffer-level nor do I see documented possible here:
>> https://orgmode.org/manual/Property-Syntax.html#Property-Syntax
>>
>> try this:
>>
>> #+property: header-args:sh :var user="loris"
>> #+begin_src sh
>>
>> echo user: ${user}
>> #+end_src
>>
>> Important: After adding or modifying the #+property line, you will need to
>> instruct org to "refresh your local setup" which I typically accomplish by
>> positioning the point on that line and typing C-c C-c
>
> This doesn't work for me - there is no error, but I get the same result
> as above, i.e. the variable ${users} is not set. Does it work for you?
Sorry, this does work for me.
> What does work for me is using a section property rather than a buffer
> property:
>
> * Dummy Section
> :PROPERTIES:
> :header-args: :R :var user="loris"
> :END:
>
> #+begin_src sh
> echo user: ${user}
> #+end_src
>
> #+RESULTS:
> : user: loris
>
> However, the restriction to source blocks of a particular language does
> not seem to work like this, but maybe I have got the syntax wrong
> (again).
Quotes around string variables is the important thing.
>>
>>>
>>>Cheers,
>>>
>>>Loris
>>>
>>>--
>>>This signature is currently under construction.
>>>
>
>
--
This signature is currently under construction.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Sharing variables between source blocks without session
2021-03-19 6:38 ` Loris Bennett
2021-03-19 7:04 ` Loris Bennett
@ 2021-03-19 13:26 ` Eric S Fraga
2021-03-19 13:59 ` Loris Bennett
1 sibling, 1 reply; 13+ messages in thread
From: Eric S Fraga @ 2021-03-19 13:26 UTC (permalink / raw)
To: Loris Bennett; +Cc: Org Mode Mailing List
On Friday, 19 Mar 2021 at 07:38, Loris Bennett wrote:
> However, the restriction to source blocks of a particular language does
> not seem to work like this, but maybe I have got the syntax wrong
> (again).
Maybe ;-)
This seems to work for me for a shell:
:PROPERTIES:
:header-args:sh: :var user="loris"
:END:
--
: Eric S Fraga via Emacs 28.0.50, Org release_9.4.4-254-g37749c
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Sharing variables between source blocks without session
2021-03-19 13:26 ` Eric S Fraga
@ 2021-03-19 13:59 ` Loris Bennett
2021-03-19 14:26 ` Eric S Fraga
0 siblings, 1 reply; 13+ messages in thread
From: Loris Bennett @ 2021-03-19 13:59 UTC (permalink / raw)
To: Org Mode Mailing List
Eric S Fraga <e.fraga@ucl.ac.uk> writes:
> On Friday, 19 Mar 2021 at 07:38, Loris Bennett wrote:
>> However, the restriction to source blocks of a particular language does
>> not seem to work like this, but maybe I have got the syntax wrong
>> (again).
>
> Maybe ;-)
>
> This seems to work for me for a shell:
>
> :PROPERTIES:
> :header-args:sh: :var user="loris"
> :END:
That works for me, too. The following correctly does not work:
:PROPERTIES:
:header-args:R: :var user="loris"
:END:
#+begin_src sh
echo user ${user}
#+end_src
#+RESULTS:
: user
To be honest, I find it is a wee bit confusing that it's
#+PROPERTY: header-args:sh :var user="loris"
*without* a colon after the language (if I add it, there is not error,
but the variable is just not set) but
:PROPERTIES:
:header-args:sh: :var user="loris"
:END:
*with* a colon after the language, but maybe I'm just easily confused
:-)
Thanks for the help,
Loris
--
This signature is currently under construction.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Sharing variables between source blocks without session
2021-03-19 13:59 ` Loris Bennett
@ 2021-03-19 14:26 ` Eric S Fraga
2021-03-22 7:39 ` Loris Bennett
0 siblings, 1 reply; 13+ messages in thread
From: Eric S Fraga @ 2021-03-19 14:26 UTC (permalink / raw)
To: Loris Bennett; +Cc: Org Mode Mailing List
On Friday, 19 Mar 2021 at 14:59, Loris Bennett wrote:
> To be honest, I find it is a wee bit confusing that it's
>
> #+PROPERTY: header-args:sh :var user="loris"
>
> *without* a colon after the language (if I add it, there is not error,
> but the variable is just not set) but
>
> :PROPERTIES:
> :header-args:sh: :var user="loris"
> :END:
When in a property drawer, the name of the property you want to set is
enclosed within a pair of :s. When in a #+PROPERTY statement, there is
no need for the :s, just the name. It's not about the language part of
it at all.
--
: Eric S Fraga via Emacs 28.0.50, Org release_9.4.4-254-g37749c
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Sharing variables between source blocks without session
2021-03-19 14:26 ` Eric S Fraga
@ 2021-03-22 7:39 ` Loris Bennett
0 siblings, 0 replies; 13+ messages in thread
From: Loris Bennett @ 2021-03-22 7:39 UTC (permalink / raw)
To: emacs-orgmode
Eric S Fraga <e.fraga@ucl.ac.uk> writes:
> On Friday, 19 Mar 2021 at 14:59, Loris Bennett wrote:
>> To be honest, I find it is a wee bit confusing that it's
>>
>> #+PROPERTY: header-args:sh :var user="loris"
>>
>> *without* a colon after the language (if I add it, there is not error,
>> but the variable is just not set) but
>>
>> :PROPERTIES:
>> :header-args:sh: :var user="loris"
>> :END:
>
> When in a property drawer, the name of the property you want to set is
> enclosed within a pair of :s. When in a #+PROPERTY statement, there is
> no need for the :s, just the name. It's not about the language part of
> it at all.
Thanks for the clarification. I didn't think it was about the language
part - I was just using the position describe which colon I meant. The
colon gets to do quite a lot of work in Org :-)
Cheers,
Loris
--
This signature is currently under construction.
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2021-03-22 7:41 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-03-16 8:56 Sharing variables between source blocks without session Loris Bennett
2021-03-16 9:16 ` Loris Bennett
2021-03-16 13:28 ` Eric S Fraga
2021-03-18 13:21 ` Loris Bennett
2021-03-18 15:56 ` Eric S Fraga
2021-03-19 6:50 ` Loris Bennett
2021-03-18 17:59 ` Cook, Malcolm
2021-03-19 6:38 ` Loris Bennett
2021-03-19 7:04 ` Loris Bennett
2021-03-19 13:26 ` Eric S Fraga
2021-03-19 13:59 ` Loris Bennett
2021-03-19 14:26 ` Eric S Fraga
2021-03-22 7:39 ` Loris Bennett
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).