Skip to content

Commit cd4850d

Browse files
committed
feat: add output.put_button() to output a single button
1 parent 797bc88 commit cd4850d

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

pywebio/output.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
| +---------------------------+------------------------------------------------------------+
5353
| | `put_table`:sup:`*` | Output table |
5454
| +---------------------------+------------------------------------------------------------+
55-
| | `put_buttons` | Output a group of buttons and bind click event |
55+
| | | `put_button` | Output button and bind click event |
56+
| | | `put_buttons` | |
5657
| +---------------------------+------------------------------------------------------------+
5758
| | `put_image` | Output image |
5859
| +---------------------------+------------------------------------------------------------+
@@ -124,6 +125,7 @@
124125
.. autofunction:: put_table
125126
.. autofunction:: span
126127
.. autofunction:: put_buttons
128+
.. autofunction:: put_button
127129
.. autofunction:: put_image
128130
.. autofunction:: put_file
129131
.. autofunction:: put_tabs
@@ -173,7 +175,7 @@
173175

174176
__all__ = ['Position', 'remove', 'scroll_to', 'put_tabs',
175177
'put_text', 'put_html', 'put_code', 'put_markdown', 'use_scope', 'set_scope', 'clear', 'remove',
176-
'put_table', 'put_buttons', 'put_image', 'put_file', 'PopupSize', 'popup',
178+
'put_table', 'put_buttons', 'put_image', 'put_file', 'PopupSize', 'popup', 'put_button',
177179
'close_popup', 'put_widget', 'put_collapse', 'put_link', 'put_scrollable', 'style', 'put_column',
178180
'put_row', 'put_grid', 'span', 'put_processbar', 'set_processbar', 'put_loading',
179181
'output', 'toast', 'get_scope', 'put_info', 'put_error', 'put_warning', 'put_success']
@@ -722,6 +724,28 @@ def click_callback(btn_val):
722724
return Output(spec)
723725

724726

727+
def put_button(label, onclick, color=None, small=None, link_style=False, outline=False, scope=Scope.Current,
728+
position=OutputPosition.BOTTOM) -> Output:
729+
"""Output a single button and bind click event to it.
730+
731+
:param str label: Button label
732+
:param callable onclick: Callback which will be called when button is clicked.
733+
:param str color: The color of the button,
734+
can be one of: `primary`, `secondary`, `success`, `danger`, `warning`, `info`, `light`, `dark`.
735+
:param - small, link_style, outline, scope, position: Those arguments have the same meaning as for `put_buttons()`
736+
737+
Example:
738+
739+
.. exportable-codeblock::
740+
:name: put_button
741+
:summary: `put_button()` usage
742+
743+
put_button("click me", onclick=lambda: toast("Clicked"), color='success', outline=True)
744+
"""
745+
return put_buttons([{'label': label, 'value': '', 'color': color or 'primary'}], onclick=[onclick],
746+
small=small, link_style=link_style, outline=outline, scope=scope, position=position)
747+
748+
725749
def put_image(src, format=None, title='', width=None, height=None,
726750
scope=Scope.Current, position=OutputPosition.BOTTOM) -> Output:
727751
"""Output image

test/template.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ def edit_row(choice, row):
158158
with use_scope('put_buttons'):
159159
put_buttons(['A', 'B', 'C'], onclick=partial(put_text, scope='put_buttons'))
160160

161+
# put_button
162+
put_button("click me", onclick=lambda: toast("Clicked"), color='success', outline=True)
163+
161164
put_markdown('### Image')
162165
put_image(img_data)
163166
from PIL.Image import open as pil_open

0 commit comments

Comments
 (0)