Differences between revisions 26 and 27
Revision 26 as of 2009-10-12 17:46:09
Size: 8239
Editor: lkcl
Comment: link to pyv8
Revision 27 as of 2010-01-02 06:06:26
Size: 8241
Editor: c83-253-240-6
Comment: fixed broken link
Deletions are marked like this. Additions are marked like this.
Line 22: Line 22:
 These are tools that convert python into javascript, that can then be run either stand-alone, using technology such as spidermonkey, google's V8 engine (for example, using [[http://code.google.com/pyv8|pyv8]]), or in a web browser (where, in order to be useful, the applications must of course interface with the DOM model of the browser, typically using an AJAX library).  These are tools that convert python into javascript, that can then be run either stand-alone, using technology such as spidermonkey, google's V8 engine (for example, using [[http://code.google.com/p/pyv8|pyv8]]), or in a web browser (where, in order to be useful, the applications must of course interface with the DOM model of the browser, typically using an AJAX library).

TODO: merge in and research these, found on comp.lang.python

> Yes, Python can do it... on Windows. > Two (examples) ways: > - ActiveScripting (PythonScript), include in PyWin32 > - Gestalt (who mix Python, Ruby &Javascript, via Silverlight)

Web Browser Programming in Python

This topic covers ways in which python can be used in web browsers to control, create or manipulate the content within a user's web browser, or a web-based technology (such as webkit, the technology behind Safari, Midori, the OLPC Browser, Adobe AIR, Google Chrome and much more; XULrunner (the engine behind Firefox) and much more; MSHTML ( the engine behind IE and much more; and KDE's KHTMLPart).

To clarify what type of technology goes onto this page, some examples of types of technology that can and cannot be added to this section:

  • Specifically excluded from the list is technology that simply generates static HTML content. So, an HTML pretty-printer library, as the resultant HTML simply uses the browser for "display" purposes rather than using the browser as an "application execution environment", is out.
  • Plugins for Web Browsers that provide direct access to the DOM model of the web browser. In exactly the same way that most web browsers have javascript by default as a language that can directly access the DOM model of the web browser, a plugin or other system that can do the same thing (i.e with python: <script type="text/python" /> instead of javascript: <script language="javascript" />) should be listed on this page.

  • Python-based technology that auto-generates or compiles javascript definitely counts, as the resultant javascript would be executed by the web browser.
  • Technology that simply *uses* javascript or depends on javascript libraries (such as mochikit, extjs or prototype) does not count, but a python-based wrapper library around a javascript engine definitely counts.

This latter example requires some further explanation: Pyjamas, for example, is a python-to-javascript compiler that can include inline javascript. So, although the input to Pyjamas is part-python, part-javascript, the output is pure-javascript that runs in a web browser, and so any such "mixed" language libraries should be included on this page.

Python to Javascript Compilers

  • These are tools that convert python into javascript, that can then be run either stand-alone, using technology such as spidermonkey, google's V8 engine (for example, using pyv8), or in a web browser (where, in order to be useful, the applications must of course interface with the DOM model of the browser, typically using an AJAX library).

  • Pyjamas is a Python-to-Javascript compiler and AJAX-based Web Toolkit and Framework that allows users to write code in standard python that will be run in a web browser, after translation to javascript. pyjs - the compiler - can also be used as a stand-alone javascript compiler.

  • Skulpt is a Python-to-Javascript compiler that is focussing on providing full python syntax: http://www.skulpt.org/ The demo includes a python interpreter prompt, which is actually running in the user's web browser, not on a server, as pure javascript.

  • Py2Js is an unmaintained project that can still be downloaded here. It is purely a python-to-javascript compiler / translator.

  • PyPy is compiler technology that contains an alternative python interpreter front-end and also supports javascript as output on the back-end. Using Pypy/JS describes how to integrate with libraries such as mochikit. Note: when last checked (2009 april) the pypy team appear to have removed all documentation mentioning support for javascript as a back-end in pypy, and have removed all tutorials on the subject.

Embedding Python inside Web Browsers

  • This section describes projects where the python interpreter itself can and has been embedded into the web browser. Instead of downloading the standard python executable and libraries, these projects come with the python runtime and libraries pre-embedded (and, as such, are typically very large downloads).
  • PyXPComExt has the full Python interpreter embedded. PyXPCOMExt is the interpreter as a XULRunner extension; PyDOM is a library that will almost certainly be needed, that allows you to manipulate the browser's DOM model using python. The API is (almost) identical to that of javascript. Effectively, where browsers have built-in support for 'lang="javascript"', PyXPComExt adds <script lang="python"> .... </script> to Firefox.

  • AppCelerator's Titanium provides support for < script type="text/python"> ... </script>, using IronPython and Silverlight.

Python wrappers around Web "Libraries" and Browser Technology

  • This section describes projects where you can (or have to) create your own web browser application in python. It includes web browser "engines" that have python interfaces to access, control and present web pages and web-relevant rich media content (such as Adobe Flash).
  • PyWebKitGtk - PyWebkitGtk is a python wrapper around Webkit that embeds the Webkit "engine" as a GTK widget. The standard version of pywebkitgtk is unable to provide access to the DOM model, treating pywebkit as a hands-off widget that can be used to write your own Web Browser (see demobrowser.py). However, a patch to webkit and a corresponding patch to pywebkitgtk will soon bring DOM model manipulation to python: see PyjamasDesktop for details.

  • PyWebkitQt4 is a python wrapper again around Webkit but this time as a Qt4 widget. Bindings to the DOM model have not been added to PyWebkitQt4, and so you can only use PyWebkitQt4 to create your own web browser, rather than manipulate the DOM model content itself.

  • PyKDE - KDE contains python bindings to KHTMLPart (which is very similar to Webkit). This allows you to embed HTML into an application window. The Python bindings to the DOM model are slightly... obtuse. to say the least, and PyKHTML - PyKHTML makes them much more tolerable (see dom.py). However, there are limitations in PyKDE's DOM bindings (that many people will never encounter) that you should investigate thoroughly before utilising PyKDE for seriously heavy-duty DOM model manipulation.

  • Webkit with the ObjectiveC bindings (MacOSX users only). Webkit itself has ObjectiveC bindings, on MacOSX. MacOSX's ObjectiveC technology comes with automatic bindings to all major programming languages, including python (using pyobjc). Consequently, you can directly manipulate the DOM model from python. However, unlike the use of MSHTML, and unlike XULrunner and the patched version of webkit, the ObjectiveC webkit bindings are limited to JUST the DOM model, and are limited to strict accordance with the W3C standards (rather than the de-facto standards defined by real-world javascript usage). So, for example, XMLHttpRequest is not included in the ObjectiveC bindings (whereas it is in XULRunner); and the <embed> element takes width and height strictly as integers, rather than accepting "100px" and stripping off "px".

  • HulaHop provides python access to DOM model manipulation - via XUL / Gecko Interfaces. HulaHop is part of the OLPC Sugar Project, but is available stand-alone. It depends on python-xpcom (part of XULrunner).

  • python win32 comtypes can be used (with care!) to create an MSHTML IWebBrowser2 Active-X window. PyjamasDesktop uses this technique to create the mshtml.py port. Note that creation and use of XMLHttpRequest is also shown in PyjamasDesktop's mshtml.py

WebBrowserProgramming (last edited 2022-05-08 08:36:36 by Olivier Pirson)

Unable to edit the page? See the FrontPage for instructions.