emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Buffer-wide definitions in org-babel
@ 2009-10-07 14:20 Juan Reyero
  2009-10-07 18:48 ` Eric Schulte
  0 siblings, 1 reply; 7+ messages in thread
From: Juan Reyero @ 2009-10-07 14:20 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 658 bytes --]

Greetings,
I am trying to define buffer-wide initializations in org-babel, so that I
can import a python module once and then use its exported symbols in all the
code chunks throughout the buffer.  Is there a way to do it?  I have tried
all the obvious approaches and none seems to work.  (My hope was that I
could define a :session and then use it in every chunk, but python doesn't
like it).

I just found out about org-babel, and I am delighted by its promise.
 Org-mode has changed the way I work (after a year and a half I could not
live without it) but now I think this is going to be another bit step.

Many thanks,

jm
-- 
http://juanreyero.com/blog

[-- Attachment #1.2: Type: text/html, Size: 823 bytes --]

[-- Attachment #2: Type: text/plain, Size: 204 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Remember: use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Buffer-wide definitions in org-babel
  2009-10-07 14:20 Buffer-wide definitions in org-babel Juan Reyero
@ 2009-10-07 18:48 ` Eric Schulte
  2009-10-07 19:17   ` Eric Schulte
  2009-10-08  9:22   ` Juan Reyero
  0 siblings, 2 replies; 7+ messages in thread
From: Eric Schulte @ 2009-10-07 18:48 UTC (permalink / raw)
  To: Juan Reyero; +Cc: emacs-orgmode

Juan Reyero <joanmg@gmail.com> writes:

> Greetings,
>

Hi,

