Embedding Python script
When using the Python plugin, Python code can be embedded into RML files. Inline responses to events are executed as Python code. Functions, structures and variables can be declared or included with the <script> tag, then referenced from the inline code.
Inline event responses
The Python plugin installs an event listener instancer to execute inline event responses as Python code. For example, in the following sample the element will run the print command when it is clicked:
<button onclick="print 'Hello world!'" />
<button onclick="print 'Hello'; print 'world!'" />
Three global variables are accessible to inline event handlers. These are:
- event: The event currently being processed (ie, the event that triggered the handler).
- self: The element currently responding to the event.
- document: The owner document of the current element.
<button onclick="print self.tag_name" /> <button onclick="print event.mouse_x + ", " + event.mouse_y" />
Embedding Python into RML
<rml> <head> <script> def Test(): print 'Hello world!' </script> </head> <body> <button onclick="Test()">Continue</button> </body> </rml>
Python code declared inline in an RML file like this must begin at the beginning of the line like a normal Python file.
The following sample imports the test.py file instead of declaring the script inline (it is assumed the Python file declares a Test() function).
<rml> <head> <script src="test.py"> </head> <body> <button onclick="Test()">Continue</button> </body> </rml>
A document can include multiple <script> tags.