From mboxrd@z Thu Jan 1 00:00:00 1970 From: Dror Atariah Subject: Capturing file name from python session when using matplotlib Date: Mon, 11 May 2015 20:48:38 +0200 Message-ID: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=f46d0444027675915f0515d2d353 Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:39566) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yrskt-0001pV-Rt for emacs-orgmode@gnu.org; Mon, 11 May 2015 14:48:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yrskq-00035n-5T for emacs-orgmode@gnu.org; Mon, 11 May 2015 14:48:43 -0400 Received: from mail-wg0-x22d.google.com ([2a00:1450:400c:c00::22d]:33589) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yrskp-00035j-S1 for emacs-orgmode@gnu.org; Mon, 11 May 2015 14:48:40 -0400 Received: by wgin8 with SMTP id n8so137936408wgi.0 for ; Mon, 11 May 2015 11:48:38 -0700 (PDT) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: "emacs-orgmode@gnu.org" --f46d0444027675915f0515d2d353 Content-Type: text/plain; charset=UTF-8 * Capturing filename I would like to use (babel) orgmode as an interactive python notebook. Therefore, in order to allow the various code blocks to "know" each other, it is important to use the ~:session~ option. However, once ~:session~ is used, together with ~matplotlib~ the desired behavior is no longer in place. * Clean testing, no Matplotlib For the sake of testing, the following merely suppose to return the filename #+BEGIN_SRC python :session no_matplotlib :results file :exports both x = 'hello ' y = 'world' z = x + y 'foo.bar' #+END_SRC #+RESULTS: [[file:foo.bar]] *Passed* Indeed, the ~RESULTS~ block contains a link to the filename indicated by the last string. * Importing Matplotlib The following minimal example should generate a simple figure, ~myfig.png~, and orgmode should capture it and interpret it as a filename to be used by the following ~RESULTS~ block. #+BEGIN_SRC python :session with_matplotlib :results file import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt fig=plt.figure(figsize=(3,2)) plt.plot([1,3,2]) plt.savefig('myfig.png') 'myfig.png' #+END_SRC #+RESULTS: [[file:]] *Failed!* This time, it doesn't work anymore. The file ~myfig.png~ is properly generated, but the last string is not captured by ~matplotlib~. Following is the content of the corresponding interactive python session: #+BEGIN_SRC Python 3.4.3 (default, Feb 27 2015, 00:22:26) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> >>> >>> , '''/var/folders/kz/1c2cxn1x60n_t5p2j1p02b180000gn/T/py27084Z6S''', 'exec')); import matplotlib import matplotlib matplotlib.use('Agg') >>> matplotlib.use('Agg') import matplotlib.pyplot as plt import matplotlib.pyplot as plt fig=plt.figure(figsize=(3,2)) fig=plt.figure(figsize=(3,2)) plt.plot([1,3,2]) plt.plot([1,3,2]) plt.savefig('myfig.png') plt.savefig('myfig.png') 'myfig.png' 'myfig.png' open('/var/folders/kz/1c2cxn1x60n_t5p2j1p02b180000gn/T/babel-2708423D/python-27084mEZ', 'w').write(str(_)) open('/var/folders/kz/1c2cxn1x60n_t5p2j1p02b180000gn/T/babel-2708423D/python-27084mEZ', 'w').write(str(_)) 'org_babel_python_eoe' 'org_babel_python_eoe' >>> >>> >>> >>> [] >>> 'myfig.png' >>> >>> 9 >>> >>> >>> 'org_babel_python_eoe' >>> #+END_SRC * Some background ** Python Version #+BEGIN_SRC python :session background :results output import sys sys.version #+END_SRC #+RESULTS: : import sys : >>> sys.version : '3.4.3 (default, Feb 27 2015, 00:22:26) \n[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)]' : sys.executable : '/Users/drorata/py-envs/generic3.4/bin/python' : : ** System I am running Emacs version ~GNU Emacs 24.4.51.1 (x86_64-apple-darwin14.1.0, NS apple-appkit-1344.72) of 2015-02-02 on boxname.local~ The orgmode version is: ~Org-mode version 8.2.10 (8.2.10-40-gc763fa-elpaplus @ /Users/username/.emacs.d/elpa/org-plus-contrib-20150504/)~ ** Stackoverflow reference I also posted a question in the SE network: [[ http://emacs.stackexchange.com/questions/11075/org-mode-python-session-does-not-return-a-file-name ]] * Question So, my question is how to tackle/debug this issue? Can someone give me a hand here? That would be really great! Thanks! -- Dror Atariah, Ph.D. de.linkedin.com/in/atariah --f46d0444027675915f0515d2d353 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
* Capturing filename

