Skip to main content

Button

Render buttons with custom functionality in your form for conditional logic and branching paths. Buttons allow users to trigger custom Python functions, processing or different form behaviors. When a button is pressed, its key becomes True in the form state, enabling conditional logic to determine the next steps in your workflow. This makes buttons essential for creating approval workflows, multi-path forms, and interactive decision trees. Unlike automatic navigation buttons (NextButton/BackButton), custom Button widgets give you full control over widget behavior within a page and can be used to implement complex conditional logic based on user choices.

How Button State Works

When a user clicks a button, the button's key is set to True in the form state. You can then check this state using state.get("button_key") to determine which button was pressed and conditionally render different widgets or forms.

Usage Pattern

Buttons are optionally returned from your Page function (a function that returns a list of widgets). You have two options:

Option 1: Return only widgets (default behavior)


python def my_page(state): return [TextOutput("Hello"), TextOutput("world!")] # Default navigation buttons will be used

Option 2: Return widgets + custom buttons as a tuple


python def my_page(state): widgets = [TextOutput("Hello"), TextOutput("world!")] buttons = [Button("Label 1", key="key1"), Button("Label 2", key="key2")] return widgets, buttons

If you don't return buttons (Option 1), the system automatically provides default navigation buttons (Next/Back). If you want custom buttons, you must return a tuple with widgets as the first element and a list of Button objects as the second element.

Common Use Cases

  • Approval Workflows: Create "Approve" and "Reject" buttons for document review processes
  • Data Reload: Refresh page content with updated information from external APIs or databases
  • Multi-path Forms: Allow users to choose between different information collection paths
  • Decision Trees: Build complex conditional forms based on user choices
  • Custom Navigation: Implement non-linear form progression with custom logic

Examples

Custom Decision Buttons

Use custom buttons to create approval workflows with conditional logic based on user decisions

Example Code

from abstra.forms import Button, TextInput, run


def approval_workflow(state):
# Check which button was pressed
if state.get("approve"):
return [
TextInput("Approved! Add your approval comments:", key="approval_comments")
]
elif state.get("reject"):
return [
TextInput(
"Rejected. Please provide rejection reason:", key="rejection_reason"
)
]
else:
# Initial form with decision buttons
return [TextInput("Enter request details:", key="request_details")], [
Button("Approve", key="approve"),
Button("Reject", key="reject"),
]


# Run the workflow
result = run([approval_workflow])

# Print the result
print(result)

Multi-Step Form Navigation

Use custom buttons to create branching form paths where different buttons lead to different form sections

Example Code

from abstra.forms import Button, DropdownInput, TextInput, run


def multi_path_form(state):
# Check which path the user chose
if state.get("personal_info"):
return [
TextInput("Full Name:", key="name"),
TextInput("Email:", key="email"),
TextInput("Phone:", key="phone"),
]
elif state.get("company_info"):
return [
TextInput("Company Name:", key="company"),
TextInput("Job Title:", key="title"),
DropdownInput(
"Industry:", ["Tech", "Finance", "Healthcare", "Other"], key="industry"
),
]
else:
# Initial choice form
return [
TextInput(
"Welcome! Please choose what information to provide:", key="welcome"
)
], [
Button("Personal Info", key="personal_info"),
Button("Company Info", key="company_info"),
]


# Run the form
result = run([multi_path_form])

# Print the result
print(result)

Data Reload with Button

Use custom buttons to refresh page content with updated data from external sources or calculations

Example Code

import random

from abstra.forms import Button, TextOutput, run

random.seed(42)
number = random.randint(1, 100)


def data_reload_page(state):
global number
# Generate new random number when refresh button is pressed
# This simulates fetching fresh data from an API or database
if state.get("refresh"):
# Button was pressed, generate new number
random.seed(42)
number = random.randint(1, 100)

return [
TextOutput(f"Random Number: {number}"),
TextOutput("Click the button below to generate a new random number:"),
], [Button("Generate New Number", key="refresh")]


# Run the form
result = run([data_reload_page])

# Print the result
print(result)

Parameters

NameDescriptionTypeDefault
label (required)The text displayed on the button.strNone
keyAn optional key for the button. If not provided, the label will be used as the key.strNone