>
> I am trying to define buffer-wide initializations in org-babel, so that I can import a python module once
> and then use its exported symbols in all the code chunks throughout the buffer.  Is there a way to do it?
>  I have tried all the obvious approaches and none seems to work.  (My hope was that I could define a
> :session and then use it in every chunk, but python doesn't like it). 
>

I believe you are on the right track by trying to use sessions.  The
following works for me

--8<---------------cut here---------------start------------->8---
** persistent python
#+begin_src python :session :results silent
  import types
#+end_src

#+begin_src python :session
  types.FunctionType
#+end_src

#+resname:
: function
--8<---------------cut here---------------end--------------->8---

Of if you grab the latest version of Org-mode from the git repo you can
set the session type in a headline property which would be more similar
to the file-wide behavior that you described.

--8<---------------cut here---------------start------------->8---
*** more persistent python
    :PROPERTIES:
    :session:  default
    :END:

#+begin_src python :results silent
  import types
#+end_src

#+begin_src python
  types.FunctionType
#+end_src

#+resname:
: function
--8<---------------cut here---------------end--------------->8---

please let me know if the above don't work for you, or if you are
encountering some other python-babel issues.

>
> I just found out about org-babel, and I am delighted by its promise.

Thanks! I hope it lives up it's promise.

Best -- Eric

> Org-mode has changed the way I work (after a year and a half I could
> not live without it) but now I think this is going to be another bit
> step. 
>
> Many thanks,
>
> jm
> --
> http://juanreyero.com/blog
>
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Buffer-wide definitions in org-babel
  2009-10-07 18:48 ` Eric Schulte
@ 2009-10-07 19:17   ` Eric Schulte
  2009-10-08 11:16     ` Juan Reyero
  2009-10-08  9:22   ` Juan Reyero
  1 sibling, 1 reply; 7+ messages in thread
From: Eric Schulte @ 2009-10-07 19:17 UTC (permalink / raw)
  To: Juan Reyero; +Cc: emacs-orgmode

Hi,

This brings up a need for further Org-babel development.

As you described below the :session environment can be used to deal with
the need for required "stage setting" source-code blocks, however there
is currently no way when directly executing a block to specify that some
other block in the same session must be evaluated first to setup the
environment.

Perhaps a new header argument named :requires would be useful here.
This would allow any block to "require" that some other named
source-code block be prepended to it's self before evaluation.

I'll add this as a task to the Org-babel development file.

Thanks for helping to point out this need.

Best -- Eric

"Eric Schulte" <schulte.eric@gmail.com> writes:

> Juan Reyero <joanmg@gmail.com> writes:
>
>> Greetings,
>>
>
> Hi,
>
>>
>> I am trying to define buffer-wide initializations in org-babel, so that I can import a python module once
>> and then use its exported symbols in all the code chunks throughout the buffer.  Is there a way to do it?
>>  I have tried all the obvious approaches and none seems to work.  (My hope was that I could define a
>> :session and then use it in every chunk, but python doesn't like it). 
>>
>
> I believe you are on the right track by trying to use sessions.  The
> following works for me
>
> ** persistent python
> #+begin_src python :session :results silent
>   import types
> #+end_src
>
> #+begin_src python :session
>   types.FunctionType
> #+end_src
>
> #+resname:
> : function
>
> Of if you grab the latest version of Org-mode from the git repo you can
> set the session type in a headline property which would be more similar
> to the file-wide behavior that you described.
>
> *** more persistent python
>     :PROPERTIES:
>     :session:  default
>     :END:
>
> #+begin_src python :results silent
>   import types
> #+end_src
>
> #+begin_src python
>   types.FunctionType
> #+end_src
>
> #+resname:
> : function
>
> please let me know if the above don't work for you, or if you are
> encountering some other python-babel issues.
>
>>
>> I just found out about org-babel, and I am delighted by its promise.
>
> Thanks! I hope it lives up it's promise.
>
> Best -- Eric
>
>> Org-mode has changed the way I work (after a year and a half I could
>> not live without it) but now I think this is going to be another bit
>> step. 
>>
>> Many thanks,
>>
>> jm
>> --
>> http://juanreyero.com/blog
>>
>> _______________________________________________
>> Emacs-orgmode mailing list
>> Remember: use `Reply All' to send replies to the list.
>> Emacs-orgmode@gnu.org
>> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Buffer-wide definitions in org-babel
  2009-10-07 18:48 ` Eric Schulte
  2009-10-07 19:17   ` Eric Schulte
@ 2009-10-08  9:22   ` Juan Reyero
  2009-10-08 14:11     ` Dan Davison
  1 sibling, 1 reply; 7+ messages in thread
From: Juan Reyero @ 2009-10-08  9:22 UTC (permalink / raw)
  To: emacs-orgmode

Eric,

Thanks a lot for your quick response.  I have tried your suggestion
and it does work, but it behaves in an unexpected way when I do some
minor modifications.  Please see below.

On Wed, Oct 7, 2009 at 8:48 PM, Eric Schulte <schulte.eric@gmail.com> wrote:
>> I am trying to define buffer-wide initializations in org-babel, so that I can import a python module once
>> and then use its exported symbols in all the code chunks throughout the buffer.  Is there a way to do it?
>>  I have tried all the obvious approaches and none seems to work.  (My hope was that I could define a
>> :session and then use it in every chunk, but python doesn't like it).
>
> I believe you are on the right track by trying to use sessions.  The
> following works for me
>
> --8<---------------cut here---------------start------------->8---
> ** persistent python
> #+begin_src python :session :results silent
>  import types
> #+end_src
>
> #+begin_src python :session
>  types.FunctionType
> #+end_src
>
> #+resname:
> : function
> --8<---------------cut here---------------end--------------->8---

This works, but see what happens with this (no previous src chunks):

#+begin_src python :session :results output
 2
#+end_src

#+resname:
: 2
: 2

(expected nothing, which is what I get if I remove the :session).

#+begin_src python :session :results value
 2
#+end_src

#+resname:
: 0

This is how my python buffer looks like after processing this last
chunk in a fresh session:

--8<---------------cut here---------------start--------------->8---
>>> import emacs; print '_emacs_out ()'
/Applications/Emacs.app/Contents/Resources/etc/emacs.py:24:
DeprecationWarning: the sets module is deprecated
  from sets import Set
2


_
'org_babel_python_eoe'
2
2
>>>
>>>
>>> _
2
>>> 'org_babel_python_eoe'
'org_babel_python_eoe'
>>>
--8<---------------cut here---------------end--------------->8---

But, interestingly, if I return a string instead of a number, as you
do in your example, it works:

#+begin_src python :session :results value
 "2"
#+end_src

#+resname:
: 2

It is not related to a previous chunk messing up the python
interpreter.  I have moved to the git version, and it still behaves
like this.  I am using python 2.6.1.

> Of if you grab the latest version of Org-mode from the git repo you can
> set the session type in a headline property which would be more similar
> to the file-wide behavior that you described.

This is cool.  It would also be great if you could define a default
interpreter (I suspect most usage will involve a single interpreter
per buffer).

Thanks again!

jm
-- 
http://juanreyero.com/blog

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Buffer-wide definitions in org-babel
  2009-10-07 19:17   ` Eric Schulte
@ 2009-10-08 11:16     ` Juan Reyero
  0 siblings, 0 replies; 7+ messages in thread
From: Juan Reyero @ 2009-10-08 11:16 UTC (permalink / raw)
  To: emacs-orgmode

Hi,

On Wed, Oct 7, 2009 at 9:17 PM, Eric Schulte <schulte.eric@gmail.com> wrote:
> As you described below the :session environment can be used to deal with
> the need for required "stage setting" source-code blocks, however there
> is currently no way when directly executing a block to specify that some
> other block in the same session must be evaluated first to setup the
> environment.
>
> Perhaps a new header argument named :requires would be useful here.
> This would allow any block to "require" that some other named
> source-code block be prepended to it's self before evaluation.

Sounds like a good solution.  Another possibility would be to add an
option that makes chunks dependent on other chunks that appear earlier
in the buffer.  It is less general, but possibly simpler to implement
(you don't have to worry about circular dependencies) and less
verbose.  If you could assume a functional style without side effects
you could even track which chunks are up-to-date, and only re-compute
from the first one not up-to-date in the buffer onwards to the chunk
you are being asked to process.  This could be yet another option.

Best,

jm
-- 
http://juanreyero.com/blog

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Buffer-wide definitions in org-babel
  2009-10-08  9:22   ` Juan Reyero
@ 2009-10-08 14:11     ` Dan Davison
  2009-10-09 14:53       ` Juan Reyero
  0 siblings, 1 reply; 7+ messages in thread
From: Dan Davison @ 2009-10-08 14:11 UTC (permalink / raw)
  To: Juan Reyero; +Cc: emacs-orgmode

Juan Reyero <joanmg@gmail.com> writes:

[...]
>> --8<---------------cut here---------------start------------->8---
>> ** persistent python
>> #+begin_src python :session :results silent
>>  import types
>> #+end_src
>>
>> #+begin_src python :session
>>  types.FunctionType
>> #+end_src
>>
>> #+resname:
>> : function
>> --8<---------------cut here---------------end--------------->8---
>
> This works, but see what happens with this (no previous src chunks):
>
> #+begin_src python :session :results output
>  2
> #+end_src
>
> #+resname:
> : 2
> : 2

Hi Juan,

Thanks for the org-babel suggestions. However I can't replicate this
behaviour under linux. I get

#+resname:
: 2

for all three examples.

>
> (expected nothing, which is what I get if I remove the :session).

An understandable expectation. In non-session mode, we collect stdout
and if the expression "2" is passed to the interpreter nothing is output
to stdout. However in session mode we collect whatever output appears in
the comint buffer, and if you give the interpreter "2" the interpreter
comes back and prints the value of that expression.

I'm using org-version 6.31trans in emacs-version 23.0.91.1 under ubuntu
jaunty with python 2.6.2. Is this definitely replicable under OSX?

Dan

>
> #+begin_src python :session :results value
>  2
> #+end_src
>
> #+resname:
> : 0
>
> This is how my python buffer looks like after processing this last
> chunk in a fresh session:
>
>>>> import emacs; print '_emacs_out ()'
> /Applications/Emacs.app/Contents/Resources/etc/emacs.py:24:
> DeprecationWarning: the sets module is deprecated
>   from sets import Set
> 2
>
>
> _
> 'org_babel_python_eoe'
> 2
> 2
>>>>
>>>>
>>>> _
> 2
>>>> 'org_babel_python_eoe'
> 'org_babel_python_eoe'
>>>>

>>> 2


_
'org_babel_python_eoe'
2
>>> >>> >>> 2
>>> 'org_babel_python_eoe'
>>> 

>
> But, interestingly, if I return a string instead of a number, as you
> do in your example, it works:
>
> #+begin_src python :session :results value
>  "2"
> #+end_src
>
> #+resname:
> : 2
>
> It is not related to a previous chunk messing up the python
> interpreter.  I have moved to the git version, and it still behaves
> like this.  I am using python 2.6.1.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Buffer-wide definitions in org-babel
  2009-10-08 14:11     ` Dan Davison
@ 2009-10-09 14:53       ` Juan Reyero
  0 siblings, 0 replies; 7+ messages in thread
From: Juan Reyero @ 2009-10-09 14:53 UTC (permalink / raw)
  To: Dan Davison; +Cc: emacs-orgmode

Hi Dan,

On Thu, Oct 8, 2009 at 4:11 PM, Dan Davison <davison@stats.ox.ac.uk> wrote:
> Juan Reyero <joanmg@gmail.com> writes:
>> #+begin_src python :session :results output
>>  2
>> #+end_src
>>
>> #+resname:
>> : 2
>> : 2
>>
>> (expected nothing, which is what I get if I remove the :session).
>
> An understandable expectation. In non-session mode, we collect stdout
> and if the expression "2" is passed to the interpreter nothing is output
> to stdout. However in session mode we collect whatever output appears in
> the comint buffer, and if you give the interpreter "2" the interpreter
> comes back and prints the value of that expression.

Ah, got it.  Thanks a lot.  It's kind of tricky to know what you are
going to get, however.  For example:

#+begin_src python :session :results output
str('10' + 'm/s')
'12'
#+end_src

#+resname:
: 10' + 'm/s')
: '10m/s
: 12'
: '12

I guess the answer to that would be to only use :results value when in
:session mode.

> ... However I can't replicate this
> behaviour under linux. I get
>
> #+resname:
> : 2
>
> for all three examples.
>
> I'm using org-version 6.31trans in emacs-version 23.0.91.1 under ubuntu
> jaunty with python 2.6.2. Is this definitely replicable under OSX?

Yes, definitely.  I am using emacs version 22.3.1, and python 2.6.1.
I have stripped bare my .emacs, and still:

#+begin_src python :session :results value
 2
#+end_src

#+resname:
: 0

jm
-- 
http://juanreyero.com/blog

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2009-10-09 14:53 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-10-07 14:20 Buffer-wide definitions in org-babel Juan Reyero
2009-10-07 18:48 ` Eric Schulte
2009-10-07 19:17   ` Eric Schulte
2009-10-08 11:16     ` Juan Reyero
2009-10-08  9:22   ` Juan Reyero
2009-10-08 14:11     ` Dan Davison
2009-10-09 14:53       ` Juan Reyero

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