millejoh / emacs-ipython-notebook

Jupyter notebook client in Emacs
http://millejoh.github.io/emacs-ipython-notebook/
GNU General Public License v3.0
1.47k stars 122 forks source link

Result from ob-ein source block is not the same as STDOUT when using `:results output` header. #696

Closed yatsky closed 4 years ago

yatsky commented 4 years ago

Problem description

Result from ob-ein source block is not the same as STDOUT when using :results output header.

Steps to reproduce the problem

In an org file, run the following src block using EIN.

#+name: 1 test my solution
#+caption: 1 test my solution
#+begin_src ein-python :session localhost :results output code
def abc():
    print("test")
    return "abc"
abc()
#+end_src

Get the following. A newline and a string of "abc". Expecting test and "abc".

+RESULTS: 1 test my solution

+begin_src none

'abc'

+end_src

But if we run the following python code without using EIN.

#+begin_src python :results output code

def abc():
    print("test")
    return "abc"
abc()
#+end_src

We get the following. Only the string "test" is in the output. Expecting test and "abc".

+RESULTS:

+begin_src python

test

+end_src

If we run the abc() function in python REPL.

>>> def abc():
...     print("test")
...     return "abc"
... 
>>> abc()
test
'abc'
>>> 

According to the documentation, when using :results output header, result should be collected from STDOUT during execution of the code, which sounds like it should include both test and 'abc'. And this had been the case before I updated EIN to the current version, but I don't remember which version I was using or if I mixed up something when I did the update.

System info:

("EIN system info"
:emacs-version
"GNU Emacs 26.3 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.22.30)
of 2019-09-17"
:emacs-bzr-version nil
:window-system x
:emacs-variant nil
:os
(:uname
"Linux Als-PC 4.4.0-17763-Microsoft #864-Microsoft Thu Nov 07 15:22:00 PST 2019 x86_64 x86_64 x86_64 GNU/Linux
"
:lsb-release
"No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.4 LTS
Release:    18.04
Codename:   bionic
")
:notebook
"Name: notebook
Version: 6.0.3
Summary: A web-based notebook environment for interactive computing
Home-page: http://jupyter.org
Author: Jupyter Development Team
Author-email: jupyter@googlegroups.com
License: BSD
Location: /home/als/.virtualenvs/data/lib/python3.6/site-packages
Requires: jinja2, traitlets, Send2Trash, terminado, pyzmq, nbformat, jupyter-core, ipykernel, ipython-genutils, nbconvert, tornado, jupyter-client, prometheus-client
Required-by: widgetsnbextension, jupyter
"
:ipython "7.13.0
"
:image-types
(svg imagemagick png gif tiff jpeg xpm xbm pbm postscript)
:image-types-available
(svg imagemagick png gif tiff jpeg xpm xbm pbm postscript)
:request
(:backend curl)
:ein
(:version "20200328.2131"
:source-dir "/home/als/.emacs.d/elpa/ein-20200328.2131/")
:lib
((:name "websocket"
:path "~/.emacs.d/elpa/websocket-20191017.30/websocket.elc"
:featurep t
:version-var websocket-version
:version "1.11.1")
(:name "anaphora"
:path "~/.emacs.d/elpa/anaphora-20180618.2200/anaphora.elc"
:featurep t
:version-var nil
:version nil)
(:name "request"
:path "~/.emacs.d/elpa/request-20200219.2257/request.elc"
:featurep t
:version-var request-version
:version "0.3.0")
(:name "deferred"
:path "~/.emacs.d/elpa/deferred-20170901.1330/deferred.elc"
:featurep t
:version-var deferred:version
:version "0.5.0")
(:name "polymode"
:path "~/.emacs.d/elpa/polymode-20190714.2017/polymode.elc"
:featurep t
:version-var nil
:version nil)
(:name "dash"
:path "~/.emacs.d/elpa/dash-20191109.1327/dash.elc"
:featurep t
:version-var nil
:version nil)))
dickmao commented 4 years ago

Thanks for reporting the bug.

yatsky commented 4 years ago

Hi @dickmao , Thanks for the quick fix, but now I'm only getting "test", is this desired behaviour? Thanks.

#+name: 1 test my solution
#+caption: 1 test my solution
#+begin_src ein-python :session localhost :results output code
def abc():
    print("test")
    return "abc"
abc()
#+end_src

#+RESULTS: 1 test my solution
#+begin_src none
test

#+end_src
dickmao commented 4 years ago

It matches ob-python which also just returns "test". "abc" is a return value; it is not part of stdout.

yatsky commented 4 years ago

Thanks.

bibekpokharel commented 4 years ago

I tried to implement the following. Here if I set it the results to 'output', I get 'test' printed but I don't see the plot, and if I set results to raw then I see the plot but 'test' is not printed. Is there a way to have both 'test and the plot printed?


#+BEGIN_SRC ein-python :session localhost :results output raw drawer
import numpy, math, matplotlib.pyplot as plt
%matplotlib inline
x = numpy.linspace(0, 2*math.pi)
def abc():
    print("test")
    return "abc"
abc()
plt.plot(x, numpy.sin(x))
plt.show()
#+END_SRC```
dickmao commented 4 years ago

Sorry to yatsky for having dismissed this concern. Yes, the relevant org section says under :session both output and results should be displayed, but if :session is not set the old behavior of omitting the result was correct.

Since ob-ein should always have :session set, we should always print everything.

Commit 9b52b02