Skip to main content
added 1596 characters in body
Source Link
Aaron Hall
  • 434
  • 1
  • 6
  • 21

As you said in a comment:

Setting :results to values would only print the last value of the function. My goal is to have exactly the same output as in the interactive interpreter: the values of all typed expressions and of all print statements.

You can't have both, but if you want "exactly the same output as in an interactive interpreter" from multiple lines, you can get that by printing the results of the expression from the repr function if the value is not None.

So here's a convenience function for that, output (indented because I edited it in a separate python-mode buffer, accessed with C-c '):

#+BEGIN_SRC python :session :results output def output(obj): if obj is None: return None print(repr(obj)) import sys print(sys.version) output(sys.version) output("hello") output(4-1) output(list("hello")) #+END_SRC #+RESULTS: : 3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) : [GCC 7.2.0] : '3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) \n[GCC 7.2.0]' : 'hello' : 3 : ['h', 'e', 'l', 'l', 'o'] : : 

Note that strings will have their quotes and literal newlines, while numbers will also look like their literals. This is how the interactive interpreter works.

But I don't think it's very elegant in this context, and as people have been writing Python in Emacs for a long time, I doubt we can come up with a better approach.

Explanation

In a code block with :results output it takes the output from standard out, that is, what you print, as the results. This is what you were doing:

In a code block with :results output it takes the output from standard out, that is, what you print, as the results. This is what you were doing:

As you said in a comment:

Setting :results to values would only print the last value of the function. My goal is to have exactly the same output as in the interactive interpreter: the values of all typed expressions and of all print statements.

You can't have both, but if you want "exactly the same output as in an interactive interpreter" from multiple lines, you can get that by printing the results of the expression from the repr function if the value is not None.

So here's a convenience function for that, output (indented because I edited it in a separate python-mode buffer, accessed with C-c '):

#+BEGIN_SRC python :session :results output def output(obj): if obj is None: return None print(repr(obj)) import sys print(sys.version) output(sys.version) output("hello") output(4-1) output(list("hello")) #+END_SRC #+RESULTS: : 3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) : [GCC 7.2.0] : '3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) \n[GCC 7.2.0]' : 'hello' : 3 : ['h', 'e', 'l', 'l', 'o'] : : 

Note that strings will have their quotes and literal newlines, while numbers will also look like their literals. This is how the interactive interpreter works.

But I don't think it's very elegant in this context, and as people have been writing Python in Emacs for a long time, I doubt we can come up with a better approach.

Explanation

In a code block with :results output it takes the output from standard out, that is, what you print, as the results. This is what you were doing:

deleted 12 characters in body
Source Link
NickD
  • 36k
  • 4
  • 33
  • 50

In a code block with :results output it takes the output from standard out, that is, what you print, as the results. This is what you were doing:

#+BEGIN_SRC python :session :results output import sys print(sys.version) #+END_SRC #+RESULTS: : 3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) : [GCC 7.2.0] 

If you don't print (or write to standard out) with output, you get nothing:

#+BEGIN_SRC python :session :results output import sys sys.version #+END_SRC #+RESULTS: 

Orgmode will show the last value when you use session, when you use the :results value (which you can actually leave out because it is the default argument.):

#+BEGIN_SRC python :session :results value import sys sys.version #+END_SRC #+RESULTS: : 3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) : [GCC 7.2.0] 

Or you can specify suboptions for value:

#+BEGIN_SRC python :session :results value raw import sys sys.version #+END_SRC #+RESULTS: 3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) [GCC 7.2.0] 

So if you remove the argument, you get what you were actually expecting (since print() returns None):

#+BEGIN_SRC python :session 4-2 print(4+5) #+END_SRC #+RESULTS: : 2 

From orgmode's docs

:results {output, value}: Value mode is the default (as with other languages). In value mode you can use the following subtypes:

  • raw: value is inserted directly
  • pp: value is pretty-printed by python using pprint.pformat(%s), then inserted
  • file: value is interpreted as a filename to be interpolated when exporting; commonly used for graphics output.

