emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Juan Reyero <joanmg@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Re: Buffer-wide definitions in org-babel
Date: Thu, 8 Oct 2009 11:22:58 +0200	[thread overview]
Message-ID: <55bd243d0910080222q34a7ac79g26a7f9358d806e62@mail.gmail.com> (raw)
In-Reply-To: <m2ocojavlj.fsf@gmail.com>

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

  parent reply	other threads:[~2009-10-08  9:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2009-10-08 14:11     ` Dan Davison
2009-10-09 14:53       ` Juan Reyero

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=55bd243d0910080222q34a7ac79g26a7f9358d806e62@mail.gmail.com \
    --to=joanmg@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).