diff --git a/mesop/env/env.py b/mesop/env/env.py index ccc5b642f..1feb753ec 100644 --- a/mesop/env/env.py +++ b/mesop/env/env.py @@ -16,7 +16,6 @@ raise MesopDeveloperException( f"MESOP_APP_BASE_PATH is not a valid directory: {MESOP_APP_BASE_PATH}" ) - print(f"MESOP_APP_BASE_PATH set to {MESOP_APP_BASE_PATH}") def get_app_base_path() -> str: @@ -34,15 +33,9 @@ def get_app_base_path() -> str: ) if MESOP_WEBSOCKETS_ENABLED: - print("Experiment enabled: MESOP_WEBSOCKETS_ENABLED") - print("Auto-enabling MESOP_CONCURRENT_UPDATES_ENABLED") MESOP_CONCURRENT_UPDATES_ENABLED = True -elif MESOP_CONCURRENT_UPDATES_ENABLED: - print("Experiment enabled: MESOP_CONCURRENT_UPDATES_ENABLED") + EXPERIMENTAL_EDITOR_TOOLBAR_ENABLED = ( os.environ.get("MESOP_EXPERIMENTAL_EDITOR_TOOLBAR", "false").lower() == "true" ) - -if EXPERIMENTAL_EDITOR_TOOLBAR_ENABLED: - print("Experiment enabled: EXPERIMENTAL_EDITOR_TOOLBAR_ENABLED") diff --git a/mesop/labs/text_to_text.py b/mesop/labs/text_to_text.py index 08baf2e6d..64f01f5f7 100644 --- a/mesop/labs/text_to_text.py +++ b/mesop/labs/text_to_text.py @@ -2,6 +2,7 @@ from typing import Callable, Generator, Literal, cast import mesop as me +from mesop.warn import warn @me.stateclass @@ -30,9 +31,7 @@ def text_io( - "append": Concatenates each new piece of text to the existing output. - "replace": Replaces the existing output with each new piece of text. """ - print( - "\033[93m[warning]\033[0m text_io is deprecated, use text_to_text instead" - ) + warn("text_io is deprecated, use text_to_text instead") text_to_text(transform=transform, title=title, transform_mode=transform_mode) diff --git a/mesop/server/server.py b/mesop/server/server.py index 9c463c73b..0f119bc11 100644 --- a/mesop/server/server.py +++ b/mesop/server/server.py @@ -1,4 +1,5 @@ import base64 +import logging import secrets import threading from typing import Generator, Sequence @@ -16,6 +17,9 @@ from mesop.component_helpers import diff_component from mesop.editor.component_configs import get_component_configs from mesop.env.env import ( + EXPERIMENTAL_EDITOR_TOOLBAR_ENABLED, + MESOP_APP_BASE_PATH, + MESOP_CONCURRENT_UPDATES_ENABLED, MESOP_WEBSOCKETS_ENABLED, ) from mesop.events import LoadEvent @@ -37,15 +41,28 @@ UI_PATH = "/__ui__" +logger = logging.getLogger(__name__) + def configure_flask_app( *, prod_mode: bool = True, exceptions_to_propagate: Sequence[type] = () ) -> Flask: + if MESOP_WEBSOCKETS_ENABLED: + logger.info( + "Experiment enabled: MESOP_WEBSOCKETS_ENABLED (auto-enables MESOP_CONCURRENT_UPDATES_ENABLED)" + ) + elif MESOP_CONCURRENT_UPDATES_ENABLED: + logger.info("Experiment enabled: MESOP_CONCURRENT_UPDATES_ENABLED") + if EXPERIMENTAL_EDITOR_TOOLBAR_ENABLED: + logger.info("Experiment enabled: EXPERIMENTAL_EDITOR_TOOLBAR_ENABLED") + + if MESOP_APP_BASE_PATH: + logger.info(f"MESOP_APP_BASE_PATH set to {MESOP_APP_BASE_PATH}") + static_folder = get_static_folder() static_url_path = get_static_url_path() if static_folder and static_url_path: - print(f"Static folder enabled: {static_folder}") - + logger.info(f"Static folder enabled: {static_folder}") flask_app = Flask( __name__, static_folder=static_folder, @@ -103,7 +120,7 @@ def render_loop( ) yield serialize(data) except Exception as e: - print(e) + logging.error(e) if e in exceptions_to_propagate: raise e yield from yield_errors( @@ -304,7 +321,7 @@ def ws_generate_data(ws, ui_request): decoded_message = base64.urlsafe_b64decode(message) ui_request.ParseFromString(decoded_message) except Exception as parse_error: - print("Failed to parse message:", parse_error) + logging.error("Failed to parse message: %s", parse_error) continue # Skip processing this message # Start a new thread so we can handle multiple @@ -320,7 +337,7 @@ def ws_generate_data(ws, ui_request): thread.start() except Exception as e: - print("WebSocket error:", e) + logging.error("WebSocket error: %s", e) finally: # Clean up context when connection closes if hasattr(request, "websocket_session_id"): diff --git a/mesop/warn/warn.py b/mesop/warn/warn.py index feed1d896..1b9dc5287 100644 --- a/mesop/warn/warn.py +++ b/mesop/warn/warn.py @@ -1,2 +1,7 @@ +import logging + +logger = logging.getLogger(__name__) + + def warn(message: str) -> None: - print("\033[93m[warning]\033[0m " + message) + logger.warning("\033[93m[warning]\033[0m " + message)