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| +---------------------------+------------------------------------------------------------+
8585| +---------------------------+------------------------------------------------------------+
8686| | `style`:sup:`*` | Customize the css style of output content |
8787+--------------------+---------------------------+------------------------------------------------------------+
88- | Placeholder | `output`:sup:`*` | Placeholder of output |
89- +--------------------+---------------------------+------------------------------------------------------------+
9088
9189Output Scope
9290--------------
9593
9694 * :ref:`Use Guide: Output Scope <output_scope>`
9795
98- .. autofunction:: set_scope
96+ .. autofunction:: put_scope
97+ .. autofunction:: use_scope
9998.. autofunction:: get_scope
10099.. autofunction:: clear
101100.. autofunction:: remove
102101.. autofunction:: scroll_to
103- .. autofunction:: use_scope
104102
105103Content Outputting
106104-----------------------
207205.. autofunction:: put_grid
208206.. autofunction:: style
209207
210- Placeholder
211- --------------
212- .. autofunction:: output
213208
214209"""
215210import html
232227
233228logger = logging .getLogger (__name__ )
234229
235- __all__ = ['Position' , 'remove' , 'scroll_to' , 'put_tabs' ,
230+ __all__ = ['Position' , 'remove' , 'scroll_to' , 'put_tabs' , 'put_scope' ,
236231 'put_text' , 'put_html' , 'put_code' , 'put_markdown' , 'use_scope' , 'set_scope' , 'clear' , 'remove' ,
237232 'put_table' , 'put_buttons' , 'put_image' , 'put_file' , 'PopupSize' , 'popup' , 'put_button' ,
238233 'close_popup' , 'put_widget' , 'put_collapse' , 'put_link' , 'put_scrollable' , 'style' , 'put_column' ,
@@ -1390,10 +1385,31 @@ def put_grid(content, cell_width='auto', cell_height='auto', cell_widths=None, c
13901385 return put_widget (template = tpl , data = dict (contents = content ), scope = scope , position = position )
13911386
13921387
1388+ @safely_destruct_output_when_exp ('content' )
1389+ def put_scope (name , content = [], scope = None , position = OutputPosition .BOTTOM ) -> Output :
1390+ """Output a scope
1391+
1392+ :param str name:
1393+ :param list/put_xxx() content: The initial content of the scope, can be ``put_xxx()`` or a list of it.
1394+ :param int scope, position: Those arguments have the same meaning as for `put_text()`
1395+ """
1396+ if not isinstance (content , list ):
1397+ content = [content ]
1398+
1399+ assert is_html_safe_value (name ), "Scope name only allow letter/digit/'_'/'-' char."
1400+ dom_id = scope2dom (name , no_css_selector = True )
1401+
1402+ spec = _get_output_spec ('scope' , dom_id = dom_id , contents = content , scope = scope , position = position )
1403+ return Output (spec )
1404+
1405+
13931406@safely_destruct_output_when_exp ('contents' )
13941407def output (* contents ):
13951408 """Placeholder of output
13961409
1410+ .. deprecated:: 1.5
1411+ See :ref:`User Guide <put_scope>` for new way to set css style for output.
1412+
13971413 ``output()`` can be passed in anywhere that ``put_xxx()`` can passed in. A handler it returned by ``output()``,
13981414 and after being output, the content can also be modified by the handler (See code example below).
13991415
@@ -1431,6 +1447,10 @@ def output(*contents):
14311447
14321448 """
14331449
1450+ import warnings
1451+ warnings .warn ("`pywebio.output.output()` is deprecated since v1.5 and will remove in the future version, "
1452+ "use `pywebio.output.put_scope()` instead" , DeprecationWarning , stacklevel = 2 )
1453+
14341454 class OutputHandler (Output ):
14351455 """
14361456 与 `Output` 的不同在于, 不会在销毁时(__del__)自动输出
@@ -1687,17 +1707,16 @@ def show_msg():
16871707clear_scope = clear
16881708
16891709
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.
1710+ def use_scope (name = None , clear = False , ** kwargs ):
1711+ """use_scope(name=None, clear=False)
1712+
1713+ Open or enter a scope. Can be used as context manager and decorator.
16921714
16931715 See :ref:`User manual - use_scope() <use_scope>`
16941716
16951717 :param str name: Scope name. If it is None, a globally unique scope name is generated.
16961718 (When used as context manager, the context manager will return the scope name)
16971719 :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``.
17011720
17021721 :Usage:
17031722
@@ -1711,17 +1730,22 @@ def app():
17111730 put_xxx()
17121731
17131732 """
1733+ # For backward compatible
1734+ # :param bool create_scope: Whether to create scope when scope does not exist.
1735+ # :param scope_params: Extra parameters passed to `set_scope()` when need to create scope.
1736+ # Only available when ``create_scope=True``.
1737+ create_scope = kwargs .pop ('create_scope' , True )
1738+ scope_params = kwargs
1739+
17141740 if name is None :
17151741 name = random_str (10 )
17161742 else :
17171743 assert is_html_safe_value (name ), "Scope name only allow letter/digit/'_'/'-' char."
17181744
17191745 def before_enter ():
17201746 if create_scope :
1721- set_scope (name , ** scope_params )
1722-
1723- if clear :
1724- clear_scope (name )
1747+ if_exist = 'clear' if clear else None
1748+ set_scope (name , if_exist = if_exist , ** scope_params )
17251749
17261750 return use_scope_ (name = name , before_enter = before_enter )
17271751
0 commit comments