

QVariant not int and when you convert to int it returns tuple. attribute() and pass reference to class property of request.
#Webkit browser code#
Note how awkward it is to get status code from response. I have to say that some things in Qt are not as easy and quick as they should be. HttpStatusCodeAttribute ) status, ok = status. toString () # getting status is bit of a pain rawHeaderPairs () headers = content_type = headers. table = table def _finished ( self, reply ): """Update table with headers, status code and url. _init_ ( self ) # add event listener on "load finished" event Webkit view to use this manager to perform its requests.įirst let’s create our network access manager: class Manager ( QNetworkAccessManager ): def _init_ ( self, table ): QNetworkAccessManager. Subclass NetworkAccessManager, add event listeners we need, and tell our To perform and monitor requests performed by application. Turns out that Qt exposes NetworkAccessManager class as an API allowing you To keep track of all requests we’ll need to get bit deeper into PyQt internals. setItem ( last_row, col, QTableWidgetItem ( dat )) setRowCount ( next_row ) for col, dat in enumerate ( data, 0 ): if not dat : continue self. rowCount () next_row = last_row + 1 self.
#Webkit browser update#
ResizeToContents ) def update ( self, data ): last_row = self.

class RequestsTable ( QTableWidget ): header = def _init_ ( self ): super ( RequestsTable, self ). Header will contain field names, it will auto-resize each time We will only log url, status code and content type of responses.ĭo do this we will need to create a table first, we’ll use QTableWidget for that, Requests will be shown in table below main browser frame, for simplicity We will simply keep track of all requests performed by browser engine while rendering Let’s add something similar to Chrome “network” tab in dev tools. Our Python browser should have some developer tools too. Every browser worth its name should have its developer console. Of course the most interesting and important part of every browser are itsĭev tools. Your app will load url into browser frame and render all HTML and JavaScript. Google Chrome and it uses same rendering engine. show () # close app when user closes windowĪt this point you have bare-bones browser that shows some resembrance to addWidget ( browser, 2, 0 ) # main app window addWidget ( url_input, 1, 0 ) # browser frame at row 2 column 0 of our grid Grid = QGridLayout () browser = QWebView () url_input = UrlInput ( browser ) # url_input at row 1 column 0 of our grid load ( url ) if _name_ = "_main_" : app = QApplication ( sys. text ()) # load url into browser frameīrowser. _return_pressed ) def _return_pressed ( self ): url = QUrl ( self. browser = browser # add event listener on "enter" pressed import sys from PyQt4.QtGui import QApplication from PyQt4.QtCore import QUrl from PyQt4.QtWebKit import QWebView from PyQt4.QtGui import QGridLayout, QLineEdit, QWidget class UrlInput ( QLineEdit ): def _init_ ( self, browser ): super ( UrlInput, self ). Since we will have two elements (text input and browser frame), we’ll Into text box, browser will load this url. To do this we’ll just add input box at the top of the window, user will type url Weĭefinitely need some way of passing urls to load to our browser. Is already better than python-requests or even Lynx because it renders JavaScript.īut it’s not much better than Lynx because you can only pass urls from command line when you invoke it. If you pass url to script from command line it should load this url and showĪt this point you maybe have something looking like command line browser, which This is trivial to do, and requires around 13 lines of code (with importsĪnd whitespace): import sys from PyQt4.QtWebKit import QWebView from PyQt4.QtGui import QApplication from PyQt4.QtCore import QUrl app = QApplication ( sys.

Opening window and rendering page in this window. Let’s start with simplest possible use case of PyQt Webkit: loading some url,
