Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

¿Cuanto browser entra en 128 líneas de código?

Lo que me gus­ta­ba de ese bro­w­ser de 42 lí­neas era que no era el ejem­plo tí­pi­co, don­de me­ten una vis­ta de We­bkit en una ven­ta­na, car­gan una pá­gi­na y te tra­tan de con­ven­cer de que son unos ba­na­na­s. Esa ver­sión son 7 lí­nea­s:

import sys
from PyQt4 import QtGui,QtCore,QtWebKit
app=QtGui.QApplication(sys.argv)
wb=QtWebKit.QWebView()
wb.setUrl(QtCore.QUrl('http://www.python.org'))
wb.show()
sys.exit(app.exec_())

O 6 si lo so­por­ta­ra un po­co más feo.

¡Pe­ro igua­l, el de 42 se veía úti­l!

This 42-line web browser, courtesy of #python and #qt -- http... on Twitpic

Esos bo­to­nes que se ven fun­cio­na­ban co­rrec­ta­men­te, ha­bi­li­tan­do y des­ha­bi­li­tan­do­se en el mo­men­to co­rrec­to, la en­tra­da de URL cam­bia­ba cuan­do ha­cías cli­ck en un li­nk, y otras co­si­tas así.

Ahí de­ci­dí em­pe­zar un pe­que­ño pro­yec­to in­ter­mi­ten­te de co­de gol­f: me­ter el me­jor bro­w­ser que pue­da en 128 lí­neas de có­di­go (sin con­tar co­men­ta­rios ni blan­co­s), usan­do so­lo Py­Q­t4.

Eso tie­ne un pro­pó­si­to úti­l: siem­pre sos­pe­ché que si uno asu­me Py­Qt co­mo par­te del sis­te­ma ba­se, la ma­yo­ría de las apli­ca­cio­nes en­tra­rían en diske­ttes. Es­ta en­tra unas 500 ve­ces en uno de 1.44MB (¡a­sí que po­dés usar los de 360 de co­m­mo­do­re sin du­pli­disk!)

