Skip to content

Commit

Permalink
add pin.pin.use_strict() to enable strict pin value getting mode (#241
Browse files Browse the repository at this point in the history
)
  • Loading branch information
wang0618 committed Dec 21, 2021
1 parent fae206b commit 205e86f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
20 changes: 17 additions & 3 deletions pywebio/pin.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@

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


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


@chose_impl
def get_client_val():
def get_client_val(strict=False):
res = yield next_client_event()
assert res['event'] == 'js_yield', "Internal Error, please report this bug on " \
"https://github.com/wang0618/PyWebIO/issues"
return res['data']
data = res['data']
assert not strict or data, 'pin widget doesn\'t exist.'
return (data or {}).get('value')


class Pin_:
_strict = False

def use_strict(self):
"""
Enable strict mode for getting pin widget value.
An AssertionError will be raised when try to get value of pin widgets that are currently not in the page.
"""
self._strict = True

def __getattr__(self, name):
"""__getattr__ is only invoked if the attribute wasn't found the usual ways"""
check_name(name)
send_msg('pin_value', spec=dict(name=name))
return get_client_val()
return get_client_val(self._strict)

def __getitem__(self, name):
return self.__getattr__(name)
Expand All @@ -246,6 +257,9 @@ def __setattr__(self, name, value):
"""
__setattr__ will be invoked regardless of whether the attribute be found
"""
if name == '_strict':
return object.__setattr__(self, name, value)

check_name(name)
send_msg('pin_update', spec=dict(name=name, attributes={"value": value}))

Expand Down
5 changes: 3 additions & 2 deletions webiojs/src/handlers/pin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export class PinHandler implements CommandHandler {

handle_message(msg: Command) {
if (msg.command === 'pin_value') {
let val = GetPinValue(msg.spec.name)
state.CurrentSession.send_message({event: "js_yield", task_id: msg.task_id, data: val});
let val = GetPinValue(msg.spec.name);
let data = val===undefined? null : {value: val};
state.CurrentSession.send_message({event: "js_yield", task_id: msg.task_id, data: data});
} else if (msg.command === 'pin_update') {
PinUpdate(msg.spec.name, msg.spec.attributes);
} else if (msg.command === 'pin_wait') {
Expand Down
2 changes: 2 additions & 0 deletions webiojs/src/models/pin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {AfterCurrentOutputWidgetShow} from "../handlers/output";
let name2input: { [k: string]: InputItem } = {};

export function GetPinValue(name: string) {
if(!document.contains(name2input[name].element[0]))
return undefined;
return name2input[name].get_value();
}

Expand Down

0 comments on commit 205e86f

Please sign in to comment.