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
| +---------------------------+------------------------------------------------------------+
85
85
| +---------------------------+------------------------------------------------------------+
86
86
| | `style`:sup:`*` | Customize the css style of output content |
87
87
+--------------------+---------------------------+------------------------------------------------------------+
88
- | Placeholder | `output`:sup:`*` | Placeholder of output |
89
- +--------------------+---------------------------+------------------------------------------------------------+
90
88
91
89
Output Scope
92
90
--------------
95
93
96
94
* :ref:`Use Guide: Output Scope <output_scope>`
97
95
98
- .. autofunction:: set_scope
96
+ .. autofunction:: put_scope
97
+ .. autofunction:: use_scope
99
98
.. autofunction:: get_scope
100
99
.. autofunction:: clear
101
100
.. autofunction:: remove
102
101
.. autofunction:: scroll_to
103
- .. autofunction:: use_scope
104
102
105
103
Content Outputting
106
104
-----------------------
207
205
.. autofunction:: put_grid
208
206
.. autofunction:: style
209
207
210
- Placeholder
211
- --------------
212
- .. autofunction:: output
213
208
214
209
"""
215
210
import html
232
227
233
228
logger = logging .getLogger (__name__ )
234
229
235
- __all__ = ['Position' , 'remove' , 'scroll_to' , 'put_tabs' ,
230
+ __all__ = ['Position' , 'remove' , 'scroll_to' , 'put_tabs' , 'put_scope' ,
236
231
'put_text' , 'put_html' , 'put_code' , 'put_markdown' , 'use_scope' , 'set_scope' , 'clear' , 'remove' ,
237
232
'put_table' , 'put_buttons' , 'put_image' , 'put_file' , 'PopupSize' , 'popup' , 'put_button' ,
238
233
'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
1390
1385
return put_widget (template = tpl , data = dict (contents = content ), scope = scope , position = position )
1391
1386
1392
1387
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
+
1393
1406
@safely_destruct_output_when_exp ('contents' )
1394
1407
def output (* contents ):
1395
1408
"""Placeholder of output
1396
1409
1410
+ .. deprecated:: 1.5
1411
+ See :ref:`User Guide <put_scope>` for new way to set css style for output.
1412
+
1397
1413
``output()`` can be passed in anywhere that ``put_xxx()`` can passed in. A handler it returned by ``output()``,
1398
1414
and after being output, the content can also be modified by the handler (See code example below).
1399
1415
@@ -1431,6 +1447,10 @@ def output(*contents):
1431
1447
1432
1448
"""
1433
1449
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
+
1434
1454
class OutputHandler (Output ):
1435
1455
"""
1436
1456
与 `Output` 的不同在于, 不会在销毁时(__del__)自动输出
@@ -1687,17 +1707,16 @@ def show_msg():
1687
1707
clear_scope = clear
1688
1708
1689
1709
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.
1692
1714
1693
1715
See :ref:`User manual - use_scope() <use_scope>`
1694
1716
1695
1717
:param str name: Scope name. If it is None, a globally unique scope name is generated.
1696
1718
(When used as context manager, the context manager will return the scope name)
1697
1719
: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
1720
1702
1721
:Usage:
1703
1722
@@ -1711,17 +1730,22 @@ def app():
1711
1730
put_xxx()
1712
1731
1713
1732
"""
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
+
1714
1740
if name is None :
1715
1741
name = random_str (10 )
1716
1742
else :
1717
1743
assert is_html_safe_value (name ), "Scope name only allow letter/digit/'_'/'-' char."
1718
1744
1719
1745
def before_enter ():
1720
1746
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 )
1725
1749
1726
1750
return use_scope_ (name = name , before_enter = before_enter )
1727
1751
0 commit comments