Skip to content

Commit 5d0f53a

Browse files
committed
output support bind onclick callback
1 parent 4da9371 commit 5d0f53a

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

pywebio/html/css/app.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,8 @@ details[open]>summary {
284284
top: 0; left: 0; right: 0; bottom: 0;
285285
height: auto;
286286
z-index: 9;
287+
}
288+
289+
.pywebio-clickable{
290+
cursor: pointer;
287291
}

pywebio/io_ctrl.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ def send(self):
128128
def style(self, css_style):
129129
"""Set css style for output
130130
131+
:param str css_style: CSS style string
132+
131133
Example::
132134
133135
put_text('hello').style('color: red; font-size: 20px')
@@ -142,6 +144,16 @@ def style(self, css_style):
142144
self.spec['style'] += ';%s' % css_style
143145
return self
144146

147+
def onclick(self, callback):
148+
"""Add click callback to this widget.
149+
150+
:param callable callback: Callback which will be called when the widget is clicked.
151+
"""
152+
callback_id = output_register_callback(lambda _: callback())
153+
self.spec.setdefault('click_callback_id', '')
154+
self.spec['click_callback_id'] += callback_id
155+
return self
156+
145157
def __del__(self):
146158
"""返回值没有被变量接收时的操作:直接输出消息"""
147159
if not self.processed:

pywebio/output.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
149149
Other
150150
--------------
151-
.. autofunction:: output
151+
.. autofunction:: output
152152
153153
"""
154154
import html
@@ -207,8 +207,6 @@ class Scope:
207207
_scope_name_allowed_chars = set(string.ascii_letters + string.digits + '_-')
208208

209209

210-
211-
212210
def set_scope(name, container_scope=Scope.Current, position=OutputPosition.BOTTOM, if_exist=None):
213211
"""Create a new scope.
214212
@@ -1015,8 +1013,7 @@ def put_scrollable(content=[], height=400, keep_bottom=False, horizon_scroll=Fal
10151013
if not isinstance(content, (list, tuple, OutputList)):
10161014
content = [content]
10171015

1018-
for item in content:
1019-
assert isinstance(item, (str, Output)), "put_scrollable() content must be list of str/put_xxx()"
1016+
content = [i if isinstance(i, Output) else put_text(i) for i in content]
10201017

10211018
if 'max_height' in kwargs:
10221019
import warnings

webiojs/src/models/output.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ export function getWidgetElement(spec: any) {
223223
let old_style = elem.attr('style') || '';
224224
elem.attr({"style": old_style + ';' + spec.style});
225225
}
226+
if (spec.click_callback_id) {
227+
elem.on('click', (e) => {
228+
pushData(null, spec.click_callback_id);
229+
});
230+
elem.addClass('pywebio-clickable');
231+
}
226232
if (spec.container_dom_id) {
227233
if (spec.container_selector)
228234
elem.find(spec.container_selector).attr('id', spec.container_dom_id);
@@ -273,10 +279,10 @@ export function render_tpl(tpl: string, data: { [i: string]: any }) {
273279
let elem = parseHtml(html);
274280
for (let dom_id in placeholder2spec) {
275281
let spec = placeholder2spec[dom_id];
276-
try{
282+
try {
277283
let sub_elem = getWidgetElement(spec);
278284
elem.find(`#${dom_id}`).replaceWith(sub_elem);
279-
}catch (e) {
285+
} catch (e) {
280286
console.error('Error when render widget: \n%s', JSON.stringify(spec));
281287
}
282288
}

0 commit comments

Comments
 (0)