"Hook up the HTML to Python"
Something Happens
click
hover
focus
keyboard
change
submit
GamepadInput
...
Loadprogress
Animation
Local Storage access
Custom events
...
In HTMLIn Python
Plus a sneak peak...
HTML Attribute is py-[event]
Value is the string of executable code
def say_hi():
print("Hi there!")
def say_hi():
print("Hi there!")
script type="text/template">
Something Happens
Common Event Attributes:
target
timeStamp
isTrusted
KeyboardEvent
Attributes
keycode
altKey
ctrlKey
metaKey
shiftKey
repeat
add_event_listener
add_event_listener(elt, event, listener)
elt
event
listener
Import from pyodide.ffi.wrappers
add_event_listener
https://tinyurl.com/addeventlistener
import js
from pyodide.ffi.wrappers import add_event_listener
def say_hi(event):
print(f"Triggered by {event}")
btn = js.document.getElementById("my-btn")
add_event_listener(btn, 'click', say_hi)
https://tinyurl.com/addeventlistener
import js
from pyodide.ffi.wrappers import add_event_listener
def say_hi(event):
print(f"Triggered by {event}")
btn = js.document.getElementById("my-btn")
add_event_listener(btn, 'click', say_hi)
https://tinyurl.com/pyconvalidate
import js
from pyodide.ffi.wrappers import add_event_listener
def validate(event):
if 'py' in event.target.value: event.target.style.color = 'green'
else: event.target.style.color = 'red'
my_input = js.document.getElementById("my-input")
add_event_listener(my_input, 'input', validate')'
remove_event_listener(elt, event, listener)
elt
event
listener
Import from pyodide.ffi.wrappers
def foo(event):
...
import js
div = js.document.getElementById("btn")
add_event_listener(div, 'focus', foo)
@when('my-div', 'focus')
def foo():
...
@when
decoratordef when(eventName:str, id: str)
Takes the name of an Event and an Element id
@when('click', 'hello-btn')
def say_hi():
print("Hi there!")