Custom Buttons
For complex interactions, default "Next" and "Back" buttons may not be sufficient. You can define custom buttons as shown below:
from abstra.forms import run, TextInput, Button
import my_api
def deal_decision(state):
if state.get("approve"):
my_api.approve(state["deal_id"])
return
if state.get("reject"):
my_api.reject(state["deal_id"])
return
return [
TextInput(label="Enter deal ID", key="deal_id")
], [Button("Approve", key="approve"), Button("Reject", key="reject")]
state = run([deal_decision])
info
- When a button is pressed, the function runs with its value set to
True. - Since the
keyparameter is optional in the button, if it is omitted, the key in the state that is set toTruewill be the button's label. - Returning widgets advances the page automatically.
ExitButton
Use ExitButton to let users cancel or exit a form at any point. When clicked, ExitButton terminates the entire process via sys.exit(0) — no code after run() will execute.
from abstra.forms import run, TextInput, ExitButton, NextButton
def page_with_exit(state):
return [
TextInput("Enter your email:", key="email"),
], [ExitButton("Cancel"), NextButton()]
def next_page(state):
return [TextInput("More info:", key="info")]
state = run([page_with_exit, next_page])
# This line only runs if the user completed the form normally (not via ExitButton)
print(f"Form completed with email: {state['email']}")
info
ExitButtondoes not validate the current page - the user explicitly chose to exit.- When the user clicks
ExitButton, the process terminates immediately. Any code afterrun()will not execute. - Always pair
ExitButtonwithNextButton()so users can also proceed normally.