emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: William Henney <whenney@gmail.com>
To: Florian Lindner <mailinglists@xgm.de>
Cc: emacs-org <emacs-orgmode@gnu.org>
Subject: Re: Problem with python session
Date: Thu, 6 Oct 2016 12:54:49 -0500	[thread overview]
Message-ID: <CAKchnZO9k0APgsB6wEHaEfj0DPCcJX54=Vc=HbejcTPuMwUhMA@mail.gmail.com> (raw)
In-Reply-To: <nt5gos$14m$1@blaine.gmane.org>

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

Hi Florian

I can reproduce your problem.  This is (arguably) a bug in ob-python when
using the vanilla python interpreter together with the :session argument.
You can work around it by putting a blank line after the for-loop in your
second code block.

I say that it is arguable that this is a bug or not since you would have
exactly the same error if you were to literally type your code block in at
the python interactive prompt.  That is, you have to give a second newline
in order to close the loop and return to the top-level prompt.  However, it
is admittedly confusing to have different behavior with and without the
":session" argument.

I had never come across this bug myself, since I use ob-ipython for
interactive python sessions (https://github.com/gregsexton/ob-ipython)

Here is a minimal example that shows the problem.

Cheers

Will

* Test of ob-python in session mode with vanilla python interpreter

** FAILS: Without blank line after indented loop
#+BEGIN_SRC python :session *ob-python session*
for x in 1, 2:
    pass
x
#+END_SRC

#+RESULTS:

An error message appears in the =*ob-python session*= buffer, which can be
visited via =C-c C-v C-z= with point inside the code block.

#+BEGIN_EXAMPLE
>>> 'org_babel_python_eoe'
>>> 'org_babel_python_eoe'
>>> for x in 1, 2:
...     pass
... x
  File "<stdin>", line 3
    x
    ^
SyntaxError: invalid syntax
#+END_EXAMPLE

** SUCCEEDS: With blank line after indented loop
#+BEGIN_SRC python :session *ob-python session*
for x in 1, 2:
    pass

x
#+END_SRC

#+RESULTS:
: 2

** SUCCEEDS: Without using a session
#+BEGIN_SRC python :return x
for x in 1, 2:
    pass
x
#+END_SRC

#+RESULTS:
: 2

** SUCCEEDS: Using ob-ipython instead of ob-python
#+BEGIN_SRC ipython :session
for x in 1, 2:
    pass
x
#+END_SRC

#+RESULTS:
: 2


On Thu, Oct 6, 2016 at 7:41 AM, Florian Lindner <mailinglists@xgm.de> wrote:

> Hello,
>
> I have an org file:
>
> * Overview of available basis functions
> #+BEGIN_SRC python :session generateBFpics :exports results :results file
>   import matplotlib.pyplot as plt
>   import numpy as np
>
>   def set_plotoptions():
>       plt.xlabel("x")
>       plt.ylabel("$\phi(x)$")
>       plt.grid()
>
>
>   np.seterr(invalid='ignore')
>
>   x = np.linspace(-3, 3, 1000)
>
>   plt.plot(x,  np.log(abs(x))*np.power(x, 2))
>   plt.suptitle("Thin Plate Splines")
>   plt.title("$\phi(|x|) = \log(x) \cdot x^2$")
>   set_plotoptions()
>   plt.savefig("bf-tps.pdf")
>   plt.close()
>   "bf-tps.pdf"
> #+END_SRC
>
> #+RESULTS:
> [[file:bf-tps.pdf]]
>
> #+BEGIN_SRC python :session generateBFpics :exports results :results file
>   for shape in [1, 2, 3, 4]:
>       plt.plot(x, np.power(shape, 2) + np.power(x,2), label = "s = %i" %
> shape)
>   plt.suptitle("Multi Quadrics")
>   plt.title("$\phi(|x|) = s^2 + x^2$")
>   plt.legend()
>   set_plotoptions()
>   plt.savefig("bf-multiquadrics.pdf")
>   plt.close()
>   "bf-multiquadrics.pdf"
> #+END_SRC
>
> #+RESULTS:
> [[file:bf-multiquadrics.pdf]]
>
>
>
> Both PDFs are generated. But only the first one has the content I expect,
> the othe one is an empty plot (it's a plot,
> yes, but empty axes.
>
> When I copy these pieces of code into on .py file it works just great. To
> my understanding that just how session mode works.
>
> What could be the problem here?
>
> Thanks,
> Florian
>
>
>


-- 

  Dr William Henney, Instituto de Radioastronomía y Astrofísica,
  Universidad Nacional Autónoma de México, Campus Morelia

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

  reply	other threads:[~2016-10-06 17:54 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-06 12:41 Problem with python session Florian Lindner
2016-10-06 17:54 ` William Henney [this message]
2016-10-09  9:30   ` Nicolas Goaziou
2016-10-10 18:40     ` John Kitchin
2016-10-11 15:53       ` Nicolas Goaziou
2016-10-13  4:10         ` William Henney

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='CAKchnZO9k0APgsB6wEHaEfj0DPCcJX54=Vc=HbejcTPuMwUhMA@mail.gmail.com' \
    --to=whenney@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=mailinglists@xgm.de \
    /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).