Events
Run Python when the user acts: connect handlers, write actions in the template and read what each control sends.
janelkit Included with janelkit. Nothing extra to install. What you will learn
- How on:event connects a user action to Python.
- The difference between passing a function and writing an action.
- What each control sends to its handler.
An event connects an action to code
An event announces that something happened: a button was clicked, a field received text, a checkbox changed. on:click={save} connects the click of a button to the function save. A handler is the code that runs in response. JanelKit calls it when the event arrives, never earlier.
<script lang="python">
from janelkit import state
count = state(0)
last = state("nothing yet")
def reset():
count.value = 0
last.value = "reset"
def moved(value):
last.value = f"slider at {value:.0f}"
</script>
<window title="Events" width={460} height={300}>
<column padding={24} gap={12}>
<text>Count: {count}</text>
<row gap={8}>
<button on:click={count += 1}>Add one</button>
<button on:click={reset}>Reset</button>
</row>
<slider min={0} max={10} step={1} on:change={moved} label="Level" />
<text>Last action: {last}</text>
</column>
</window>CopyComplete component · Save as src/App.janel in a scaffolded project.
How it works
- on:click={count += 1} is an action written in the template. It runs as is on each click.
- on:click={reset} passes the function itself. Defining reset does nothing; each click calls it.
- <slider> sends its new value with on:change, so moved receives it as its argument.
- Handlers change state; the <text> elements that read that state update by themselves.
What you should see
Add one increases the count, Reset sets it to 0 and the last line to Last action: reset. Moving the slider to 3 shows Last action: slider at 3.
A function or an action
JanelKit decides from what you write. A name, an attribute, an index, a lambda or x if c else y is a function: it is called when the event happens. Anything else, such as a call or an assignment, is an action: it is executed as written when the event happens.
Actions may contain calls and simple statements such as assignments and +=, separated by semicolons. Assigning to a name is only allowed when that name is state in the script. Compound statements such as if, for and def are not allowed; write a function instead.
| You write | Kind | On each click |
|---|---|---|
| on:click={save} | Function | Calls save(). |
| on:click={lambda: remove(item["id"])} | Function | Calls the lambda. |
| on:click={save()} | Action | Runs save(). |
| on:click={count = 0; query = ""} | Action | Runs both assignments. |
| on:click={make_handler(x)} | Action | Runs make_handler(x) and ignores what it returns. |
Common mistakes
- on:click={make_handler(x)} does not call the function that make_handler returns. Write on:click={make_handler(x)()} or use a lambda.
- Assigning to a prop or to a list row variable in an action is a compile error. Change the item in place instead: todo["done"] = True.
What each control sends
Many events carry a value. A <textfield> sends its text with on:input and on:submit; a <checkbox> or <switch> sends True or False with on:change; <slider>, <select>, <radiogroup> and <tabs> send the new value. A handler function receives the arguments its signature accepts, so def submit(): and def submit(text): both work for on:submit.
In an action, the value is available as event: a single value, or a tuple when the event has several. It is not a browser event object.
<!-- done is a Boolean state; query is a text state. -->
<checkbox checked={done} on:change={done = event}>Done</checkbox>
<textfield value={query} on:submit={search(event)} label="Search" />CopyFragment · Add it to an existing component; it is not a complete file.
Common mistakes
- Do not expect event.target.value; event is the value itself.
- Inside an action, event hides any script variable with the same name.
Handlers run on the interface thread
Handlers run on the same thread that draws the window, so the window waits while a handler runs. Keep them short. Changes made by one handler are grouped and the window updates once at the end, so you never need a manual redraw.
For slow work such as reading a large file or calling a web service, use an async def handler and the tools in Async work; the window stays responsive while it waits.
Learn moreAsync work ↗
Practice: two ways to reset
Add a Reset button with an action, on:click={count = 0}, and another that calls a function. Then make Reset also clear the last-action text. Which version is easier to read once the rule has two steps?
Common mistakes
- Starting the app is not testing it. Activate every control and check what changed.
Key points
- on:event={function} calls the function; on:event={action} runs the action.
- Controls send values: text, True or False, the chosen option.
- Handlers run on the interface thread; move slow work to async handlers.
English documentation based on the JanelKit source. For additional details, browse the source docs ↗.