browsergui.events package¶
Module contents¶
Defines Events
that represent actions taken by the user.
You can attach Events
to Elements
to make sure certain functions get called when the user takes the corresponding kind of action:
>>> e = Element(tag_name='input')
>>> e.callbacks[Input] = (lambda event: print(event.value))
The predefined types of Event are:
Click (target_id, **kwargs) |
Fired when the user clicks on an element. |
Input (value, **kwargs) |
Fired when the user changes the value of an input-type element, in some sense. |
Change (value, **kwargs) |
Fired when the user changes the value of an input-type element, in some sense. |
-
class
browsergui.events.
Change
(value, **kwargs)[source]¶ Bases:
browsergui.events.Event
Fired when the user changes the value of an input-type element, in some sense.
To be honest, I don’t really know the difference between the “input” and “change” JavaScript events.
-
javascript_type_name
= 'change'¶
-
-
class
browsergui.events.
Click
(target_id, **kwargs)[source]¶ Bases:
browsergui.events.Event
Fired when the user clicks on an element.
-
javascript_type_name
= 'click'¶
-
-
class
browsergui.events.
Event
(target_id, **kwargs)[source]¶ Bases:
object
Represents an event triggered by the user interacting with an Element.
The Event life cycle:
- Every Event subclass has a
javascript_type_name
, which is the name of the corresponding kind of JS event. (The subclass should also useEvent.register_subclass
to ensure that it will be instantiated when notifications with that type-name are received; see below.) - An Event subclass can enable itself on a tag, so that when that tag handles an event of that type-name in the browser, the server is sent a dict. That dict describes the browser-side event, including the type-name.
- That type-name is looked up in a table to determine which Event subclass to instantiate. (Subclasses are added to the table via
Event.register_subclass
.) - The subclass is instantiated, using the dict as
**kwargs
.
Parameters: target_id (str) – the id
attribute of the HTML tag interacted with.-
classmethod
dict_to_notify_server
()[source]¶ The information the browser should send the server when an event occurs.
Returns a dict where the keys are strings, and the values are strings (JS expressions to be evaluated and stuck into the server notification).
Example: for the Click event, the JS to notify the server would be:
notify_server({target_id: this.getAttribute(“id”), type_name: “click”})so
dict_to_notify_server
should return{‘target_id’: ‘this.getAttribute(“id”)’, ‘type_name’: ‘“click”’}
-
classmethod
disable_server_notification
(tag)[source]¶ Remove server-notification JS for this event from the given tag.
-
classmethod
enable_server_notification
(tag)[source]¶ Attach JS to a tag to notify the server when this event happens targeting the given tag.
Parameters: tag (xml.dom.minidom.Element) –
-
static
from_dict
(event_dict)[source]¶ Parse a dict received from the browser into an Event instance.
-
static
register_subclass
()[source]¶ Decorator to add the class to the (JS-type-name -> class) table.
-
registered_subclasses_by_name
= {'change': <class 'browsergui.events.Change'>, 'click': <class 'browsergui.events.Click'>, 'input': <class 'browsergui.events.Input'>}¶
- Every Event subclass has a