prompt-toolkit / ptpython

A better Python REPL
BSD 3-Clause "New" or "Revised" License
5.23k stars 281 forks source link

matplotlib integration seems a bit broken with ptipython #20

Open ianozsvald opened 9 years ago

ianozsvald commented 9 years ago

It feels like the matplotlib integration (which runs in a separate event loop) is a bit broken by ptipython, this will probably be a problem for anyone using matplotlib.

I'm using Linux Mint 17.1 (Ubuntu 14.04), Python 3.4.2 (Anaconda), IPython 2.2.0, today's <master> for ptipython.

If I run this in either ipython or ptipython, the plt.show() correctly causes a graph window to be drawn:

In [1]: import matplotlib.pyplot as plt
In [2]: plt.plot([1,2,3])
Out[2]: [<matplotlib.lines.Line2D at 0x7f04cd208278>]
In [3]: plt.show()  <-- graph window gets drawn

If the above (lines 2 & 3) are repeated in either ipython or ptipython, the graph will show again (so it feels like matplotlib integration is working in ptipython).

Note that in this mode (without using %matplotlib or passing in --pylab) the .show() is blocking.

If I run a vanilla ipython (i.e. $ ipython, not $ ipython --pylab), the %matplotlib magic works and the event-loop is non-blocking. This is the usual interactive development process if you're working with charts:

In [1]: %matplotlib
Using matplotlib backend: Qt4Agg

In [2]: import matplotlib.pyplot as plt

In [3]: plt.plot([1,2,3])
Out[3]: [<matplotlib.lines.Line2D at 0x7f2adff4bef0>]  
<-- graph windows pops up, no need to call plt.show(), interactive prompt is still interactive

If I run the same using $ ptipython, I don't get a graph window. If I add plt.show() then I also don't get a graph window. It looks like the matplotlib integration is a bit broken.

Some (possibly unhelpful) notes:

I'll also note that autocompletion on e.g. plt.plot( gets slow, this is a Jedi problem (and Rope too) with matplotlib, scipy etc and has been noted in e.g. the GVim forums. Somebody is bound to mention this soon so you ought to be aware of it.

jonathanslenders commented 9 years ago

Thanks @ianozsvald for the extensive bug report!

This is a known issue: (See: https://github.com/jonathanslenders/python-prompt-toolkit/issues/63 ) I consider it serious, but it's hard to solve at the moment. prompt-toolkit has its own event loop, and IPython has an event loop. (Actually, I believe IPython patches stdin.) I have to find a way to integrate these two event loops.

ianozsvald commented 9 years ago

Ah, it looks like a tricky one! Good luck with it

juanshishido commented 9 years ago

Thanks, @jonathanslenders!

Changed the backend to TkAgg in matplotlibrc and %matplotlib in ptipython works with no problems. More info on how to do that here.

Close this based on https://github.com/jonathanslenders/python-prompt-toolkit/issues/63?