emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Orgmode] What is output for org-babel?


From: Dan Davison
Subject: Re: [Orgmode] What is output for org-babel?
Date: Wed, 11 Nov 2009 19:25:16 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)

Dan Davison <address@hidden> writes:

> Darlan Cavalcante Moreira <address@hidden> 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 via email to

[Prev in Thread] Current Thread [Next in Thread]