Skip to content

Commit 1a80a70

Browse files
committed
add PageClosedException catch in hold() and pin_wait_change()
1 parent 6ec21db commit 1a80a70

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

pywebio/output.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
18551855
get_current_session().pop_page()
18561856
if isinstance(exc_val, PageClosedException): # page is close by app user
18571857
if self.silent_quit:
1858-
# supress PageClosedException Exception
1858+
# suppress PageClosedException Exception
18591859
return True
18601860
else:
18611861
send_msg('close_page', dict(page_id=self.page_id))

pywebio/pin.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@
130130
from pywebio.output import OutputPosition, Output
131131
from pywebio.output import _get_output_spec
132132
from .io_ctrl import send_msg, single_input_kwargs, output_register_callback
133-
from .session import next_client_event, chose_impl
133+
from .session import next_client_event, chose_impl, get_current_session
134134
from .utils import check_dom_name_value
135+
from .exceptions import PageClosedException
135136

136137
_pin_name_chars = set(string.ascii_letters + string.digits + '_-')
137138

@@ -227,6 +228,12 @@ def put_actions(name, *, label='', buttons=None, help_text=None,
227228
@chose_impl
228229
def get_client_val():
229230
res = yield next_client_event()
231+
if res['event'] == 'page_close':
232+
current_page = get_current_session().get_page_id(check_active=False)
233+
closed_page = res['data']
234+
if closed_page == current_page:
235+
raise PageClosedException
236+
230237
assert res['event'] == 'js_yield', "Internal Error, please report this bug on " \
231238
"https://github.com/wang0618/PyWebIO/issues"
232239
return res['data']

pywebio/session/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def show():
168168
from .base import Session
169169
from .coroutinebased import CoroutineBasedSession
170170
from .threadbased import ThreadBasedSession, ScriptModeSession
171-
from ..exceptions import SessionNotFoundException, SessionException
171+
from ..exceptions import SessionNotFoundException, SessionException, PageClosedException
172172
from ..utils import iscoroutinefunction, isgeneratorfunction, run_as_function, to_coroutine, ObjectDictProxy, \
173173
ReadOnlyObjectDict
174174

@@ -293,7 +293,7 @@ def next_client_event():
293293

294294
@chose_impl
295295
def hold():
296-
"""Keep the session alive until the browser page is closed by user.
296+
"""Hold and wait the browser page is closed by user.
297297
298298
.. attention::
299299
@@ -315,6 +315,8 @@ def hold():
315315
yield next_client_event()
316316
except SessionException:
317317
return
318+
except PageClosedException:
319+
return
318320

319321

320322
def download(name, content):

pywebio/session/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def get_page_id(self, check_active=True):
113113
if page_id not in self.active_page[task_id] and check_active:
114114
raise PageClosedException(
115115
"The page is closed by app user, "
116-
"you see this exception mostly because you set `silent_quit=False` in `pywebio.output.page()`"
116+
"set `silent_quit=True` in `pywebio.output.page()` to suppress this error"
117117
)
118118

119119
return page_id

0 commit comments

Comments
 (0)