Skip to content

Commit 6f2660a

Browse files
committed
the output inside the put_loading() context will also been removed after the context block exits.
1 parent 884772a commit 6f2660a

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

pywebio/io_ctrl.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ def __init__(self, spec, on_embed=None):
7676
self.enabled_context_manager = False
7777
self.container_selector = None
7878
self.container_dom_id = None # todo: this name is ambiguous, rename it to `scope_name` or others
79-
self.custom_enter = None
80-
self.custom_exit = None
79+
self.after_exit = None
8180

8281
# Try to make sure current session exist.
8382
# If we leave the session interaction in `Output.__del__`,
@@ -86,20 +85,16 @@ def __init__(self, spec, on_embed=None):
8685
# See also: https://github.com/pywebio/PyWebIO/issues/243
8786
get_current_session()
8887

89-
def enable_context_manager(self, container_selector=None, container_dom_id=None, custom_enter=None,
90-
custom_exit=None):
88+
def enable_context_manager(self, container_selector=None, container_dom_id=None, after_exit=None):
9189
self.enabled_context_manager = True
9290
self.container_selector = container_selector
9391
self.container_dom_id = container_dom_id
94-
self.custom_enter = custom_enter
95-
self.custom_exit = custom_exit
92+
self.after_exit = after_exit
9693
return self
9794

9895
def __enter__(self):
9996
if not self.enabled_context_manager:
10097
raise RuntimeError("This output function can't be used as context manager!")
101-
if self.custom_enter:
102-
return self.custom_enter(self)
10398

10499
self.container_dom_id = self.container_dom_id or random_str(10)
105100
self.spec['container_selector'] = self.container_selector
@@ -114,10 +109,9 @@ def __exit__(self, exc_type, exc_val, exc_tb):
114109
it means that the context manager can handle the exception,
115110
so that the with statement terminates the propagation of the exception
116111
"""
117-
if self.custom_exit:
118-
return self.custom_exit(self, exc_type=exc_type, exc_val=exc_val, exc_tb=exc_tb)
119-
120112
get_current_session().pop_scope()
113+
if self.after_exit:
114+
self.after_exit()
121115
return False # Propagate Exception
122116

123117
def embed_data(self):

pywebio/output.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,35 +1034,36 @@ def put_loading(shape: str = 'border', color: str = 'dark', scope: str = None, p
10341034
10351035
## ----
10361036
import time # ..demo-only
1037-
# Use as context manager, the loading prompt will disappear automatically when the context block exits.
1037+
# The loading prompt and the output inside the context will disappear
1038+
# automatically when the context block exits.
10381039
with put_loading():
1040+
put_text("Start waiting...")
10391041
time.sleep(3) # Some time-consuming operations
1040-
put_text("The answer of the universe is 42")
1042+
put_text("The answer of the universe is 42")
10411043
10421044
## ----
10431045
# using style() to set the size of the loading prompt
10441046
put_loading().style('width:4rem; height:4rem')
1047+
1048+
.. versionchanged:: 1.8
1049+
when use `put_loading()` as context manager, the output inside the context will also been removed
1050+
after the context block exits.
10451051
"""
10461052
assert shape in ('border', 'grow'), "shape must in ('border', 'grow')"
10471053
assert color in {'primary', 'secondary', 'success', 'danger', 'warning', 'info', 'light', 'dark'}
10481054

1049-
html = """<div class="spinner-{shape} text-{color}" role="status">
1055+
html = """<div><div class="spinner-{shape} text-{color}" role="status">
10501056
<span class="sr-only">Loading...</span>
1051-
</div>""".format(shape=shape, color=color)
1057+
</div></div>""".format(shape=shape, color=color)
10521058

10531059
scope_name = random_str(10)
10541060

1055-
def enter(self):
1056-
self.spec['container_dom_id'] = scope2dom(scope_name, no_css_selector=True)
1057-
self.send()
1058-
return scope_name
1059-
1060-
def exit_(self, exc_type, exc_val, exc_tb):
1061+
def after_exit():
10611062
remove(scope_name)
10621063
return False # Propagate Exception
10631064

10641065
return put_html(html, sanitize=False, scope=scope, position=position). \
1065-
enable_context_manager(custom_enter=enter, custom_exit=exit_)
1066+
enable_context_manager(container_dom_id=scope_name, after_exit=after_exit)
10661067

10671068

10681069
@safely_destruct_output_when_exp('content')

0 commit comments

Comments
 (0)