Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate print calls to logging #1083

Merged
merged 5 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions mesop/env/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
5 changes: 2 additions & 3 deletions mesop/labs/text_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Callable, Generator, Literal, cast

import mesop as me
from mesop.warn import warn


@me.stateclass
Expand Down Expand Up @@ -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)


Expand Down
27 changes: 22 additions & 5 deletions mesop/server/server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import logging
import secrets
import threading
from typing import Generator, Sequence
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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"):
Expand Down
7 changes: 6 additions & 1 deletion mesop/warn/warn.py
Original file line number Diff line number Diff line change
@@ -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)
Loading