Handles¶
Typed imperative handles published on ref.current when a
Ref from use_ref is
passed to a built-in element. The reconciler picks the handle class by
element type after commit and clears ref.current back to None on
unmount. Methods that act on the view are plain calls; methods that need
an answer from the native view are async. Composite components
(FlatList, SectionList) publish a
ListController instead, through
use_imperative_handle.
import pythonnative as pn
@pn.component
def Search() -> pn.Node:
field = pn.use_ref()
def focus_field():
if field.current is not None:
field.current.focus()
pn.use_effect(focus_field, [])
return pn.TextInput(placeholder="Search", ref=field)
Typed imperative handles published on ref.current for built-in elements.
When a Ref is passed to a built-in element, the
reconciler publishes a handle chosen by element type on ref.current
after commit and clears it back to None on unmount:
| Element | ref.current |
|---|---|
TextInput |
TextInputHandle |
ScrollView |
ScrollViewHandle |
WebView |
WebViewHandle |
| everything else | ViewHandle |
Every handle exposes the view's tag, its type_name, and
frame (the last committed LayoutEvent,
written by the layout pass). Methods that act on the view are plain
calls; they queue behind the pending mount, so they reach the view the
last commit created. Methods that need an answer from the native view
are async and await it without blocking the application loop.
Composite components (FlatList, SectionList) publish their own
ListController instead.
Example
Classes:
| Name | Description |
|---|---|
ScrollOffset |
A scroll container's content offset in logical units. |
ViewHandle |
The imperative handle for a mounted built-in element. |
TextInputHandle |
Handle for |
ScrollViewHandle |
Handle for |
WebViewHandle |
Handle for |
Functions:
| Name | Description |
|---|---|
make_handle |
Build the handle the reconciler publishes on |
Attributes:
| Name | Type | Description |
|---|---|---|
HANDLE_TYPES |
Dict[str, type[ViewHandle]]
|
Element types with a specialized handle; every other type gets a |
HANDLE_TYPES
module-attribute
¶
HANDLE_TYPES: Dict[str, type[ViewHandle]] = {
"TextInput": TextInputHandle,
"ScrollView": ScrollViewHandle,
"WebView": WebViewHandle,
}
Element types with a specialized handle; every other type gets a ViewHandle.
ScrollOffset
dataclass
¶
A scroll container's content offset in logical units.
ViewHandle
¶
The imperative handle for a mounted built-in element.
Attributes:
| Name | Type | Description |
|---|---|---|
tag |
The reconciler-assigned view tag. |
|
type_name |
The element type ( |
|
frame |
Optional[LayoutEvent]
|
The last committed frame as a
|
Methods:
| Name | Description |
|---|---|
command |
Queue a native view command by name; returns a future for its result. |
measure_in_window |
The view's frame relative to its screen, read from the renderer core. |
command
¶
Queue a native view command by name; returns a future for its result.
The typed methods on the subclasses are thin wrappers over this; it stays public for commands a custom native component declares in its own contract. Await the future for a command that answers.
measure_in_window
async
¶
measure_in_window() -> Optional[LayoutEvent]
The view's frame relative to its screen, read from the renderer core.
Unlike frame, which is relative to the parent and updated after
each layout, this answers for the latest layout immediately, and
works for views the core flattened out of the mounted tree.
TextInputHandle
¶
Bases: ViewHandle
Handle for TextInput.
Methods:
| Name | Description |
|---|---|
focus |
Give the field keyboard focus. |
blur |
Remove keyboard focus from the field. |
clear |
Empty the field ( |
select_all |
Select the whole text. |
set_selection |
Move the caret to |
get_value |
Return the field's current text as the native view holds it. |
set_selection
¶
Move the caret to start, or select start to end (UTF-16 offsets).
ScrollViewHandle
¶
Bases: ViewHandle
Handle for ScrollView.
Methods:
| Name | Description |
|---|---|
scroll_to |
Scroll to a content offset; an omitted axis keeps its current offset. |
scroll_to_end |
Scroll to the end of the content along the scroll axis. |
flash_scroll_indicators |
Briefly show the scroll indicators. |
get_scroll_offset |
Return the current content offset. |
WebViewHandle
¶
Bases: ViewHandle
Handle for WebView.
Methods:
| Name | Description |
|---|---|
reload |
Reload the current page. |
go_back |
Navigate back in the page history. |
go_forward |
Navigate forward in the page history. |
stop_loading |
Stop the current load. |
load_url |
Navigate to |
inject_javascript |
Evaluate |
eval_js |
Evaluate |
can_go_back |
Whether the page history has an entry to go back to. |
can_go_forward |
Whether the page history has an entry to go forward to. |
get_url |
Return the URL of the current page. |
inject_javascript
¶
inject_javascript(script: str) -> None
Evaluate script in the page without waiting for a result.
eval_js
async
¶
Evaluate script in the page and return its result as a string.
The page answers asynchronously, so this awaits the WebViews
native module rather than issuing a view command. Strings come back
as-is, null and undefined as "", and other values in
their JSON form ("42", "true").
Raises:
| Type | Description |
|---|---|
NativeModuleError
|
If the script throws or the page can't be reached (a cross-origin page in the browser preview). |
make_handle
¶
make_handle(
type_name: str, tag: int, backend: Any
) -> ViewHandle
Build the handle the reconciler publishes on ref.current for type_name.
Next steps¶
- Read the usage patterns in Refs and handles.
- Scroll a list imperatively with
ListController; see the Lists guide. - Call a custom component's own commands through
ViewHandle.command; see Custom native components.