IntroductionInstallationLimitations of the "file" protocolFrequently asked questionsSyntax, keywords and built-in functionsStandard distributionimport implementationBrython packagesBrowser interface
Introduction - DOM API
Creating a document Accessing elements Attributes, properties and methods Events Mouse events Keyboard events Focus events Drag events Query string Interactions with Javascript Brython-specific built-in modules
browser
browser.aio browser.ajax browser.html browser.local_storage browser.markdown browser.object_storage browser.session_storage browser.svg browser.template browser.timer browser.webcomponent browser.websocket browser.worker Widgets browser.widgets.dialog browser.widgets.menu interpreter javascript Working with BrythonCookbook |
Quote from the W3C Document Object Model specification : What is the Document Object Model? The Document Object Model is a platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents.Brython's goal is to replace Javascript with Python, as the scripting language for web browsers. A simple example :
<script> tag :
<html> <head> <script src="/src/brython.js"></script> <script src="/src/brython_stdlib.js"></script> </head> <body> <script type="text/python" src="test.py"></script> <input id="zone" autocomplete="off"> <button id="mybutton">click!</button> </body> </html>Please note that in this case the Python script will be loaded through an Ajax call : it must be in the same domain as the HTML page. The script extension is usually .py . In some cases, servers interpret
Ajax calls to this extension as a request to execute the script in the server.
In this case you have to change the extension, for instance replace it by
.bry as in the following example:
<script type="text/python" src="test.bry"></script>In the above two examples of code, when we click on the button, the click event calls and runs the function click() defined in the Python
script.
This function gets the value of the INPUT element, through its id
(zone). This is accomplished by the syntax document["zone"] : document ,
defined in module browser, is an object that represents the document
currently displayed in the browser. It behaves like a dictionary whose keys
are the ids of the elements of the DOM. Hence, in our example,
document["zone"] is an object that maps to the INPUT element ; the value
property holds, interestingly enough, the value of the object.
In Brython, the output can be accomplished in various ways, including with the
function alert() (also defined in browser) which shows a popup window
with the text passed as an argument.
In this example, we use a module from the Brython standard distribution,
browser.widgets.dialog, with a class InfoDialog that displays
a popup window.
|