@@ -1034,35 +1034,36 @@ def put_loading(shape: str = 'border', color: str = 'dark', scope: str = None, p
1034
1034
1035
1035
## ----
1036
1036
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.
1038
1039
with put_loading():
1040
+ put_text("Start waiting...")
1039
1041
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")
1041
1043
1042
1044
## ----
1043
1045
# using style() to set the size of the loading prompt
1044
1046
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.
1045
1051
"""
1046
1052
assert shape in ('border' , 'grow' ), "shape must in ('border', 'grow')"
1047
1053
assert color in {'primary' , 'secondary' , 'success' , 'danger' , 'warning' , 'info' , 'light' , 'dark' }
1048
1054
1049
- html = """<div class="spinner-{shape} text-{color}" role="status">
1055
+ html = """<div><div class="spinner-{shape} text-{color}" role="status">
1050
1056
<span class="sr-only">Loading...</span>
1051
- </div>""" .format (shape = shape , color = color )
1057
+ </div></div> """ .format (shape = shape , color = color )
1052
1058
1053
1059
scope_name = random_str (10 )
1054
1060
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 ():
1061
1062
remove (scope_name )
1062
1063
return False # Propagate Exception
1063
1064
1064
1065
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 )
1066
1067
1067
1068
1068
1069
@safely_destruct_output_when_exp ('content' )
0 commit comments