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.
"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
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
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.
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
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. >
"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. >>
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.
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.
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
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.
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
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.