[PyQt] python bindings around webkit

Luke Kenneth Casson Leighton lkcl at lkcl.net
Wed Oct 15 13:23:45 BST 2008


> Hi,

 helloo :)

> This sounds very interesting. I'm looking at this from a user
> perspective. Will this allow me to access all and any object in a HTML
> page loaded by Webkit?

 yyyyep :)  including (once it's implemented) the SVG canvas (ooo).
 you _can_ access the SVG canvas at the moment, but you'd have
 to do it by executing javascript code-fragments.

 which is also possible - i.e. handing javascript to webkit, from a
python app, and getting webkit to execute it.  it makes for a _really_
weird combination :)  you can have an app where you've obtained the
body element using python, then you run a javascript code-fragment to
add a text node to body, and then print the innerHTML of the body -
back in python-land!

 veery bizarre :)


>  So will I be able to fully examine and modify
> the HTML as if I was using JavaScript?

 yyyep :)   [ and execute javascript as well (as text.  so you can do
%d and %s substitutions, etc., load javascript from local filesystem -
anything) ]

 1,500 or so of the DOM-model-manipulation functions that are
"accessible" to javascript, along with all of the attributes, have
_direct_ corresponding function names in webkit-glib, from there to
python, and thus to your app.

sadly, in a fit of... glib weakness, i lower-cased and underscored all
the function names and attribute names, but i realise now that that
was a _dreadful_ mistake that needs to be rectified.  easily done.
should take about 20 mins (and another 5 hours to rework
pyjamas-desktop which is why i haven't done it :) ... yet )

> I'm especially interested if I
> would be able to get access to the current cursor position in edit
> mode - that would allow me to use WebKit as the basis to write a very
> powerful WYSIWYG text editor.

well... you can definitely get access to the cursor position, in a
textarea, for example, just like you can with javascript.  basically,
_anything_ that can be done with javascript, you can also do with the
webkit DOM bindings.

people have written WYSIWYG editors in javascript (fsckeditor.js for
example), and there's a "rich text" widget in GWT 1.5 - don't know if
that's purely a wrapper around fsckeditor.js or similar or whether
it's been written in java and compiled to javascript with the GWT
compiler, but it'd be easy to find out.  so - that should reassure you
that it's technically possible.

but, if you wanted to do a "proper" WYSIWYG editor, then you'd be
almost undoubtedly implementing your own cursor, with a Timer to make
it flash on/off; you'd be capturing all keyboard events yourself -
etc. etc. - again, all of that is technically possible.

if you do decide to implement it, can i ask you a small favour?  could
you possibly implement it according to the "pyjamas" API (which you
will find more convenient anyway) so implement it as a pyjamas Widget,
and could you try to test its development simultaneously using
pyjamas-desktop - http://pyjd.org - as _well_ as under pyjamas -
http://pyjs.org ?

the reason i ask is that pyjs and pyjd have identical APIs, and are
designed so that an app that conforms to the pyjamas widget set API
will run _unmodified_ on either of the desktop port(s) or the web-app
port.

also, if you use the pyjamas API, then when python-qwebkit with DOM
bindings is available, pyjamas-qwebkit will be an almost trivial
exercise to write (under 24 hours), and your app will be
up-and-running _immediately_ under QWebKit, with zero modifications to
your code.

btw - slightly off-topic: there _do_ exist other DOM bindings
libraries (pyxpcomext and khtmlpart, part of python-kde) but i haven't
investigated pyxpcomext's suitability yet (well, actually PyDOM
running under XULrunner) and i _have_ investigated python-kde's
khtml.DOM, and it has that design flaw i mentioned, which needs to be
fixed.

l.


More information about the PyQt mailing list