Events

					
					
					
				

"Hook up the HTML to Python"

Something Happens

click

hover

focus

keyboard

change

submit

GamepadInput

...

Loadprogress

Animation

Local Storage access

Custom events

...

Run Code


				

2 Ways To React to Events in PyScript


In HTMLIn Python

Plus a sneak peak...

Event Handling in HTML

Event Executor

Event Executor

HTML Attribute is py-[event]

Value is the string of executable code


						
							def say_hi():
								print("Hi there!")
						
					

						
					

Event Executor


						
							def say_hi():
								print("Hi there!")
						
					

						
					

						

Event Executor


script type="text/template">

				

Event Handling in Python

Event Handling in Python

Something Happens

Event Object
Run Code

Event Objects

Event Types

AnimationEvent ClipboardEvent CustomEvent DeviceOrientationEvent DragEvent FocusEvent FormDataEvent GamepadEvent InputEvent KeyboardEvent MouseEvent PointerEvent SubmitEvent TouchEvent TransitionEvent WheelEvent

Event Objects

Common Event Attributes:

target
element the event was dispatched to
timeStamp
time since "origin" in milliseonds
isTrusted
was event trigged by the user

Event Objects

KeyboardEvent Attributes

keycode
index of key that was pressed
altKeyctrlKey
metaKeyshiftKey
were modifier keys pressed
repeat
is key automatically repeating

Event Handling in Python

add_event_listener

add_event_listener(elt, event, listener)
elt
An element from the DOM
event
The event (string)
listener
A Callable

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
An element from the DOM
event
The event (string)
listener
A Callable

Import from pyodide.ffi.wrappers

What's Next


				

				
					def foo(event):
						...
						
					import js
					div = js.document.getElementById("btn")
					add_event_listener(div, 'focus', foo)
				
				

						@when('my-div', 'focus')
						def foo():
							...
						

@when decorator

def 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!")
						

What's Next

Event Listener


							
						

Code Runner


							
						

Questions?