Skip to content

Commit

Permalink
Merge pull request #565 from mmikita95/feat-ungenerated-ui-warning
Browse files Browse the repository at this point in the history
chore: fail gracefully when ui.py was not generated
  • Loading branch information
ramedina86 authored Sep 19, 2024
2 parents 07c120a + 1be78b8 commit 57a580e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
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

0 comments on commit 57a580e

Please sign in to comment.