Thursday, November 8, 2012

Write your own web browser in 9 lines flat

This web browser in 9 lines of code comes to you courtesy of the awesome python bindings to the webkit renderer on Mac OSX, WebKitCtrl. One simply defines a window and drops the webkit renderer in, done!

First, make sure you have installed wxPython

Then drop this code into a file called :
import sys
import wx
import wx.webkit       

theApp = wx.PySimpleApp(0)
theFrame = wx.Frame(None, -1, "", size=(640,480))
w = wx.webkit.WebKitCtrl(theFrame, -1)
You launch the browser from the command line, passing the URL to go to as argument, like so:
The last bit needed is the script python_32, which ensures python will run in 32 bit mode, because the 64 bit mode is broken unless you have installed the wxPython Cocoa libraries. Place these lines in the file 'python_32'
/usr/bin/python "$@"
and place that file in the same directory as ''. Now, also make sure that this script is executable
chmod a+x python_32
Nitpickers take note. One might say that this is no different from calling a browser from the command line directly, which, according to the line counting above, would give you a browser in 0 lines of code:
However, I would not call that 'your own browser' in the sense that you cannot wrap your own controls around it and interact with the browser contents as you can with the script above.