Skip to content

Commit 205e86f

Browse files
committed
add pin.pin.use_strict() to enable strict pin value getting mode (#241)
1 parent fae206b commit 205e86f

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

pywebio/pin.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135

136136
def check_name(name):
137137
assert all(i in _html_value_chars for i in name), "pin `name` can only contain letters, digits and underscore"
138+
assert name != 'use_strict', "'use_strict' is a reserve name, can't use as pin widget name"
138139
assert name[0] in string.ascii_letters, "pin `name` can only starts with letters"
139140

140141

@@ -224,20 +225,30 @@ def put_actions(name, *, label='', buttons=None, help_text=None,
224225

225226

226227
@chose_impl
227-
def get_client_val():
228+
def get_client_val(strict=False):
228229
res = yield next_client_event()
229230
assert res['event'] == 'js_yield', "Internal Error, please report this bug on " \
230231
"https://github.com/wang0618/PyWebIO/issues"
231-
return res['data']
232+
data = res['data']
233+
assert not strict or data, 'pin widget doesn\'t exist.'
234+
return (data or {}).get('value')
232235

233236

234237
class Pin_:
238+
_strict = False
239+
240+
def use_strict(self):
241+
"""
242+
Enable strict mode for getting pin widget value.
243+
An AssertionError will be raised when try to get value of pin widgets that are currently not in the page.
244+
"""
245+
self._strict = True
235246

236247
def __getattr__(self, name):
237248
"""__getattr__ is only invoked if the attribute wasn't found the usual ways"""
238249
check_name(name)
239250
send_msg('pin_value', spec=dict(name=name))
240-
return get_client_val()
251+
return get_client_val(self._strict)
241252

242253
def __getitem__(self, name):
243254
return self.__getattr__(name)
@@ -246,6 +257,9 @@ def __setattr__(self, name, value):
246257
"""
247258
__setattr__ will be invoked regardless of whether the attribute be found
248259
"""
260+
if name == '_strict':
261+
return object.__setattr__(self, name, value)
262+
249263
check_name(name)
250264
send_msg('pin_update', spec=dict(name=name, attributes={"value": value}))
251265

webiojs/src/handlers/pin.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ export class PinHandler implements CommandHandler {
1515

1616
handle_message(msg: Command) {
1717
if (msg.command === 'pin_value') {
18-
let val = GetPinValue(msg.spec.name)
19-
state.CurrentSession.send_message({event: "js_yield", task_id: msg.task_id, data: val});
18+
let val = GetPinValue(msg.spec.name);
19+
let data = val===undefined? null : {value: val};
20+
state.CurrentSession.send_message({event: "js_yield", task_id: msg.task_id, data: data});
2021
} else if (msg.command === 'pin_update') {
2122
PinUpdate(msg.spec.name, msg.spec.attributes);
2223
} else if (msg.command === 'pin_wait') {

webiojs/src/models/pin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {AfterCurrentOutputWidgetShow} from "../handlers/output";
77
let name2input: { [k: string]: InputItem } = {};
88

99
export function GetPinValue(name: string) {
10+
if(!document.contains(name2input[name].element[0]))
11+
return undefined;
1012
return name2input[name].get_value();
1113
}
1214

0 commit comments

Comments
 (0)