102102.. autofunction:: put_text
103103.. autofunction:: put_markdown
104104
105- .. py:function:: put_info(*contents, closable=False, scope=-1 , position=-1) -> Output:
106- put_success(*contents, closable=False, scope=-1 , position=-1) -> Output:
107- put_warning(*contents, closable=False, scope=-1 , position=-1) -> Output:
108- put_error(*contents, closable=False, scope=-1 , position=-1) -> Output:
105+ .. py:function:: put_info(*contents, closable=False, scope=None , position=-1) -> Output:
106+ put_success(*contents, closable=False, scope=None , position=-1) -> Output:
107+ put_warning(*contents, closable=False, scope=None , position=-1) -> Output:
108+ put_error(*contents, closable=False, scope=None , position=-1) -> Output:
109109
110110 Output Messages.
111111
@@ -200,23 +200,17 @@ class OutputPosition:
200200 BOTTOM = - 1
201201
202202
203- class Scope :
204- Current = - 1
205- Root = 0
206- Parent = - 2
207-
208-
209203_scope_name_allowed_chars = set (string .ascii_letters + string .digits + '_-' )
210204
211205
212- def set_scope (name , container_scope = Scope . Current , position = OutputPosition .BOTTOM , if_exist = None ):
206+ def set_scope (name , container_scope = None , position = OutputPosition .BOTTOM , if_exist = None ):
213207 """Create a new scope.
214208
215209 :param str name: scope name
216- :param int/str container_scope: Specify the parent scope of this scope. You can use the scope name or use a integer to index the runtime scope stack (see :ref:`User Guide <scope_param>`). When the scope does not exist, no operation is performed.
210+ :param str container_scope: Specify the parent scope of this scope.
211+ When the scope doesn't exist, no operation is performed.
217212 :param int position: The location where this scope is created in the parent scope.
218- Available values: `OutputPosition.TOP`: created at the top of the parent scope, `OutputPosition.BOTTOM`: created at the bottom of the parent scope.
219- You can also use a integer to index the position (see :ref:`User Guide <scope_param>`)
213+ (see :ref:`Scope related parameters <scope_param>`)
220214 :param str if_exist: What to do when the specified scope already exists:
221215
222216 - `None`: Do nothing
@@ -225,65 +219,65 @@ def set_scope(name, container_scope=Scope.Current, position=OutputPosition.BOTTO
225219
226220 Default is `None`
227221 """
228- if isinstance (container_scope , int ):
229- container_scope = get_current_session ().get_scope_name (container_scope )
230-
222+ if container_scope is None :
223+ container_scope = get_scope ()
231224 assert is_html_safe_value (name ), "Scope name only allow letter/digit/'_'/'-' char."
232225 send_msg ('output_ctl' , dict (set_scope = scope2dom (name , no_css_selector = True ),
233226 container = scope2dom (container_scope ),
234227 position = position , if_exist = if_exist ))
235228
236229
237- def get_scope (stack_idx = Scope . Current ):
230+ def get_scope (stack_idx = - 1 ):
238231 """Get the scope name of runtime scope stack
239232
240233 :param int stack_idx: The index of the runtime scope stack. Default is -1.
241234
242- 0 means the top level scope(the ROOT Scope),
235+ 0 means the top level scope(the `` ROOT`` Scope),
243236 -1 means the current Scope,
244- -2 means the scope used before entering the current scope, …
237+ -2 means the scope used before entering the current scope, ...
245238 :return: Returns the scope name with the index, and returns ``None`` when occurs index error
246239 """
247240 try :
248241 return get_current_session ().get_scope_name (stack_idx )
249242 except IndexError :
243+ logger .exception ("Scope stack index error" )
250244 return None
251245
252246
253- def clear (scope = Scope . Current ):
247+ def clear (scope = None ):
254248 """Clear the content of the specified scope
255249
256- :param int/ str scope: Can specify the scope name or use a integer to index the runtime scope stack (see :ref:`User Guide <scope_param>`)
250+ :param str scope: Target scope name. Default is the current scope.
257251 """
258- if isinstance ( scope , int ) :
259- scope = get_current_session (). get_scope_name ( scope )
252+ if scope is None :
253+ scope = get_scope ( )
260254 send_msg ('output_ctl' , dict (clear = scope2dom (scope )))
261255
262256
263- def remove (scope = Scope . Current ):
257+ def remove (scope = None ):
264258 """Remove the specified scope
265259
266- :param int/ str scope: Can specify the scope name or use a integer to index the runtime scope stack (see :ref:`User Guide <scope_param>`)
260+ :param str scope: Target scope name. Default is the current scope.
267261 """
268- if isinstance ( scope , int ) :
269- scope = get_current_session (). get_scope_name ( scope )
262+ if scope is None :
263+ scope = get_scope ( )
270264 assert scope != 'ROOT' , "Can not remove `ROOT` scope."
271265 send_msg ('output_ctl' , dict (remove = scope2dom (scope )))
272266
273267
274- def scroll_to (scope = Scope . Current , position = Position .TOP ):
268+ def scroll_to (scope = None , position = Position .TOP ):
275269 """
276270 Scroll the page to the specified scope
277271
278- :param str/int scope: Target scope. Can specify the scope name or use a integer to index the runtime scope stack (see :ref:`User Guide <scope_param>`)
272+ :param str scope: Target scope. Default is the current scope.
279273 :param str position: Where to place the scope in the visible area of the page. Available value:
280274
281275 * ``'top'`` : Keep the scope at the top of the visible area of the page
282276 * ``'middle'`` : Keep the scope at the middle of the visible area of the page
283277 * ``'bottom'`` : Keep the scope at the bottom of the visible area of the page
284278 """
285- if isinstance ( scope , int ) :
286- scope = get_current_session (). get_scope_name ( scope )
279+ if scope is None :
280+ scope = get_scope ( )
287281 send_msg ('output_ctl' , dict (scroll_to = scope2dom (scope ), position = position ))
288282
289283
@@ -292,7 +286,7 @@ def _get_output_spec(type, scope, position, **other_spec):
292286 get the spec dict of output functions
293287
294288 :param str type: output type
295- :param int/ str scope: target scope
289+ :param str scope: target scope
296290 :param int position:
297291 :param other_spec: Additional output parameters, the None value will not be included in the return value
298292
@@ -303,8 +297,8 @@ def _get_output_spec(type, scope, position, **other_spec):
303297 # add non-None arguments to spec
304298 spec .update ({k : v for k , v in other_spec .items () if v is not None })
305299
306- if isinstance ( scope , int ) :
307- scope_name = get_current_session (). get_scope_name ( scope )
300+ if not scope :
301+ scope_name = get_scope ( )
308302 else :
309303 scope_name = scope
310304
@@ -314,14 +308,14 @@ def _get_output_spec(type, scope, position, **other_spec):
314308 return spec
315309
316310
317- def put_text (* texts , sep = ' ' , inline = False , scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
311+ def put_text (* texts , sep = ' ' , inline = False , scope = None , position = OutputPosition .BOTTOM ) -> Output :
318312 """
319313 Output plain text
320314
321315 :param texts: Texts need to output. The type can be any object, and the `str()` function will be used for non-string objects.
322316 :param str sep: The separator between the texts
323317 :param bool inline: Use text as an inline element (no line break at the end of the text). Default is ``False``
324- :param int/ str scope: The target scope to output. If the scope does not exist, no operation will be performed.
318+ :param str scope: The target scope to output. If the scope does not exist, no operation will be performed.
325319
326320 Can specify the scope name or use a integer to index the runtime scope stack.
327321 :param int position: The position where the content is output in target scope
@@ -333,7 +327,7 @@ def put_text(*texts, sep=' ', inline=False, scope=Scope.Current, position=Output
333327 return Output (spec )
334328
335329
336- def _put_message (color , contents , closable = False , scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
330+ def _put_message (color , contents , closable = False , scope = None , position = OutputPosition .BOTTOM ) -> Output :
337331 tpl = r"""
338332<div class="alert alert-{{color}} {{#dismissible}}alert-dismissible fade show{{/dismissible}}" role="alert">
339333{{#contents}}
@@ -350,7 +344,7 @@ def _put_message(color, contents, closable=False, scope=Scope.Current, position=
350344 scope = scope , position = position ).enable_context_manager ()
351345
352346
353- def put_info (* contents , closable = False , scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
347+ def put_info (* contents , closable = False , scope = None , position = OutputPosition .BOTTOM ) -> Output :
354348 """Output information message.
355349
356350 :param contents: Message contents.
@@ -363,29 +357,29 @@ def put_info(*contents, closable=False, scope=Scope.Current, position=OutputPosi
363357 return _put_message (color = 'info' , contents = contents , closable = closable , scope = scope , position = position )
364358
365359
366- def put_success (* contents , closable = False , scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
360+ def put_success (* contents , closable = False , scope = None , position = OutputPosition .BOTTOM ) -> Output :
367361 """Output success message.
368362 .. seealso:: `put_info()`
369363 .. versionadded:: 1.2
370364 """
371365 return _put_message (color = 'success' , contents = contents , closable = closable , scope = scope , position = position )
372366
373367
374- def put_warning (* contents , closable = False , scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
368+ def put_warning (* contents , closable = False , scope = None , position = OutputPosition .BOTTOM ) -> Output :
375369 """Output warning message.
376370 .. seealso:: `put_info()`
377371 """
378372 return _put_message (color = 'warning' , contents = contents , closable = closable , scope = scope , position = position )
379373
380374
381- def put_error (* contents , closable = False , scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
375+ def put_error (* contents , closable = False , scope = None , position = OutputPosition .BOTTOM ) -> Output :
382376 """Output error message.
383377 .. seealso:: `put_info()`
384378 """
385379 return _put_message (color = 'danger' , contents = contents , closable = closable , scope = scope , position = position )
386380
387381
388- def put_html (html , sanitize = False , scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
382+ def put_html (html , sanitize = False , scope = None , position = OutputPosition .BOTTOM ) -> Output :
389383 """
390384 Output HTML content
391385
@@ -405,7 +399,7 @@ def put_html(html, sanitize=False, scope=Scope.Current, position=OutputPosition.
405399 return Output (spec )
406400
407401
408- def put_code (content , language = '' , rows = None , scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
402+ def put_code (content , language = '' , rows = None , scope = None , position = OutputPosition .BOTTOM ) -> Output :
409403 """
410404 Output code block
411405
@@ -431,7 +425,7 @@ def put_code(content, language='', rows=None, scope=Scope.Current, position=Outp
431425
432426
433427def put_markdown (mdcontent , strip_indent = 0 , lstrip = False , options = None , sanitize = True ,
434- scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
428+ scope = None , position = OutputPosition .BOTTOM ) -> Output :
435429 """
436430 Output Markdown
437431
@@ -512,7 +506,7 @@ def span(content, row=1, col=1):
512506
513507
514508@safely_destruct_output_when_exp ('tdata' )
515- def put_table (tdata , header = None , scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
509+ def put_table (tdata , header = None , scope = None , position = OutputPosition .BOTTOM ) -> Output :
516510 """
517511 Output table
518512
@@ -628,7 +622,7 @@ def _format_button(buttons):
628622 return btns
629623
630624
631- def put_buttons (buttons , onclick , small = None , link_style = False , outline = False , group = False , scope = Scope . Current ,
625+ def put_buttons (buttons , onclick , small = None , link_style = False , outline = False , group = False , scope = None ,
632626 position = OutputPosition .BOTTOM , ** callback_options ) -> Output :
633627 """
634628 Output a group of buttons and bind click event
@@ -699,11 +693,6 @@ def delete():
699693 put_text("You click delete button")
700694
701695 put_buttons(['edit', 'delete'], onclick=[edit, delete])
702-
703- .. attention::
704-
705- After the PyWebIO session (see :ref:`Server and script mode <server_and_script_mode>` for more information about session) closed, the event callback will not work. You can call the :func:`pywebio.session.hold()` function at the end of the task function to hold the session, so that the event callback will always be available before the browser page is closed by user.
706-
707696 """
708697 btns = _format_button (buttons )
709698
@@ -724,7 +713,7 @@ def click_callback(btn_val):
724713 return Output (spec )
725714
726715
727- def put_button (label , onclick , color = None , small = None , link_style = False , outline = False , scope = Scope . Current ,
716+ def put_button (label , onclick , color = None , small = None , link_style = False , outline = False , scope = None ,
728717 position = OutputPosition .BOTTOM ) -> Output :
729718 """Output a single button and bind click event to it.
730719
@@ -741,13 +730,15 @@ def put_button(label, onclick, color=None, small=None, link_style=False, outline
741730 :summary: `put_button()` usage
742731
743732 put_button("click me", onclick=lambda: toast("Clicked"), color='success', outline=True)
733+
734+ .. versionadded:: 1.4
744735 """
745736 return put_buttons ([{'label' : label , 'value' : '' , 'color' : color or 'primary' }], onclick = [onclick ],
746737 small = small , link_style = link_style , outline = outline , scope = scope , position = position )
747738
748739
749740def put_image (src , format = None , title = '' , width = None , height = None ,
750- scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
741+ scope = None , position = OutputPosition .BOTTOM ) -> Output :
751742 """Output image
752743
753744 :param src: Source of image. It can be a string specifying image URL, a bytes-like object specifying the binary content of an image or an instance of ``PIL.Image.Image``
@@ -792,7 +783,7 @@ def put_image(src, format=None, title='', width=None, height=None,
792783 return put_html (tag , scope = scope , position = position )
793784
794785
795- def put_file (name , content , label = None , scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
786+ def put_file (name , content , label = None , scope = None , position = OutputPosition .BOTTOM ) -> Output :
796787 """Output a link to download a file
797788
798789 To show a link with the file name on the browser. When click the link, the browser automatically downloads the file.
@@ -822,7 +813,7 @@ def put_file(name, content, label=None, scope=Scope.Current, position=OutputPosi
822813 return output
823814
824815
825- def put_link (name , url = None , app = None , new_window = False , scope = Scope . Current ,
816+ def put_link (name , url = None , app = None , new_window = False , scope = None ,
826817 position = OutputPosition .BOTTOM ) -> Output :
827818 """Output hyperlinks to other web page or PyWebIO Application page.
828819
@@ -843,7 +834,7 @@ def put_link(name, url=None, app=None, new_window=False, scope=Scope.Current,
843834 return put_html (tag , scope = scope , position = position )
844835
845836
846- def put_processbar (name , init = 0 , label = None , auto_close = False , scope = Scope . Current ,
837+ def put_processbar (name , init = 0 , label = None , auto_close = False , scope = None ,
847838 position = OutputPosition .BOTTOM ) -> Output :
848839 """Output a process bar
849840
@@ -906,7 +897,7 @@ def set_processbar(name, value, label=None):
906897 run_js (js_code )
907898
908899
909- def put_loading (shape = 'border' , color = 'dark' , scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
900+ def put_loading (shape = 'border' , color = 'dark' , scope = None , position = OutputPosition .BOTTOM ) -> Output :
910901 """Output loading prompt
911902
912903 :param str shape: The shape of loading prompt. The available values are: `'border'` (default)、 `'grow'`
@@ -959,7 +950,7 @@ def exit_(self, exc_type, exc_val, exc_tb):
959950
960951
961952@safely_destruct_output_when_exp ('content' )
962- def put_collapse (title , content = [], open = False , scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
953+ def put_collapse (title , content = [], open = False , scope = None , position = OutputPosition .BOTTOM ) -> Output :
963954 """Output collapsible content
964955
965956 :param str title: Title of content
@@ -1004,7 +995,7 @@ def put_collapse(title, content=[], open=False, scope=Scope.Current, position=Ou
1004995
1005996@safely_destruct_output_when_exp ('content' )
1006997def put_scrollable (content = [], height = 400 , keep_bottom = False , horizon_scroll = False , border = True ,
1007- scope = Scope . Current , position = OutputPosition .BOTTOM , ** kwargs ) -> Output :
998+ scope = None , position = OutputPosition .BOTTOM , ** kwargs ) -> Output :
1008999 """Output a fixed height content area. scroll bar is displayed when the content exceeds the limit
10091000
10101001 :type content: list/str/put_xxx()
@@ -1088,7 +1079,7 @@ def put_scrollable(content=[], height=400, keep_bottom=False, horizon_scroll=Fal
10881079
10891080
10901081@safely_destruct_output_when_exp ('tabs' )
1091- def put_tabs (tabs , scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
1082+ def put_tabs (tabs , scope = None , position = OutputPosition .BOTTOM ) -> Output :
10921083 """Output tabs.
10931084
10941085 :param list tabs: Tab list, each item is a dict: ``{"title": "Title", "content": ...}`` .
@@ -1123,7 +1114,7 @@ def put_tabs(tabs, scope=Scope.Current, position=OutputPosition.BOTTOM) -> Outpu
11231114
11241115
11251116@safely_destruct_output_when_exp ('data' )
1126- def put_widget (template , data , scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
1117+ def put_widget (template , data , scope = None , position = OutputPosition .BOTTOM ) -> Output :
11271118 """Output your own widget
11281119
11291120 :param template: html template, using `mustache.js <https://github.com/janl/mustache.js>`_ syntax
@@ -1168,7 +1159,7 @@ def put_widget(template, data, scope=Scope.Current, position=OutputPosition.BOTT
11681159
11691160
11701161@safely_destruct_output_when_exp ('content' )
1171- def put_row (content = [], size = None , scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
1162+ def put_row (content = [], size = None , scope = None , position = OutputPosition .BOTTOM ) -> Output :
11721163 """Use row layout to output content. The content is arranged horizontally
11731164
11741165 :param list content: Content list, the item is ``put_xxx()`` call or ``None``. ``None`` represents the space between the output
@@ -1207,7 +1198,7 @@ def put_row(content=[], size=None, scope=Scope.Current, position=OutputPosition.
12071198
12081199
12091200@safely_destruct_output_when_exp ('content' )
1210- def put_column (content = [], size = None , scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
1201+ def put_column (content = [], size = None , scope = None , position = OutputPosition .BOTTOM ) -> Output :
12111202 """Use column layout to output content. The content is arranged vertically
12121203
12131204 :param list content: Content list, the item is ``put_xxx()`` call or ``None``. ``None`` represents the space between the output
@@ -1219,7 +1210,7 @@ def put_column(content=[], size=None, scope=Scope.Current, position=OutputPositi
12191210 return _row_column_layout (content , flow = 'row' , size = size , scope = scope , position = position ).enable_context_manager ()
12201211
12211212
1222- def _row_column_layout (content , flow , size , scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
1213+ def _row_column_layout (content , flow , size , scope = None , position = OutputPosition .BOTTOM ) -> Output :
12231214 if not isinstance (content , (list , tuple , OutputList )):
12241215 content = [content ]
12251216
@@ -1243,7 +1234,7 @@ def _row_column_layout(content, flow, size, scope=Scope.Current, position=Output
12431234
12441235@safely_destruct_output_when_exp ('content' )
12451236def put_grid (content , cell_width = 'auto' , cell_height = 'auto' , cell_widths = None , cell_heights = None , direction = 'row' ,
1246- scope = Scope . Current , position = OutputPosition .BOTTOM ) -> Output :
1237+ scope = None , position = OutputPosition .BOTTOM ) -> Output :
12471238 """Output content using grid layout
12481239
12491240 :param content: Content of grid, which is a two-dimensional list. The item of list is ``put_xxx()`` call or ``None``.
0 commit comments