1717+--------------------+---------------------------+------------------------------------------------------------+
1818| | **Name** | **Description** |
1919+--------------------+---------------------------+------------------------------------------------------------+
20- | Output Scope | `set_scope ` | Create a new scope |
20+ | Output Scope | `put_scope ` | Create a new scope |
2121| +---------------------------+------------------------------------------------------------+
22- | | `get_scope` | Get the scope name in the runtime scope stack |
22+ | | `use_scope`:sup:`†` | Enter a scope |
23+ | +---------------------------+------------------------------------------------------------+
24+ | | `get_scope` | Get the current scope name in the runtime scope stack |
2325| +---------------------------+------------------------------------------------------------+
2426| | `clear` | Clear the content of scope |
2527| +---------------------------+------------------------------------------------------------+
2628| | `remove` | Remove the scope |
2729| +---------------------------+------------------------------------------------------------+
2830| | `scroll_to` | Scroll the page to the scope |
29- | +---------------------------+------------------------------------------------------------+
30- | | `use_scope`:sup:`†` | Open or enter a scope |
3131+--------------------+---------------------------+------------------------------------------------------------+
3232| Content Outputting | `put_text` | Output plain text |
3333| +---------------------------+------------------------------------------------------------+
9595
9696 * :ref:`Use Guide: Output Scope <output_scope>`
9797
98- .. autofunction:: set_scope
98+ .. autofunction:: put_scope
99+ .. autofunction:: use_scope
99100.. autofunction:: get_scope
100101.. autofunction:: clear
101102.. autofunction:: remove
102103.. autofunction:: scroll_to
103- .. autofunction:: use_scope
104104
105105Content Outputting
106106-----------------------
232232
233233logger = logging .getLogger (__name__ )
234234
235- __all__ = ['Position' , 'remove' , 'scroll_to' , 'put_tabs' ,
235+ __all__ = ['Position' , 'remove' , 'scroll_to' , 'put_tabs' , 'put_scope' ,
236236 'put_text' , 'put_html' , 'put_code' , 'put_markdown' , 'use_scope' , 'set_scope' , 'clear' , 'remove' ,
237237 'put_table' , 'put_buttons' , 'put_image' , 'put_file' , 'PopupSize' , 'popup' , 'put_button' ,
238238 'close_popup' , 'put_widget' , 'put_collapse' , 'put_link' , 'put_scrollable' , 'style' , 'put_column' ,
@@ -1390,10 +1390,30 @@ def put_grid(content, cell_width='auto', cell_height='auto', cell_widths=None, c
13901390 return put_widget (template = tpl , data = dict (contents = content ), scope = scope , position = position )
13911391
13921392
1393+ @safely_destruct_output_when_exp ('content' )
1394+ def put_scope (name , content = [], scope = None , position = OutputPosition .BOTTOM ) -> Output :
1395+ """Output a scope
1396+
1397+ :param str name:
1398+ :param list/put_xxx() content: The initial content of the scope, can be ``put_xxx()`` or a list of it.
1399+ :param int scope, position: Those arguments have the same meaning as for `put_text()`
1400+ """
1401+ if not isinstance (content , list ):
1402+ content = [content ]
1403+
1404+ dom_id = scope2dom (name , no_css_selector = True )
1405+
1406+ spec = _get_output_spec ('scope' , dom_id = dom_id , contents = content , scope = scope , position = position )
1407+ return Output (spec )
1408+
1409+
13931410@safely_destruct_output_when_exp ('contents' )
13941411def output (* contents ):
13951412 """Placeholder of output
13961413
1414+ .. deprecated:: 1.5
1415+ See :ref:`User Guide <put_scope>` for new way to set css style for output.
1416+
13971417 ``output()`` can be passed in anywhere that ``put_xxx()`` can passed in. A handler it returned by ``output()``,
13981418 and after being output, the content can also be modified by the handler (See code example below).
13991419
@@ -1431,6 +1451,10 @@ def output(*contents):
14311451
14321452 """
14331453
1454+ import warnings
1455+ warnings .warn ("`pywebio.output.output()` is deprecated since v1.5 and will remove in the future version, "
1456+ "use `pywebio.output.put_scope()` instead" , DeprecationWarning , stacklevel = 2 )
1457+
14341458 class OutputHandler (Output ):
14351459 """
14361460 与 `Output` 的不同在于, 不会在销毁时(__del__)自动输出
@@ -1687,17 +1711,16 @@ def show_msg():
16871711clear_scope = clear
16881712
16891713
1690- def use_scope (name = None , clear = False , create_scope = True , ** scope_params ):
1691- """Open or enter a scope. Can be used as context manager and decorator.
1714+ def use_scope (name = None , clear = False , ** kwargs ):
1715+ """use_scope(name=None, clear=False)
1716+
1717+ Open or enter a scope. Can be used as context manager and decorator.
16921718
16931719 See :ref:`User manual - use_scope() <use_scope>`
16941720
16951721 :param str name: Scope name. If it is None, a globally unique scope name is generated.
16961722 (When used as context manager, the context manager will return the scope name)
16971723 :param bool clear: Whether to clear the contents of the scope before entering the scope.
1698- :param bool create_scope: Whether to create scope when scope does not exist.
1699- :param scope_params: Extra parameters passed to `set_scope()` when need to create scope.
1700- Only available when ``create_scope=True``.
17011724
17021725 :Usage:
17031726
@@ -1711,17 +1734,22 @@ def app():
17111734 put_xxx()
17121735
17131736 """
1737+ # For backward compatible
1738+ # :param bool create_scope: Whether to create scope when scope does not exist.
1739+ # :param scope_params: Extra parameters passed to `set_scope()` when need to create scope.
1740+ # Only available when ``create_scope=True``.
1741+ create_scope = kwargs .pop ('create_scope' , True )
1742+ scope_params = kwargs
1743+
17141744 if name is None :
17151745 name = random_str (10 )
17161746 else :
17171747 assert is_html_safe_value (name ), "Scope name only allow letter/digit/'_'/'-' char."
17181748
17191749 def before_enter ():
17201750 if create_scope :
1721- set_scope (name , ** scope_params )
1722-
1723- if clear :
1724- clear_scope (name )
1751+ if_exist = 'clear' if clear else None
1752+ set_scope (name , if_exist = if_exist , ** scope_params )
17251753
17261754 return use_scope_ (name = name , before_enter = before_enter )
17271755
0 commit comments