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

Add enable_deck to task decorator #1898

Merged
merged 2 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 14 additions & 3 deletions flytekit/core/base_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ def __init__(
task_config: Optional[T],
interface: Optional[Interface] = None,
environment: Optional[Dict[str, str]] = None,
disable_deck: bool = True,
disable_deck: Optional[bool] = None,
enable_deck: Optional[bool] = None,
**kwargs,
):
"""
Expand All @@ -423,7 +424,8 @@ def __init__(
signature of the task
environment (Optional[Dict[str, str]]): Any environment variables that should be supplied during the
execution of the task. Supplied as a dictionary of key/value pairs
disable_deck (bool): If true, this task will not output deck html file
disable_deck (bool): (deprecated) If true, this task will not output deck html file
enable_deck (bool): If true, this task will output deck html file
"""
super().__init__(
task_type=task_type,
Expand All @@ -434,7 +436,16 @@ def __init__(
self._python_interface = interface if interface else Interface()
self._environment = environment if environment else {}
self._task_config = task_config
self._disable_deck = disable_deck

# Confirm that disable_deck and enable_deck do not contradict each other
if disable_deck is not None and enable_deck is not None:
raise ValueError("disable_deck and enable_deck cannot both be set at the same time")

if enable_deck is not None:
self._disable_deck = not enable_deck
else:
assert disable_deck is not None
self._disable_deck = disable_deck
if self._python_interface.docstring:
if self.docs is None:
self._docs = Documentation(
Expand Down
13 changes: 9 additions & 4 deletions flytekit/core/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def task(
execution_mode: PythonFunctionTask.ExecutionBehavior = ...,
task_resolver: Optional[TaskResolverMixin] = ...,
docs: Optional[Documentation] = ...,
disable_deck: bool = ...,
disable_deck: Optional[bool] = ...,
enable_deck: Optional[bool] = ...,
pod_template: Optional["PodTemplate"] = ...,
pod_template_name: Optional[str] = ...,
) -> Callable[[Callable[..., FuncOut]], PythonFunctionTask[T]]:
Expand All @@ -124,7 +125,8 @@ def task(
execution_mode: PythonFunctionTask.ExecutionBehavior = ...,
task_resolver: Optional[TaskResolverMixin] = ...,
docs: Optional[Documentation] = ...,
disable_deck: bool = ...,
disable_deck: Optional[bool] = ...,
enable_deck: Optional[bool] = ...,
pod_template: Optional["PodTemplate"] = ...,
pod_template_name: Optional[str] = ...,
) -> Union[PythonFunctionTask[T], Callable[..., FuncOut]]:
Expand All @@ -149,7 +151,8 @@ def task(
execution_mode: PythonFunctionTask.ExecutionBehavior = PythonFunctionTask.ExecutionBehavior.DEFAULT,
task_resolver: Optional[TaskResolverMixin] = None,
docs: Optional[Documentation] = None,
disable_deck: bool = True,
disable_deck: Optional[bool] = None,
enable_deck: Optional[bool] = None,
pod_template: Optional["PodTemplate"] = None,
pod_template_name: Optional[str] = None,
) -> Union[Callable[[Callable[..., FuncOut]], PythonFunctionTask[T]], PythonFunctionTask[T], Callable[..., FuncOut]]:
Expand Down Expand Up @@ -240,7 +243,8 @@ def foo2():
may change based on the backend provider.
:param execution_mode: This is mainly for internal use. Please ignore. It is filled in automatically.
:param task_resolver: Provide a custom task resolver.
:param disable_deck: If true, this task will not output deck html file
:param disable_deck: (deprecated) If true, this task will not output deck html file
:param enable_deck: If true, this task will output deck html file
:param docs: Documentation about this task
:param pod_template: Custom PodTemplate for this task.
:param pod_template_name: The name of the existing PodTemplate resource which will be used in this task.
Expand Down Expand Up @@ -269,6 +273,7 @@ def wrapper(fn: Callable[..., Any]) -> PythonFunctionTask[T]:
execution_mode=execution_mode,
task_resolver=task_resolver,
disable_deck=disable_deck,
enable_deck=enable_deck,
docs=docs,
pod_template=pod_template,
pod_template_name=pod_template_name,
Expand Down
42 changes: 30 additions & 12 deletions tests/flytekit/unit/deck/test_deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,46 @@ def t1(a: int) -> str:


@pytest.mark.parametrize(
"disable_deck, expected_decks",
"enable_deck,disable_deck, expected_decks, expect_error",
[
(None, 2), # default deck and time line deck
(False, 4), # default deck and time line deck + input and output decks
(True, 2), # default deck and time line deck
(None, None, 2, False), # default deck and time line deck
(None, False, 4, False), # default deck and time line deck + input and output decks
(None, True, 2, False), # default deck and time line deck
(True, None, 4, False), # default deck and time line deck + input and output decks
(False, None, 2, False), # default deck and time line deck
(True, True, -1, True), # Set both disable_deck and enable_deck to True and confirm that it fails
(False, False, -1, True), # Set both disable_deck and enable_deck to False and confirm that it fails
],
)
def test_deck_pandas_dataframe(disable_deck, expected_decks):
def test_deck_pandas_dataframe(enable_deck, disable_deck, expected_decks, expect_error):
ctx = FlyteContextManager.current_context()

kwargs = {}
if disable_deck is not None:
kwargs["disable_deck"] = disable_deck

@task(**kwargs)
def t_df(a: str) -> int:
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
flytekit.current_context().default_deck.append(TopFrameRenderer().to_html(df))
return int(a)
if enable_deck is not None:
kwargs["enable_deck"] = enable_deck

t_df(a="42")
assert len(ctx.user_space_params.decks) == expected_decks
if expect_error:
with pytest.raises(ValueError):

@task(**kwargs)
def t_df(a: str) -> int:
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
flytekit.current_context().default_deck.append(TopFrameRenderer().to_html(df))
return int(a)

else:

@task(**kwargs)
def t_df(a: str) -> int:
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
flytekit.current_context().default_deck.append(TopFrameRenderer().to_html(df))
return int(a)

t_df(a="42")
assert len(ctx.user_space_params.decks) == expected_decks


@mock.patch("flytekit.deck.deck.ipython_check")
Expand Down
Loading