When I use IPython along with the -wthread option, it spawns a python subprocess, which appears as a Mac OS X application.
My problem is that when I send commands to that application (for example plotting with matplotlib), the window is updated behind all my other windows. I would like to be able to call a python command to switch this python window to the front (I do that manually with ⌘-tab, but I have to find the python application first, and there might be several ones).
Is there a python script to detect which application IPython has spawned, and how to then automatically switch to it in OS X?
(I’m stating the problem in OS X, but the issue should be similar on other systems).
Edit: let me break this down in two problems:
- how to know which Mac OS X application python is running in? (probably possible with some IPython witchery)
- how to tell Mac OS X to put the focus on that application? (maybe using applescript)
Here is my full solution, with an IPython magic function.
Install appscript (see this question about switching apps programmatically in OS X), and put the following code in a script called
activate.pyin your~/.ipythonfolder.Now, edit your
~/.ipython/ipy_user_conf.pyconfiguration file and define the magic function:Now you just have to register this magic IPython function by putting the following somewhere in that same configuration file:
Now, after you run
IPython -wthread, you can call%wxactivateand you will switch to the corresponding Python application!(note that the reason why one has to run the call to appscript’s
activate()in another process in not clear to me; it may have to do with some threading problem… any explanation would be appreciatated)