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

chore: fail gracefully when ui.py was not generated #565

Merged
merged 2 commits into from
Sep 19, 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
26 changes: 24 additions & 2 deletions src/writer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import importlib.metadata
import logging
import textwrap
from types import ModuleType
from typing import Any, Dict, List, Optional, Type, TypeVar, Union, cast

Expand All @@ -20,7 +22,24 @@
from writer.core import (
writerproperty as property,
)
from writer.ui import WriterUIManager

try:
from writer.ui import WriterUIManager
PROPER_UI_INIT = True
except ModuleNotFoundError:
logging.error(
textwrap.dedent(
"""\
\x1b[31;20mError: Failed to import `writer.ui` module.
This indicates that the Writer Framework was not built properly.
Please refer to CONTRIBUTING.md for instructions to resolve this issue.\x1b[0m"""
)
)
PROPER_UI_INIT = False

def _get_ui_runtime_error_message() -> str:
return "UI module is unavailable – Writer Framework is not properly built. " + \
"Please refer to CONTRIBUTING.md for instructions to resolve this issue."

VERSION = importlib.metadata.version("writer")

Expand Down Expand Up @@ -72,7 +91,10 @@ def init_ui() -> WriterUIManager:
>>> with ui.Page({"key": "hello"}):
>>> ui.Text({"text": "Hello pigeons"})
"""
return WriterUIManager()
if PROPER_UI_INIT:
return WriterUIManager()
else:
raise RuntimeError(_get_ui_runtime_error_message())


def init_state(raw_state: Dict[str, Any], schema: Optional[Type[S]] = None) -> Union[S, WriterState]:
Expand Down
8 changes: 6 additions & 2 deletions src/writer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2529,8 +2529,12 @@ def _event_handler_session_info() -> Dict[str, Any]:
return session_info

def _event_handler_ui_manager():
from writer.ui import WriterUIManager
return WriterUIManager()
from writer import PROPER_UI_INIT, _get_ui_runtime_error_message
if PROPER_UI_INIT:
from writer.ui import WriterUIManager
return WriterUIManager()
else:
raise RuntimeError(_get_ui_runtime_error_message())


def _split_record_as_pandas_record_and_index(param: dict, index_columns: list) -> Tuple[dict, tuple]:
Expand Down
Loading