emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Dan Davison <davison@stats.ox.ac.uk>
To: Darlan Cavalcante Moreira <darcamo@gmail.com>
Cc: emacs-orgmode@gnu.org, andrea Crotti <andrea.crotti.0@gmail.com>
Subject: Re: What is output for org-babel?
Date: Wed, 11 Nov 2009 19:25:16 -0500	[thread overview]
Message-ID: <87tyx0r3mb.fsf@stats.ox.ac.uk> (raw)
In-Reply-To: 87skcoydie.fsf@stats.ox.ac.uk

Dan Davison <davison@stats.ox.ac.uk> writes:

> Darlan Cavalcante Moreira <darcamo@gmail.com> writes:
>
<...>
>>> So I investigate and for example this
>>> 
>>> #+BEGIN_SRC python
>>> import os; os.listdir(os.getcwd())
>>> #+END_SRC
>>
>> This does not return anything for me either, but
>> #+BEGIN_SRC python
>> import os
>> os.listdir(os.getcwd())
>> #+END_SRC
>> works as expected. This is strange, since "import os; os.listdir(os.getcwd())"
>> is valid and will work if typed in the python interpreter directly.
>
> This is a limitation of the current implementation of :results value
> evaluation. 

I've just pushed a change which fixes this and related problems. It
introduces a change for python users: when using the default evaluation
mode (':results value' non-session), if you want the source code block
to return a value, you must now include the 'return' statement that
would be required if you were writing a python function definition.

On Worg I've added some more detailed documentation of exactly how the
result is obtained in the four different cases (value/output,
session/non-session), and I'm also pasting that below.

Dan


*** Evaluation results: output/value & session/non-session
    
    The following applies particularly to perl, python, R and ruby.

Nature of Results:

|        | non-session              | session                             |
|--------+--------------------------+-------------------------------------|
| value  | value of last expression | value of last expression            |
| output | contents of stdout       | concatenation of interpreter output |


Note that in ':results value' (session and non-session), the result is
imported into org-mode as a table (a one- or two-dimensional vector of
strings or numbers) when appropriate.

**** :results value (non-session)
     This is the default. Internally, the value is obtained by
     wrapping the code in a function definition in the external
     language, and evaluating that function. Therefore, code should be
     written as if it were the body of such a function. In particular,
     note that python does not automatically return a value from a
     function unless a =return= statement is present, and so a
     'return' statement will usually be required in python :results
     value (non-session).

     This is the only one of the four evaluation contexts in which the
     code is automatically wrapped in a function definition.

**** :results output (non-session)
     The code is passed to the interpreter as an external process, and
     the contents of the standard output stream is returned as text. (In
     certain languages this also contains the error output stream; this
     is an area for future work.)

**** :results value (session)
     The code is passed to the interpreter running as an interactive
     emacs inferior process. The result returned is the result of the
     last evaluation performed by the interpreter. (This is obtained in
     a language-specific manner: the value of the variable =_= in
     python and ruby, and the value of =.Last.value= in R).

**** :results output (session)
     The code is passed to the interpreter running as an interactive
     emacs inferior process. The result returned is the concatenation
     of the sequence of (text) output from the interactive
     interpreter. Notice that this is not necessarily the same as what
     would be sent to stdout if the same code were passed to a
     non-interactive interpreter running as an external process. For
     example, compare the following two blocks:

#+begin_src python :results output
  print "hello"
  2
  print "bye"
#+end_src

#+resname:
: hello
: bye

In non-session mode, the '2' is not printed and does not appear.

#+begin_src python :results output :session
  print "hello"
  2
  print "bye"
#+end_src

#+resname:
: hello
: 2
: bye

But in session mode, the interactive interpreter receives input '2'
and prints out its value, '2'. (Indeed, the other print statements are
unnecessary here).

  reply	other threads:[~2009-11-12  0:25 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-05 11:17 What is output for org-babel? andrea Crotti
2009-11-05 14:26 ` Darlan Cavalcante Moreira
2009-11-06 12:13   ` andrea Crotti
2009-11-06 12:17     ` Carsten Dominik
2009-11-06 16:15     ` Thomas S. Dye
2009-11-08 20:20   ` Dan Davison
2009-11-12  0:25     ` Dan Davison [this message]
2009-11-08 15:05 ` "Martin G. Skjæveland"
2009-11-08 15:19   ` "Martin G. Skjæveland"
2009-11-08 19:48   ` Dan Davison

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=87tyx0r3mb.fsf@stats.ox.ac.uk \
    --to=davison@stats.ox.ac.uk \
    --cc=andrea.crotti.0@gmail.com \
    --cc=darcamo@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).