17
17
+--------------------+---------------------------+------------------------------------------------------------+
18
18
| | **Name** | **Description** |
19
19
+--------------------+---------------------------+------------------------------------------------------------+
20
- | Output Scope | `set_scope ` | Create a new scope |
20
+ | Output Scope | `put_scope ` | Create a new scope |
21
21
| +---------------------------+------------------------------------------------------------+
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 |
23
25
| +---------------------------+------------------------------------------------------------+
24
26
| | `clear` | Clear the content of scope |
25
27
| +---------------------------+------------------------------------------------------------+
26
28
| | `remove` | Remove the scope |
27
29
| +---------------------------+------------------------------------------------------------+
28
30
| | `scroll_to` | Scroll the page to the scope |
29
- | +---------------------------+------------------------------------------------------------+
30
- | | `use_scope`:sup:`†` | Open or enter a scope |
31
31
+--------------------+---------------------------+------------------------------------------------------------+
32
32
| Content Outputting | `put_text` | Output plain text |
33
33
| +---------------------------+------------------------------------------------------------+
95
95
96
96
* :ref:`Use Guide: Output Scope <output_scope>`
97
97
98
- .. autofunction:: set_scope
98
+ .. autofunction:: put_scope
99
+ .. autofunction:: use_scope
99
100
.. autofunction:: get_scope
100
101
.. autofunction:: clear
101
102
.. autofunction:: remove
102
103
.. autofunction:: scroll_to
103
- .. autofunction:: use_scope
104
104
105
105
Content Outputting
106
106
-----------------------
232
232
233
233
logger = logging .getLogger (__name__ )
234
234
235
- __all__ = ['Position' , 'remove' , 'scroll_to' , 'put_tabs' ,
235
+ __all__ = ['Position' , 'remove' , 'scroll_to' , 'put_tabs' , 'put_scope' ,
236
236
'put_text' , 'put_html' , 'put_code' , 'put_markdown' , 'use_scope' , 'set_scope' , 'clear' , 'remove' ,
237
237
'put_table' , 'put_buttons' , 'put_image' , 'put_file' , 'PopupSize' , 'popup' , 'put_button' ,
238
238
'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
1390
1390
return put_widget (template = tpl , data = dict (contents = content ), scope = scope , position = position )
1391
1391
1392
1392
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
+
1393
1410
@safely_destruct_output_when_exp ('contents' )
1394
1411
def output (* contents ):
1395
1412
"""Placeholder of output
1396
1413
1414
+ .. deprecated:: 1.5
1415
+ See :ref:`User Guide <put_scope>` for new way to set css style for output.
1416
+
1397
1417
``output()`` can be passed in anywhere that ``put_xxx()`` can passed in. A handler it returned by ``output()``,
1398
1418
and after being output, the content can also be modified by the handler (See code example below).
1399
1419
@@ -1431,6 +1451,10 @@ def output(*contents):
1431
1451
1432
1452
"""
1433
1453
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
+
1434
1458
class OutputHandler (Output ):
1435
1459
"""
1436
1460
与 `Output` 的不同在于, 不会在销毁时(__del__)自动输出
@@ -1687,17 +1711,16 @@ def show_msg():
1687
1711
clear_scope = clear
1688
1712
1689
1713
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.
1692
1718
1693
1719
See :ref:`User manual - use_scope() <use_scope>`
1694
1720
1695
1721
:param str name: Scope name. If it is None, a globally unique scope name is generated.
1696
1722
(When used as context manager, the context manager will return the scope name)
1697
1723
: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``.
1701
1724
1702
1725
:Usage:
1703
1726
@@ -1711,17 +1734,22 @@ def app():
1711
1734
put_xxx()
1712
1735
1713
1736
"""
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
+
1714
1744
if name is None :
1715
1745
name = random_str (10 )
1716
1746
else :
1717
1747
assert is_html_safe_value (name ), "Scope name only allow letter/digit/'_'/'-' char."
1718
1748
1719
1749
def before_enter ():
1720
1750
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 )
1725
1753
1726
1754
return use_scope_ (name = name , before_enter = before_enter )
1727
1755
0 commit comments