Using wxPython in a non-blocking manner (for matplotlib & PTVS)

Hi Everyone,

I've spent a significant amount of time hacking away at code trying to
figure this out. After hitting wall after wall, I decided I should try
asking for a bit of help.

The end goal that I'm trying to achieve is to get matplotlib's wx
backend to work (interactively) with the Python Tools for Visual
Studio (PTVS) IDE.
Currently the PTVS only supports matplotlib for Python builds with
integrated matplot libary, so IPython and EPD I believe.

(I found that other IDE's usually have some sort of hack to allow
this, for example PyScripter requires you to run in a special mode,
"Remote Wx", or in IDLE you have to launch with the -n argument.)

Long story short, I would love to figure out how to get this going in
the PTVS IDE. (I'm hoping I can write some Python code for this,
possibly a new matplotlib backend, and not have to hack away at the
PTVS source code.)

The problem it seems is that wxPython (and other GUI toolkits) require
a MainLoop, which by default runs in a blocking manner, keeping you
from having the interactive plots along with the interactive
interpreter (REPL).

I came across an example (http://wiki.wxpython.org/MainLoopAsThread),
which allows me to open one GUI window that runs asynchronously, and
keeps the REPL running. Now that I have the MainLoop running in a
separate thread, I'm not sure how to go about creating and showing
more frames. Any new frame will open, but then becomes unresponsive.

Here is my example code, I'm running it in PTVS, but I found the
behavior is the same if I use PyScripter in "Remote mode" (not the
"Remote Wx" mode).

http://pastebin.com/q1wH9PA4

I've been digging through the PyScripter source code and it seem that
their hack, has something to do with creating a dummy MainLoop after
the initial one is run, but I can't quite figure out how it's done. (I
don't know, or have a Delphi IDE J).

Maybe I need to turn the first thread I created into a frame
generating factory.?

Any little pointers would be greatly appreciated.

Thanks!
Chris

The end goal that I'm trying to achieve is to get matplotlib's wx
backend to work (interactively) with the Python Tools for Visual
Studio (PTVS) IDE.

this is a trick, for just the reasons you mention.

Currently the PTVS only supports matplotlib for Python builds with
integrated matplot libary, so IPython and EPD I believe.

I don't think EPD provides anything special in this regard, except
maybe iPython out of the box.

iPython is not a python "build", it is an implementation of the
interactive interpreter, and it is wonderful, but it used the standard
Python. The new iPython, using 0mq is really nice, and solves this
issue well.

What I'm getting at is that to do what you want, you'll need to
implement something similar to what iPython has done -- so why not
just use iPython? I understand that MS has integrated with with PTVS
-- so your job is done for you.

-Chris

···

On Tue, Mar 27, 2012 at 7:11 PM, Chris Zeh <chris.w.zeh@gmail.com> wrote:

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Hi Chris,

Thanks for the helpful reply.

Up until just now I was under the impression that IPython was the same
thing as IronPython. Previously I had tried IronPython and learned it
was incompatible with some of the packages I use and tried to avoid it
henceforth. After reading your email and doing more research, I
learned IPython is very much not IronPython. (Tricky!)

As I understand it now, CPython and IronPython are just different
implementations of the Python specification. While IPython is an
interactive interpreter built to run with a Python implementation
(correct me if I'm wrong).

So, I'll start playing around with IPython instead of trying to hack
away at this workaround.

Thanks for the help!
Chris

···

On Mar 28, 9:58 am, Chris Barker <chris.bar...@noaa.gov> wrote:

On Tue, Mar 27, 2012 at 7:11 PM, Chris Zeh <chris.w....@gmail.com> wrote:
> The end goal that I'm trying to achieve is to get matplotlib's wx
> backend to work (interactively) with the Python Tools for Visual
> Studio (PTVS) IDE.

this is a trick, for just the reasons you mention.

> Currently the PTVS only supports matplotlib for Python builds with
> integrated matplot libary, so IPython and EPD I believe.

I don't think EPD provides anything special in this regard, except
maybe iPython out of the box.

iPython is not a python "build", it is an implementation of the
interactive interpreter, and it is wonderful, but it used the standard
Python. The new iPython, using 0mq is really nice, and solves this
issue well.

What I'm getting at is that to do what you want, you'll need to
implement something similar to what iPython has done -- so why not
just use iPython? I understand that MS has integrated with with PTVS
-- so your job is done for you.

-Chris

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Bar...@noaa.gov

Hi Chris,

Thanks for the helpful reply.

Up until just now I was under the impression that IPython was the same
thing as IronPython. Previously I had tried IronPython and learned it
was incompatible with some of the packages I use and tried to avoid it
henceforth. After reading your email and doing more research, I
learned IPython is very much not IronPython. (Tricky!)

As I understand it now, CPython and IronPython are just different
implementations of the Python specification. While IPython is an
interactive interpreter built to run with a Python implementation
(correct me if I'm wrong).

So, I'll start playing around with IPython instead of trying to hack
away at this workaround.

Just in case you need it later I'll go ahead and address your previous question.

I came across an example (MainLoopAsThread - wxPyWiki),
which allows me to open one GUI window that runs asynchronously, and
keeps the REPL running. Now that I have the MainLoop running in a
separate thread, I'm not sure how to go about creating and showing
more frames. Any new frame will open, but then becomes unresponsive.

The easiest way to deal with the UI from another thread is to use wx.CallAfter. It's primary purpose is to delay the call of some callable object until after the currently pending events have been delivered. Since it works by packaging the callable and args into an event which it posts to the application's pending event queue then it inherently also becomes a good way to cause something to happen in the UI thread from another thread because adding to that queue is thread-safe and the processing of that event will always happen in the UI thread.

···

On 3/28/12 9:28 AM, Chris Zeh wrote:

--
Robin Dunn
Software Craftsman

On Wed, Mar 28, 2012 at 9:28 AM, Chris Zeh

Up until just now I was under the impression that IPython was the same
thing as IronPython.

I see -- yes, very different!

As I understand it now, CPython and IronPython are just
different
implementations of the Python specification.

correct.

While IPython is an
interactive interpreter built to run with a Python
implementation

yes, but specifically with the "CPython" implementation, I'm pretty
sure. CPython is a name for the python implementation
developed/maintained by python.org (the original). Called thus because
the interpreter itself is written in C.

So, I'll start playing around with IPython instead of trying to hack
away at this workaround.

Yup -- i imagine that's what you want -- iPython is really nice, and I
learned at Pycon that folks have integrated it with the MS Visual
Studio tools -- I hope it works for you.

-Chris

···

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov