Skip to content

Commit 993b61f

Browse files
committed
cookbook: update popup_input()
1 parent fc8a546 commit 993b61f

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

docs/cookbook.rst

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,6 @@ In the following code, we define a ``popup_input()`` function, which can be used
129129
:name: cookbook-redirect-stdout
130130
:summary: Redirect stdout to PyWebIO
131131

132-
import threading
133-
134-
135132
def popup_input(pins, names, title='Please fill out the form'):
136133
"""Show a form in popup window.
137134

@@ -143,27 +140,21 @@ In the following code, we define a ``popup_input()`` function, which can be used
143140
if not isinstance(pins, list):
144141
pins = [pins]
145142

146-
event = threading.Event()
147-
confirmed_form = None
143+
from pywebio.utils import random_str
144+
action_name = 'action_' + random_str(10)
148145

149-
def onclick(val):
150-
nonlocal confirmed_form
151-
confirmed_form = val
152-
event.set()
153-
154-
pins.append(put_buttons([
146+
pins.append(put_actions(action_name, buttons=[
155147
{'label': 'Submit', 'value': True},
156148
{'label': 'Cancel', 'value': False, 'color': 'danger'},
157-
], onclick=onclick))
149+
]))
158150
popup(title=title, content=pins, closable=False)
159151

160-
event.wait()
152+
change_info = pin_wait_change(action_name)
153+
result = None
154+
if change_info['name'] == action_name and change_info['value']:
155+
result = {name: pin[name] for name in names}
161156
close_popup()
162-
if not confirmed_form:
163-
return None
164-
165-
from pywebio.pin import pin
166-
return {name: pin[name] for name in names}
157+
return result
167158

168159

169160
from pywebio.pin import put_input

0 commit comments

Comments
 (0)