Skip to content

Commit faf5bf2

Browse files
committed
add init_run to pin_on_change()
1 parent 8a038df commit faf5bf2

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

pywebio/pin.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def pin_update(name, **spec):
334334
send_msg('pin_update', spec=dict(name=name, attributes=attributes))
335335

336336

337-
def pin_on_change(name, onchange=None, clear=False, **callback_options):
337+
def pin_on_change(name, onchange=None, clear=False, init_run=False, **callback_options):
338338
"""
339339
Bind a callback function to pin widget, the function will be called when user change the value of the pin widget.
340340
@@ -346,6 +346,8 @@ def pin_on_change(name, onchange=None, clear=False, **callback_options):
346346
:param callable onchange: callback function
347347
:param bool clear: whether to clear the previous callbacks bound to this pin widget.
348348
If you just want to clear callbacks and not set new callback, use ``pin_on_change(name, clear=True)``.
349+
:param bool init_run: whether to run the ``onchange`` callback once immediately before the pin widget changed.
350+
This parameter can be used to initialize the output.
349351
:param callback_options: Other options of the ``onclick`` callback.
350352
Refer to the ``callback_options`` parameter of :func:`put_buttons() <pywebio.output.put_buttons>`
351353
@@ -354,6 +356,8 @@ def pin_on_change(name, onchange=None, clear=False, **callback_options):
354356
assert not (onchange is None and clear is False), "When `onchange` is `None`, `clear` must be `True`"
355357
if onchange is not None:
356358
callback_id = output_register_callback(onchange, **callback_options)
359+
if init_run:
360+
onchange(pin[name])
357361
else:
358362
callback_id = None
359363
send_msg('pin_onchange', spec=dict(name=name, callback_id=callback_id, clear=clear))

0 commit comments

Comments
 (0)