Re: [O] Problem with python session
Yep, I agree. This is a wart in the vanilla python REPL, and org-mode can't really do much about it. Except maybe warn people. As John noted, this is less of a problem if you use ipython as your REPL. I think it is enough to just do #+BEGIN_SRC emacs-lisp (setq org-babel-python-command "ipython") #+END_SRC but I haven't tested this extensively since, as I mentioned, I prefer to use Greg Sexton's ob-ipython instead. This integrates with the Jupyter kernel by sending JSON to a web socket, rather than just pasting the source block into a buffer, and that seems to be a more robust approach. Will On Tue, Oct 11, 2016 at 10:53 AM, Nicolas Goaziouwrote: > Hello, > > John Kitchin writes: > > > I am not sure it makes sense to change anything for this. > > Noted. Thank you for the feedback. > > Regards, > > -- > Nicolas Goaziou > > -- Dr William Henney, Instituto de Radioastronomía y Astrofísica, Universidad Nacional Autónoma de México, Campus Morelia
Re: [O] Problem with python session
Hello, John Kitchinwrites: > I am not sure it makes sense to change anything for this. Noted. Thank you for the feedback. Regards, -- Nicolas Goaziou
Re: [O] Problem with python session
I am not sure it makes sense to change anything for this. There is different behavior with scripts and the interpreter independently of org-mode, e.g. with python -i: >>> for i in range(3): ... print(i) ... i File "", line 3 i ^ SyntaxError: invalid syntax Vanilla python sessions are kind of maddening. These seemingly identical blocks are different, i.e. one works and one doesn't! Spoiler alert, they are not identical. #+BEGIN_SRC python :results output org drawer :session for i in range(3): for j in range(3): pass print(i) #+END_SRC #+RESULTS: :RESULTS: ... File "", line 3 ^ IndentationError: expected an indented block File "", line 1 pass ^ IndentationError: unexpected indent >>> File "", line 1 print(i) ^ IndentationError: unexpected indent :END: This block which works has one space at the beginning of the blank lines. #+BEGIN_SRC python :results output org drawer :session for i in range(3): for j in range(3): pass print(i) #+END_SRC #+RESULTS: :RESULTS: ... ... ... ... ... 0 1 2 :END: This kind of error would be hard to reliably fix IMHO since it would rely on replacing blank lines with at least a space, and adding a blank line after indentation changes, except they can not be empty, they need at least a space in them. It is not clear that is a good idea. Maybe a test that replaces "\n" with "\n \n" might clear it up, but might also add a bunch of the ... >>> characters in the output? This is not an issue with python scripts or ipython, however. Nicolas Goaziou writes: > Hello, > > William Henneywrites: > >> 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. > > Thank you for the analysis. Would you have a suggestion on how to > improve the situation? > > Regards, -- Professor John Kitchin Doherty Hall A207F Department of Chemical Engineering Carnegie Mellon University Pittsburgh, PA 15213 412-268-7803 @johnkitchin http://kitchingroup.cheme.cmu.edu
Re: [O] Problem with python session
Hello, William Henneywrites: > 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. Thank you for the analysis. Would you have a suggestion on how to improve the situation? Regards, -- Nicolas Goaziou
Re: [O] Problem with python session
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 "", 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 Lindnerwrote: > 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
[O] Problem with python session
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