@@ -660,17 +660,18 @@ def put_table(tdata, header=None, scope=None, position=OutputPosition.BOTTOM) ->
660
660
661
661
def _format_button (buttons ):
662
662
"""
663
- Format `buttons` parameter in `put_buttons()`
663
+ Format `buttons` parameter in `put_buttons()`, replace its value with its idx
664
664
:param buttons:
665
665
{label:, value:, }
666
666
(label, value, )
667
667
single value, label=value
668
668
669
- :return: [{value:, label:, }, ...]
669
+ :return: [{value:, label:, }, ...], values
670
670
"""
671
671
672
672
btns = []
673
- for btn in buttons :
673
+ values = []
674
+ for idx , btn in enumerate (buttons ):
674
675
btn = copy .deepcopy (btn )
675
676
if isinstance (btn , Mapping ):
676
677
assert 'value' in btn and 'label' in btn , 'actions item must have value and label key'
@@ -679,8 +680,10 @@ def _format_button(buttons):
679
680
btn = dict (zip (('label' , 'value' ), btn ))
680
681
else :
681
682
btn = dict (value = btn , label = btn )
683
+ values .append (btn ['value' ])
684
+ btn ['value' ] = idx
682
685
btns .append (btn )
683
- return btns
686
+ return btns , values
684
687
685
688
686
689
def put_buttons (buttons , onclick , small = None , link_style = False , outline = False , group = False , scope = None ,
@@ -690,11 +693,19 @@ def put_buttons(buttons, onclick, small=None, link_style=False, outline=False, g
690
693
691
694
:param list buttons: Button list. The available formats of list items are:
692
695
693
- * dict: ``{label:(str)button label, value:(str)button value, color:(str, optional)button color}``
696
+ * dict::
697
+
698
+ {
699
+ "label":(str)button label,
700
+ "value":(str)button value,
701
+ "color":(str, optional)button color,
702
+ "disabled":(bool, optional) whether the button is disabled
703
+ }
704
+
694
705
* tuple or list: ``(label, value)``
695
706
* single value: label and value of option use the same value
696
707
697
- The ``value`` of button can be any JSON serializable object .
708
+ The ``value`` of button can be any type .
698
709
The ``color`` of button can be one of: `primary`, `secondary`, `success`, `danger`, `warning`, `info`, `light`, `dark`.
699
710
700
711
Example:
@@ -760,19 +771,23 @@ def delete():
760
771
put_text("You click delete button")
761
772
762
773
put_buttons(['edit', 'delete'], onclick=[edit, delete])
774
+
775
+ .. versionchanged:: 1.5
776
+ Add ``disabled`` button support.
777
+ The ``value`` of button can be any object.
763
778
"""
764
- btns = _format_button (buttons )
779
+ btns , values = _format_button (buttons )
765
780
766
781
if isinstance (onclick , Sequence ):
767
782
assert len (btns ) == len (onclick ), "`onclick` and `buttons` must be same length."
768
- for idx , btn in enumerate (btns ):
769
- btn ['value' ] = idx
770
783
771
- def click_callback (btn_val ):
784
+ def click_callback (btn_idx ):
772
785
if isinstance (onclick , Sequence ):
773
- return onclick [btn_val ]()
786
+ onclick [btn_idx ]()
774
787
else :
775
- return onclick (btn_val )
788
+ btn_val = values [btn_idx ]
789
+ if not btns [btn_idx ].get ('disabled' ):
790
+ onclick (btn_val )
776
791
777
792
callback_id = output_register_callback (click_callback , ** callback_options )
778
793
spec = _get_output_spec ('buttons' , callback_id = callback_id , buttons = btns , small = small ,
@@ -781,14 +796,15 @@ def click_callback(btn_val):
781
796
return Output (spec )
782
797
783
798
784
- def put_button (label , onclick , color = None , small = None , link_style = False , outline = False , scope = None ,
799
+ def put_button (label , onclick , color = None , small = None , link_style = False , outline = False , disabled = False , scope = None ,
785
800
position = OutputPosition .BOTTOM ) -> Output :
786
801
"""Output a single button and bind click event to it.
787
802
788
803
:param str label: Button label
789
804
:param callable onclick: Callback which will be called when button is clicked.
790
805
:param str color: The color of the button,
791
806
can be one of: `primary`, `secondary`, `success`, `danger`, `warning`, `info`, `light`, `dark`.
807
+ :param bool disabled: Whether the button is disabled
792
808
:param - small, link_style, outline, scope, position: Those arguments have the same meaning as for `put_buttons()`
793
809
794
810
Example:
@@ -800,9 +816,13 @@ def put_button(label, onclick, color=None, small=None, link_style=False, outline
800
816
put_button("click me", onclick=lambda: toast("Clicked"), color='success', outline=True)
801
817
802
818
.. versionadded:: 1.4
819
+
820
+ .. versionchanged:: 1.5
821
+ add ``disabled`` parameter
803
822
"""
804
- return put_buttons ([{'label' : label , 'value' : '' , 'color' : color or 'primary' }], onclick = [onclick ],
805
- small = small , link_style = link_style , outline = outline , scope = scope , position = position )
823
+ return put_buttons ([{'label' : label , 'value' : '' , 'color' : color or 'primary' , 'disabled' : disabled }],
824
+ onclick = [onclick ], small = small , link_style = link_style , outline = outline , scope = scope ,
825
+ position = position )
806
826
807
827
808
828
def put_image (src , format = None , title = '' , width = None , height = None ,
0 commit comments