Skip to content

Commit

Permalink
feat(sdk): expose IfPresentPlaceholder and ConcatPlaceholder to kfp.d…
Browse files Browse the repository at this point in the history
  • Loading branch information
zichuan-scott-xu authored and jlyaoyuli committed Jan 5, 2023
1 parent 36108ba commit b1c577a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from kfp.components import placeholders
from kfp.dsl import ConcatPlaceholder
from kfp.dsl import container_component
from kfp.dsl import ContainerSpec
from kfp.dsl import Dataset
Expand All @@ -26,7 +26,7 @@ def container_with_concat_placeholder(text1: str, text2: Output[Dataset],
image='python:3.7',
command=[
'my_program',
placeholders.ConcatPlaceholder(['prefix-', text1, text2.uri])
ConcatPlaceholder(['prefix-', text1, text2.uri])
],
args=['--output_path', output_path])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Optional

from kfp.components import placeholders
from kfp.dsl import container_component
from kfp.dsl import ContainerSpec
from kfp.dsl import Dataset
from kfp.dsl import IfPresentPlaceholder
from kfp.dsl import Output
from kfp.dsl import OutputPath

Expand All @@ -29,11 +27,11 @@ def container_with_if_placeholder(output_path: OutputPath(str),
image='python:3.7',
command=[
'my_program',
placeholders.IfPresentPlaceholder(
IfPresentPlaceholder(
input_name='optional_input',
then=[optional_input],
else_=['bye']), '--dataset',
placeholders.IfPresentPlaceholder(
IfPresentPlaceholder(
input_name='optional_input', then=[dataset.uri], else_=['bye'])
],
args=['--output_path', output_path])
Expand Down
51 changes: 41 additions & 10 deletions sdk/python/kfp/components/placeholders.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,23 @@ class ConcatPlaceholder(base_model.BaseModel, Placeholder):
"""Placeholder for concatenating multiple strings. May contain other
placeholders.
Attributes:
items: Elements to concatenate.
Examples:
::
@container_component
def container_with_concat_placeholder(text1: str, text2: Output[Dataset],
output_path: OutputPath(str)):
return ContainerSpec(
image='python:3.7',
command=[
'my_program',
ConcatPlaceholder(['prefix-', text1, text2.uri])
],
args=['--output_path', output_path]
)
"""
items: List[CommandLineElement]
"""Elements to concatenate."""

@classmethod
def split_cel_concat_string(self, string: str) -> List[str]:
Expand Down Expand Up @@ -338,18 +351,36 @@ class IfPresentPlaceholder(base_model.BaseModel, Placeholder):
"""Placeholder for handling cases where an input may or may not be passed.
May contain other placeholders.
Attributes:
input_name: name of the input/output.
then: If the input/output specified in name is present
the command-line argument will be replaced at run-time by the
expanded value of then.
else_: If the input/output specified in name is not present,
the command-line argument will be replaced at run-time by the
expanded value of otherwise.
Examples:
::
@container_component
def container_with_if_placeholder(output_path: OutputPath(str),
dataset: Output[Dataset],
optional_input: str = 'default'):
return ContainerSpec(
image='python:3.7',
command=[
'my_program',
IfPresentPlaceholder(
input_name='optional_input',
then=[optional_input],
else_=['no_input']), '--dataset',
IfPresentPlaceholder(
input_name='optional_input', then=[dataset.uri], else_=['no_dataset'])
],
args=['--output_path', output_path]
)
"""
input_name: str
"""name of the input/output."""

then: List[CommandLineElement]
"""If the input/output specified in name is present, the command-line argument will be replaced at run-time by the expanded value of then."""

else_: Optional[List[CommandLineElement]] = None
"""If the input/output specified in name is not present, the command-line argument will be replaced at run-time by the expanded value of otherwise."""

_aliases = {'input_name': 'inputName', 'else_': 'else'}

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion sdk/python/kfp/components/structures_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def comp_with_artifact_output(dataset_old: dsl.Output[dsl.Dataset],
image='gcr.io/my-image',
command=['sh', 'run.sh'],
args=[
placeholders.IfPresentPlaceholder(
dsl.IfPresentPlaceholder(
input_name='optional_input',
then=[dataset_old],
else_=[dataset_new])
Expand Down
4 changes: 4 additions & 0 deletions sdk/python/kfp/dsl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@
'PIPELINE_JOB_ID_PLACEHOLDER',
'PIPELINE_TASK_NAME_PLACEHOLDER',
'PIPELINE_TASK_ID_PLACEHOLDER',
'IfPresentPlaceholder',
'ConcatPlaceholder',
]

from kfp.components.component_decorator import component
from kfp.components.container_component_decorator import container_component
from kfp.components.importer_node import importer
from kfp.components.pipeline_context import pipeline
from kfp.components.pipeline_task import PipelineTask
from kfp.components.placeholders import ConcatPlaceholder
from kfp.components.placeholders import IfPresentPlaceholder
from kfp.components.structures import ContainerSpec
from kfp.components.task_final_status import PipelineTaskFinalStatus
from kfp.components.tasks_group import Condition
Expand Down

0 comments on commit b1c577a

Please sign in to comment.