浏览代码

fix `put_buttons()` issue when buttons have same value

reproduce code:
```
put_buttons([
        {'label': 'A', 'value': 1},
        {'label': 'B', 'value': 1},
    ], [lambda: put_text('A'), lambda: put_text('B')])
```
wangweimin 3 年之前
父节点
当前提交
cb5ac8d512
共有 1 个文件被更改,包括 5 次插入2 次删除
  1. 5 2
      pywebio/output.py

+ 5 - 2
pywebio/output.py

@@ -207,6 +207,7 @@ Layout and Style
 
 
 """
+import copy
 import html
 import io
 import logging
@@ -670,6 +671,7 @@ def _format_button(buttons):
 
     btns = []
     for btn in buttons:
+        btn = copy.deepcopy(btn)
         if isinstance(btn, Mapping):
             assert 'value' in btn and 'label' in btn, 'actions item must have value and label key'
         elif isinstance(btn, (list, tuple)):
@@ -763,10 +765,11 @@ def put_buttons(buttons, onclick, small=None, link_style=False, outline=False, g
 
     if isinstance(onclick, Sequence):
         assert len(btns) == len(onclick), "`onclick` and `buttons` must be same length."
-        onclick = {btn['value']: callback for btn, callback in zip(btns, onclick)}
+        for idx, btn in enumerate(btns):
+            btn['value'] = idx
 
     def click_callback(btn_val):
-        if isinstance(onclick, dict):
+        if isinstance(onclick, Sequence):
             return onclick[btn_val]()
         else:
             return onclick(btn_val)