I would= like to use (babel) orgmode as an interactive python
notebook. T= herefore, in order to allow the various code blocks to
"know= " each other, it is important to use the ~:session~
option. = However, once ~:session~ is used, together with ~matplotlib~
the = desired behavior is no longer in place.

* Clean te= sting, no Matplotlib
For the sake of testing, the following merel= y suppose to return the filename

#+BEGIN_SRC pytho= n :session no_matplotlib :results file :exports both
x =3D 'h= ello '
y =3D 'world'
z =3D x + y
= 'foo.bar'
#+END_SRC

#+RESULTS:
[[file:foo.bar]]

*Passed* Indeed, the ~RE= SULTS~ block contains a link to the filename
indicated by the las= t string.

* Importing Matplotlib

The following minimal example should generate a simple figure,
~myfig.png~, and orgmode should capture it and interpret it as a
filename to be used by the following ~RESULTS~ block.

<= /div>
#+BEGIN_SRC python :session with_matplotlib :results file
import matplotlib
matplotlib.use('Agg')
impo= rt matplotlib.pyplot as plt
fig=3Dplt.figure(figsize=3D(3,2))
plt.plot([1,3,2])
plt.savefig('myfig.png')
'myfig.png'
#+END_SRC

#+RESUL= TS:
[[file:]]

*Failed!* This time, it do= esn't work anymore. The file ~myfig.png~ is
=C2=A0properly ge= nerated, but the last string is not captured by
=C2=A0~matplotlib= ~. Following is the content of the corresponding
=C2=A0interactiv= e python session:

#+BEGIN_SRC
Python 3.4= .3 (default, Feb 27 2015, 00:22:26)
[GCC 4.2.1 Compatible Apple L= LVM 6.0 (clang-600.0.56)] on darwin
Type "help", "= copyright", "credits" or "license" for more inform= ation.
>>> >>> >>> , '''/v= ar/folders/kz/1c2cxn1x60n_t5p2j1p02b180000gn/T/py27084Z6S''', &= #39;exec'));
import matplotlib
import matplotlib
matplotlib.use('Agg')
>>> matplotlib.use= ('Agg')
import matplotlib.pyplot as plt
import = matplotlib.pyplot as plt
fig=3Dplt.figure(figsize=3D(3,2))
<= div>fig=3Dplt.figure(figsize=3D(3,2))
plt.plot([1,3,2])
plt.plot([1,3,2])
plt.savefig('myfig.png')
plt= .savefig('myfig.png')
'myfig.png'
'= myfig.png'


open('/var/folde= rs/kz/1c2cxn1x60n_t5p2j1p02b180000gn/T/babel-2708423D/python-27084mEZ',= 'w').write(str(_))
open('/var/folders/kz/1c2cxn1x60n= _t5p2j1p02b180000gn/T/babel-2708423D/python-27084mEZ', 'w').wri= te(str(_))




'org_babel_python_eoe'
'org_babel_python_eoe= 9;
>>> >>> >>> >>> [<matpl= otlib.lines.Line2D object at 0x1071d3860>]
>>> 'm= yfig.png'
>>> >>> 9
>>> = >>> >>> 'org_babel_python_eoe'
>>= >
#+END_SRC

* Some background

** Python Version
#+BEGIN_SRC python :session b= ackground :results output
import sys
sys.version
<= div>#+END_SRC

#+RESULTS:
: import sys
: >>> sys.version
: '3.4.3 (default, Feb 27 = 2015, 00:22:26) \n[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)]= 9;
: sys.executable
: '/Users/drorata/py-envs/gener= ic3.4/bin/python'
:
:

** S= ystem
I am running Emacs version ~GNU Emacs 24.4.51.1 (x86_64-app= le-darwin14.1.0, NS apple-appkit-1344.72) of 2015-02-02 on boxname.local~

The orgmode version is: ~Org-mode version 8.2.10 (8= .2.10-40-gc763fa-elpaplus @ /Users/username/.emacs.d/elpa/org-plus-contrib-= 20150504/)~

** Stackoverflow reference
I= also posted a question in the SE network:

* Qu= estion
So, my question is how to tackle/debug this issue? Can som= eone give me
a hand here? That would be really great! Thanks!

--
Dro= r Atariah, Ph.D.
--f46d0444027675915f0515d2d353--