From ef0ed773d492541f266ac7bf80d24aeadb97c307 Mon Sep 17 00:00:00 2001 From: Will Chen Date: Mon, 29 Jul 2024 19:38:45 -0700 Subject: [PATCH 1/4] Disable has_served_traffic safety checks for debug mode --- mesop/runtime/runtime.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mesop/runtime/runtime.py b/mesop/runtime/runtime.py index 066ea489e..4a2eead8b 100644 --- a/mesop/runtime/runtime.py +++ b/mesop/runtime/runtime.py @@ -61,7 +61,11 @@ def context(self) -> Context: return g._mesop_context def create_context(self) -> Context: - self._has_served_traffic = True + # In debug mode, do not set _has_served_traffic to True, + # otherwise this breaks Colab / notebok use cases where users + # will want to register pages after traffic has been served. + if not self.debug_mode: + self._has_served_traffic = True if len(self._state_classes) == 0: states = {EmptyState: EmptyState()} else: From 22058c01b3280102bcf3261aba3302b980efca33 Mon Sep 17 00:00:00 2001 From: Will Chen Date: Tue, 30 Jul 2024 09:30:08 -0700 Subject: [PATCH 2/4] switch to detecting colab/ipython --- mesop/runtime/runtime.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mesop/runtime/runtime.py b/mesop/runtime/runtime.py index 4a2eead8b..c13dc4320 100644 --- a/mesop/runtime/runtime.py +++ b/mesop/runtime/runtime.py @@ -4,6 +4,7 @@ from flask import g import mesop.protos.ui_pb2 as pb +from mesop.colab import colab_utils from mesop.events import LoadEvent, MesopEvent from mesop.exceptions import MesopDeveloperException, MesopUserException from mesop.key import Key @@ -61,10 +62,12 @@ def context(self) -> Context: return g._mesop_context def create_context(self) -> Context: - # In debug mode, do not set _has_served_traffic to True, - # otherwise this breaks Colab / notebok use cases where users + # In IPython (e.g. Colab/Jupyter notebook envs), + # do not set _has_served_traffic to True, + # otherwise this breaks the iterative development where developers # will want to register pages after traffic has been served. - if not self.debug_mode: + # Unlike CLI (w/ hot reload), in notebook envs, runtime is *not* reset. + if not colab_utils.is_running_ipython(): self._has_served_traffic = True if len(self._state_classes) == 0: states = {EmptyState: EmptyState()} From f51b7703d4b253b070527ff0931b3b98318eae77 Mon Sep 17 00:00:00 2001 From: Will Chen Date: Tue, 30 Jul 2024 09:36:24 -0700 Subject: [PATCH 3/4] extract colab_utils to utils so runtime can depend on it without a cycle --- mesop/colab/colab_run.py | 2 +- mesop/colab/colab_run_test.py | 2 +- mesop/colab/colab_show.py | 2 +- mesop/runtime/BUILD | 1 + mesop/runtime/runtime.py | 2 +- mesop/{colab => utils}/colab_utils.py | 0 6 files changed, 5 insertions(+), 4 deletions(-) rename mesop/{colab => utils}/colab_utils.py (100%) diff --git a/mesop/colab/colab_run.py b/mesop/colab/colab_run.py index 17df54a9f..921bdd217 100644 --- a/mesop/colab/colab_run.py +++ b/mesop/colab/colab_run.py @@ -2,12 +2,12 @@ from absl import flags -from mesop.colab import colab_utils from mesop.runtime import enable_debug_mode from mesop.server.constants import EDITOR_PACKAGE_PATH, PROD_PACKAGE_PATH from mesop.server.logging import log_startup from mesop.server.server import configure_flask_app from mesop.server.static_file_serving import configure_static_file_serving +from mesop.utils import colab_utils from mesop.utils.host_util import get_default_host diff --git a/mesop/colab/colab_run_test.py b/mesop/colab/colab_run_test.py index 57209ebe5..65d0aec1e 100644 --- a/mesop/colab/colab_run_test.py +++ b/mesop/colab/colab_run_test.py @@ -3,9 +3,9 @@ import pytest -from mesop.colab import colab_utils from mesop.colab.colab_run import colab_run from mesop.runtime import runtime +from mesop.utils import colab_utils class FakeThread: diff --git a/mesop/colab/colab_show.py b/mesop/colab/colab_show.py index 9ce7a0b0a..1aeab4605 100644 --- a/mesop/colab/colab_show.py +++ b/mesop/colab/colab_show.py @@ -1,4 +1,4 @@ -from mesop.colab import colab_utils +from mesop.utils import colab_utils def colab_show( diff --git a/mesop/runtime/BUILD b/mesop/runtime/BUILD index caa84ba62..754b574d5 100644 --- a/mesop/runtime/BUILD +++ b/mesop/runtime/BUILD @@ -12,5 +12,6 @@ py_library( "//mesop/exceptions", "//mesop/protos:ui_py_pb2", "//mesop/security", + "//mesop/utils", ] + THIRD_PARTY_PY_FLASK, ) diff --git a/mesop/runtime/runtime.py b/mesop/runtime/runtime.py index c13dc4320..6d04b9138 100644 --- a/mesop/runtime/runtime.py +++ b/mesop/runtime/runtime.py @@ -4,11 +4,11 @@ from flask import g import mesop.protos.ui_pb2 as pb -from mesop.colab import colab_utils from mesop.events import LoadEvent, MesopEvent from mesop.exceptions import MesopDeveloperException, MesopUserException from mesop.key import Key from mesop.security.security_policy import SecurityPolicy +from mesop.utils import colab_utils from mesop.utils.backoff import exponential_backoff from .context import Context diff --git a/mesop/colab/colab_utils.py b/mesop/utils/colab_utils.py similarity index 100% rename from mesop/colab/colab_utils.py rename to mesop/utils/colab_utils.py From 89acf261d07ac3aeab0e0c19af7c7bbddba68ca5 Mon Sep 17 00:00:00 2001 From: Will Chen Date: Tue, 30 Jul 2024 09:41:53 -0700 Subject: [PATCH 4/4] change the safety check --- mesop/runtime/runtime.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/mesop/runtime/runtime.py b/mesop/runtime/runtime.py index 6d04b9138..6d4df8a9c 100644 --- a/mesop/runtime/runtime.py +++ b/mesop/runtime/runtime.py @@ -62,12 +62,14 @@ def context(self) -> Context: return g._mesop_context def create_context(self) -> Context: - # In IPython (e.g. Colab/Jupyter notebook envs), - # do not set _has_served_traffic to True, - # otherwise this breaks the iterative development where developers + # If running in prod mode, *always* enable the has served traffic safety check. + # If running in debug mode, only enable the has served traffic safety check if + # app is not running in IPython / notebook environments. + # + # We don't want to break iterative development where notebook app developers # will want to register pages after traffic has been served. # Unlike CLI (w/ hot reload), in notebook envs, runtime is *not* reset. - if not colab_utils.is_running_ipython(): + if not self.debug_mode or not colab_utils.is_running_ipython(): self._has_served_traffic = True if len(self._state_classes) == 0: states = {EmptyState: EmptyState()}