Skip to content

Other

An assortment of helper methods.

Props

Often it's desirable to define fully typed the arguments on component method. Some of the arguments may be specific to the method logic while other arguments are to be aggregated into an attribute dictionary and passed to a child element. The props() method allows the method arguments to be sliced and filtered to achieve this.

reactpy_utils.props.props(include=None, exclude=None)

Convert the caller functions arguments into a props dict. Include the arguments named in the include string. Exclude those named in the exclude string. If nether include or exclude are defined all the arguments are added to the props dict.

Parameters:

Name Type Description Default
include str

Arguments to include in the returned props. Defaults to None.

None
exclude str

Arguments to exclude from the returned props. Defaults to None.

None

Returns:

Type Description
dict[str, Any]

dict[str, Any]: The props

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@component
def Input(
    label: str | None = None,
    id: str | None = None,
    name: str | None = None,
    placeholder: str | None = None,
    value: str | None = None,
):
    _input_props = props(include="id, name, placeholder, value")
    return html.div(html.h2(label), html.input(_input_props))

Immutable Counter

reactpy_utils.static_counter.ID()

An immutable counter. Returns 0, 1, 2 ... on each successive call

Returns:

Name Type Description
int int

count 0, 1, 2 ...

Render Child Components When test is True

Render children when test is True

Parameters:

Name Type Description Default
test bool

description

required

Returns:

Name Type Description
Component VdomChild

Return a fragment containing the child components

Returns:

Name Type Description
Component VdomChild

Return a fragment containing the child components

Example

1
2
3
@component
def App():
    return AppContext(When(app_state.is_valid, MainPage()))

Unique_sequence

reactpy_utils.UID(prefix='')

Generator method that returns unique IDs prepended with the given prefix

Parameters:

Name Type Description Default
prefix str

The ID prefix Defaults to "".

''

Returns:

Name Type Description
str str

The unique ID, eg (user-da80b519)

Example:

1
user_ids = [UID(prefix="user") for i in range(1000)]


Last update: November 30, 2024
Authors: Steve Jones