has­ta aho­ra van 50 lí­nea­s, y tie­ne los si­guien­tes fea­tu­res:

  • Zoom in (C­­tr­­l++)

  • Zoom out (C­­tr­­l+-)

  • Re­set Zoom (C­­tr­­l+=)

  • Bus­­car (C­­tr­­l+­­F)

  • Es­­co­n­­der bús­­que­­da (Es­­c)

  • Bo­­­to­­­nes de atrá­s/a­­de­­lan­­te y re­­ca­r­­gar

  • En­­tra­­da de URL que coi­n­­ci­­de con la pá­­gi­­na + au­­to­­­co­m­­ple­­ta­­do des­­de la his­­to­­­ria + arre­­gla la URL pues­­ta a ma­no (a­­gre­­ga http://, esas co­­sas)

  • Plu­­gins (i­n­­cluí­­do fla­s­h, que hay que ba­­jar apa­r­­te ;-)

  • El tí­­tu­­lo de la ven­­ta­­na mues­­tra el tí­­tu­­lo de la pá­­gi­­na (sin pro­­­pa­­gan­­da del bro­­w­se­­r)

  • Ba­­rra de pro­­­gre­­so pa­­ra la ca­r­­ga de la pá­­gi­­na

  • Ba­­rra de es­­ta­­do que mues­­tra el des­­tino de los li­nks cuan­­do pa­sas el mou­­se

  • To­­­ma una URL en la lí­­nea de co­­­man­­do (o abre http://­­p­­y­­tho­­n.org

  • Mu­l­­ti­­pla­­ta­­fo­r­­ma (fun­­cio­­­na do­n­­de fun­­cio­­­na QtWe­­bKi­­t)

Fal­tan ta­bs y so­por­te de pro­x­y. Es­pe­ro que lle­ven unas 40 lí­neas má­s, pe­ro creo que ya es el más ca­paz de to­dos es­tos bro­w­sers de ejem­plo.

El có­di­go­... no es tan te­rri­ble. Uso mu­chos lamb­da­s, y los ar­gu­men­tos ke­yword de Py­Qt pa­ra co­nec­tar se­ña­le­s, que ha­cen que al­gu­nas lí­neas sean muy lar­ga­s, pe­ro no muy di­fí­ci­le­s. Se po­dría achi­car bas­tan­te to­da­vía!

Aquí es­tá en ac­ció­n:

Y aquí es­tá el có­di­go:

#!/usr/bin/env python
"A web browser that will never exceed 128 lines of code. (not counting blanks)"

import sys
from PyQt4 import QtGui,QtCore,QtWebKit

class MainWindow(QtGui.QMainWindow):
    def __init__(self, url):
        QtGui.QMainWindow.__init__(self)
        self.sb=self.statusBar()

        self.pbar = QtGui.QProgressBar()
        self.pbar.setMaximumWidth(120)
        self.wb=QtWebKit.QWebView(loadProgress = self.pbar.setValue, loadFinished = self.pbar.hide, loadStarted = self.pbar.show, titleChanged = self.setWindowTitle)
        self.setCentralWidget(self.wb)

        self.tb=self.addToolBar("Main Toolbar")
        for a in (QtWebKit.QWebPage.Back, QtWebKit.QWebPage.Forward, QtWebKit.QWebPage.Reload):
            self.tb.addAction(self.wb.pageAction(a))

        self.url = QtGui.QLineEdit(returnPressed = lambda:self.wb.setUrl(QtCore.QUrl.fromUserInput(self.url.text())))
        self.tb.addWidget(self.url)

        self.wb.urlChanged.connect(lambda u: self.url.setText(u.toString()))
        self.wb.urlChanged.connect(lambda: self.url.setCompleter(QtGui.QCompleter(QtCore.QStringList([QtCore.QString(i.url().toString()) for i in self.wb.history().items()]), caseSensitivity = QtCore.Qt.CaseInsensitive)))

        self.wb.statusBarMessage.connect(self.sb.showMessage)
        self.wb.page().linkHovered.connect(lambda l: self.sb.showMessage(l, 3000))

        self.search = QtGui.QLineEdit(returnPressed = lambda: self.wb.findText(self.search.text()))
        self.search.hide()
        self.showSearch = QtGui.QShortcut("Ctrl+F", self, activated = lambda: (self.search.show() , self.search.setFocus()))
        self.hideSearch = QtGui.QShortcut("Esc", self, activated = lambda: (self.search.hide(), self.wb.setFocus()))

        self.quit = QtGui.QShortcut("Ctrl+Q", self, activated = self.close)
        self.zoomIn = QtGui.QShortcut("Ctrl++", self, activated = lambda: self.wb.setZoomFactor(self.wb.zoomFactor()+.2))
        self.zoomOut = QtGui.QShortcut("Ctrl+-", self, activated = lambda: self.wb.setZoomFactor(self.wb.zoomFactor()-.2))
        self.zoomOne = QtGui.QShortcut("Ctrl+=", self, activated = lambda: self.wb.setZoomFactor(1))
        self.wb.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled, True)

        self.sb.addPermanentWidget(self.search)
        self.sb.addPermanentWidget(self.pbar)
        self.wb.load(url)


if __name__ == "__main__":
    app=QtGui.QApplication(sys.argv)
    if len(sys.argv) > 1:
        url = QtCore.QUrl.fromUserInput(sys.argv[1])
    else:
        url = QtCore.QUrl('http://www.python.org')
    wb=MainWindow(url)
    wb.show()
    sys.exit(app.exec_())
Roland Lovelock / 2011-03-04 08:09:

You sir, are a scholar and a gentleman.

Rafe Kettler / 2011-03-04 08:26:

Protip: use Github gists if you want any sense of permanence in your pastebin.

pete / 2011-03-04 13:44:

Fantastic, cool example. On a side note, do you have any tips for debubbing Javascript with WebKit

Roberto Alsina / 2011-03-06 20:38:

Thanks, no I don't :-(
At least not beyond showing the inspector and working from there?

Mason Larobina / 2011-03-04 17:07:

Here is my attempt using the luakit browser framework: https://gist.github.com/855103 I've gone with a modal approach and have managed to squeeze a few more key bindings in.

Roberto Alsina / 2011-03-06 20:38:

That's very interesting!

Ralph / 2011-03-04 17:11:

I tried this out, and it actually works just fine. Only thing is the browsing experience is a bit slow. At first I thought it was a Python thing...but since PyQt wraps the native Qt framework which is written in C or C++ (not sure) shouldn't the browsing experience be somewhat fast? I realize that it's not going to probably be as fast as the mainstream browsers which have a ton of optimizations but just wondering if you could shed some light.

Roberto Alsina / 2011-03-06 20:38:

In my computer I don't see it as significantly slower than other browsers, but I do have a fast-ish computer.
I believe (but this is really just a vague memory) that the Javascript engine in Qt's webkit is not up to par with other implementations, maybe that is ;art of the problem.

Another thing is that there is no persistent disk cache, so it should feel fairly slower than other browsers for sites you visit frequently.

Patx44 / 2011-07-19 21:00:

http://patx.me/fastpatx  << fastPATX is a web browser I wrote. Its very good. Not quite as small as yours... but it does and looks a tad better.

Vinay Sajip / 2012-01-23 19:45:

This is a very nice minimal browser. I'd like to use it in a Sphinx documentation watcher (which is just a small bit of extra code on top of your work) - as I don't see a license on your code, is it OK for me to use it in this way? I'm planning to release the result under the MIT license. Thanks!

Roberto Alsina / 2012-01-23 20:00:

That's just fine for me! The code is under a MIT license. Ping me if you need help with something :-)

Vinay Sajip / 2012-01-24 22:07:

Thanks, Roberto. I've posted about my application of your work to easing the development flow when working on Sphinx documentation: http://pymolurus.blogspot.c...

Roberto Alsina / 2012-01-23 20:03:

BTW: latest code is at http://devicenzo.googlecode... (since I don't seem to have linked it on this post)


Contents © 2000-2024 Roberto Alsina