Reacton
Reacton is a pure Python library that ports the declarative UI paradigm of React (JavaScript) to the ipywidgets ecosystem. It enables developers to build reusable and composable user interface components within Jupyter notebooks and web applications built with ipywidgets, by offering a declarative approach to state and UI management. It aims to simplify complex UI logic, reduce boilerplate, and enhance maintainability, serving as a foundation for frameworks like Solara. The current version is 1.9.1, with updates released on a feature-ready basis rather than a strict cadence.
Common errors
-
NameError: name 'Button' is not defined
cause A component from `ipywidgets` (like `Button` or `VBox`) was used without being correctly imported, specifically not from `reacton.ipywidgets`.fixImport the wrapped ipywidgets components using `import reacton.ipywidgets as w` and refer to them with the alias, e.g., `w.Button`. -
RuntimeError: Widget constructor arguments do not match traits for widget type X
cause The arguments provided to an `ipywidgets` constructor via Reacton (or a custom wrapper) do not align with the defined traitlets of that widget, or a new version of ipywidgets changed its API.fixCarefully review the constructor arguments for the specific widget (`X`) against the `ipywidgets` documentation. If using custom wrappers, verify the mapping from `reacton` arguments to `ipywidgets` traits. Update `ipywidgets` or `reacton` if there's a known incompatibility. -
UI updates are not reflected after modifying component data
cause The component's data was modified directly instead of using Reacton's state management hooks, preventing Reacton from detecting changes and re-rendering the UI.fixEnsure all mutable data that affects the component's render output is managed using `reacton.use_state`. Updates must be performed via the `set_` function returned by `use_state` to trigger re-renders.
Warnings
- gotcha When integrating with existing `ipywidgets` or custom widgets, Reacton assumes that widget constructor arguments match their underlying traits. Discrepancies can lead to runtime errors.
- gotcha Directly manipulating ipywidgets' elements via `ipywidgets.Widget.element(...)` bypasses Reacton's type-safe wrappers. This can lead to a less robust development experience, lacking type completion and static analysis.
- gotcha Attempting to manage UI state or event handlers imperatively (outside of Reacton's `use_state`, `use_effect`, etc.) can lead to inconsistent UI behavior, memory leaks, and difficult-to-debug issues, similar to common pitfalls in non-declarative UI frameworks.
Install
-
pip install reacton
Imports
- component
import reacton @reacton.component
- use_state
import reacton clicks, set_clicks = reacton.use_state(0)
- w
import ipywidgets as w
import reacton.ipywidgets as w
Quickstart
import reacton
import reacton.ipywidgets as w
from IPython.display import display
@reacton.component
def ButtonClick():
clicks, set_clicks = reacton.use_state(0)
def my_click_handler():
set_clicks(clicks + 1)
button = w.Button(
description=f"Clicked {clicks} times",
on_click=my_click_handler
)
return button
display(ButtonClick())