Archive for May, 2008

Firefox 3 beta on ubuntu 8.04 crawlage & solution

May 23, 2008

(* fanfare_for_first_offtopic_post *)

Since this blog is not strictly restricted to IPython subject matter, here’s a good tip: firefox 3 beta on Ubuntu 8.04 LTS is quite slow, at least on nvidia cards. Especially scrolling around gmail can be a pain. A good solution is installing firefox 2 (sudo apt-get install firefox-2). Then, you can set firefox 2 as your default browser from System -> Preferences -> Preferred applications, select “Custom” web browser and enter “firefox-2 %s” as the command.

Problem solved. Otherwise, 8.04 LTS pretty much rocks on all departments, at least for desktop use.

To navigate back to on-topic realm, IPython 0.8.3 is pretty much ready; release candidate 1 has been built, and there should be no more bugfixes, provided that the installers (that have seen some changes) work correctly. Try them out at launchpad

Embedding IPython in GUI apps is trivial

May 15, 2008

Here is a little known secret: IPython can be trivially embedded to GUI apps with event loops. This is verified to work with Tk and Qt4, at least.

What you need to do is this:

def embed_ipython(w):
    from IPython.Shell import IPShellEmbed
    ipshell = IPShellEmbed(user_ns = dict(w = w))
    ipshell()

Here ‘w’ is a central object of some kind that you want to expose to IPython (to manipulate, test various methods, etc).

My GUI app initialization is like this:

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    embed_ipython(window)
    sys.exit(app.exec_())

What may be nonobvious here is that embed_ipython() call never returns. There is “secret sauce” in at least Qt4 and Tk that allows the GUI event loop to proceed while IPython read-eval-print loop is being handled (the same sauce that allows you to operate GUIs in standard interactive python prompt). One would intuitively guess that surely the REPL should be running in a separate thread, but this is not the case. While the UI event loop (or IPython) is doing something time-consuming, the other party will be blocked for that time – but there is the huge benefit that everything occurs in the same thread, that of the GUI event loop.

Greedy completer

May 14, 2008

Every now and again, we at IPython0 get complaints about the strict criteria that we use for tab completing python attributes; basically, we only tab complete expressions where side effects are not likely when calling ‘eval’, as in expression foo.bar.ba<TAB> (we eval foo.bar, then get attributes from the resulting object). An expression we do not eval is foo(12).bar.ba + TAB, because calling foo(12) may do something nasty that the innocent ipythoneer may not be aware of.

Until now, that is. Sometimes all you care about is convenience – maybe your interactive work is not so side-effect-sensitive, maybe you trust yourself enough to not press tab after a dangerous command, or maybe you just enjoy living on the edge. I just added ipy_greedycompleter.py IPython extension to the trunk, which does not really care about what it evaluates – pressing TAB is enough, and if something breaks, the user gets to keep both pieces.

Activate it by typing “import ipy_greedycompleter” (or “import ipy_gr” + TAB for completion savvy), or add it to you ipy_user_conf.py.

I should probably also mention that you can get IPython trunk (it’s probably stabler than 0.8.2 at this point) from launchpad by doing:

bzr branch lp:ipython

– Ville