In a code block with :results output it takes the output from standard out, that is, what you print, as the results. This is what you were doing:

#+BEGIN_SRC python :session :results output import sys print(sys.version) #+END_SRC #+RESULTS: : 3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) : [GCC 7.2.0] 

If you don't print (or write to standard out) with output, you get nothing:

#+BEGIN_SRC python :session :results output import sys sys.version #+END_SRC #+RESULTS: 

Orgmode will show the last value when you use session, when you use the :results value (which you can actually leave out because it is the default argument.):

#+BEGIN_SRC python :session :results value import sys sys.version #+END_SRC #+RESULTS: : 3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) : [GCC 7.2.0] 

Or you can specify suboptions for value:

#+BEGIN_SRC python :session :results value raw import sys sys.version #+END_SRC #+RESULTS: 3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) [GCC 7.2.0] 

So if you remove the argument, you get what you were actually expecting (since print() returns None):

#+BEGIN_SRC python :session 4-2 print(4+5) #+END_SRC #+RESULTS: : 2 

From orgmode's docs

:results {output, value}: Value mode is the default (as with other languages). In value mode you can use the following subtypes:

  • raw: value is inserted directly
  • pp: value is pretty-printed by python using pprint.pformat(%s), then inserted
  • file: value is interpreted as a filename to be interpolated when exporting; commonly used for graphics output.

In a code block with :results output it takes the output from standard out, that is, what you print, as the results. This is what you were doing:

#+BEGIN_SRC python :session :results output import sys print(sys.version) #+END_SRC #+RESULTS: : 3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) : [GCC 7.2.0] 

If you don't print (or write to standard out) with output, you get nothing:

#+BEGIN_SRC python :session :results output import sys sys.version #+END_SRC #+RESULTS: 

Orgmode will show the last value when you use session, when you use the :results value (which you can actually leave out because it is the default):

#+BEGIN_SRC python :session :results value import sys sys.version #+END_SRC #+RESULTS: : 3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) : [GCC 7.2.0] 

Or you can specify suboptions for value:

#+BEGIN_SRC python :session :results value raw import sys sys.version #+END_SRC #+RESULTS: 3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) [GCC 7.2.0] 

So if you remove the argument, you get what you were actually expecting (since print() returns None):

#+BEGIN_SRC python :session 4-2 print(4+5) #+END_SRC #+RESULTS: : 2 

From orgmode's docs

:results {output, value}: Value mode is the default (as with other languages). In value mode you can use the following subtypes:

  • raw: value is inserted directly
  • pp: value is pretty-printed by python using pprint.pformat(%s), then inserted
  • file: value is interpreted as a filename to be interpolated when exporting; commonly used for graphics output.
Source Link
Aaron Hall
  • 434
  • 1
  • 6
  • 21

In a code block with :results output it takes the output from standard out, that is, what you print, as the results. This is what you were doing:

#+BEGIN_SRC python :session :results output import sys print(sys.version) #+END_SRC #+RESULTS: : 3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) : [GCC 7.2.0] 

If you don't print (or write to standard out) with output, you get nothing:

#+BEGIN_SRC python :session :results output import sys sys.version #+END_SRC #+RESULTS: 

Orgmode will show the last value when you use session, when you use the :results value (which you can actually leave out because it is the default argument.):

#+BEGIN_SRC python :session :results value import sys sys.version #+END_SRC #+RESULTS: : 3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) : [GCC 7.2.0] 

Or you can specify suboptions for value:

#+BEGIN_SRC python :session :results value raw import sys sys.version #+END_SRC #+RESULTS: 3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) [GCC 7.2.0] 

So if you remove the argument, you get what you were actually expecting (since print() returns None):

#+BEGIN_SRC python :session 4-2 print(4+5) #+END_SRC #+RESULTS: : 2 

From orgmode's docs

:results {output, value}: Value mode is the default (as with other languages). In value mode you can use the following subtypes:

  • raw: value is inserted directly
  • pp: value is pretty-printed by python using pprint.pformat(%s), then inserted
  • file: value is interpreted as a filename to be interpolated when exporting; commonly used for graphics output.