emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Dror Atariah <drorata@gmail.com>
To: "emacs-orgmode@gnu.org" <emacs-orgmode@gnu.org>
Subject: Capturing file name from python session when using matplotlib
Date: Mon, 11 May 2015 20:48:38 +0200	[thread overview]
Message-ID: <CANfRcg3t+i0-AW2vw4nONKU+5RKR4LePxjnc2UY8b5Ef0_Dttg@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 3353 bytes --]

* 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'
>>> >>> >>> >>> [<matplotlib.lines.Line2D object at 0x1071d3860>]
>>> '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

[-- Attachment #2: Type: text/html, Size: 5280 bytes --]

                 reply	other threads:[~2015-05-11 18:48 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=CANfRcg3t+i0-AW2vw4nONKU+5RKR4LePxjnc2UY8b5Ef0_Dttg@mail.gmail.com \
    --to=drorata@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).