Skip to content

Commit

Permalink
SDK/Components/PythionContainerOp - Make the local output path config…
Browse files Browse the repository at this point in the history
…urableThis is part of a bigger effort to make all output locations manageable in preparation for storage system.
  • Loading branch information
Ark-kun committed Nov 30, 2018
1 parent 052de19 commit d012c51
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
15 changes: 6 additions & 9 deletions sdk/python/kfp/compiler/_component_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,7 @@ def _generate_entrypoint(self, component_func):
func_signature = 'def ' + new_func_name + '('
for input_arg in input_args:
func_signature += input_arg + ','
if len(input_args) > 0:
func_signature = func_signature[:-1]
func_signature += '):'
func_signature += '_output_file):'
codegen.writeline(func_signature)

# Call user function
Expand All @@ -321,9 +319,9 @@ def _generate_entrypoint(self, component_func):
codegen.writeline(call_component_func)

# Serialize output
codegen.writeline('with open("/output.txt", "w") as f:')
codegen.indent()
codegen.writeline('f.write(str(output))')
codegen.writeline('from pathlib import Path')
codegen.writeline('Path(_output_file).parent.mkdir(parents=True, exist_ok=True)')
codegen.writeline('Path(_output_file).write_text(str(output))')
wrapper_code = codegen.end()

# CLI codes
Expand All @@ -332,6 +330,7 @@ def _generate_entrypoint(self, component_func):
codegen.writeline('parser = argparse.ArgumentParser(description="Parsing arguments")')
for input_arg in input_args:
codegen.writeline('parser.add_argument("' + input_arg + '", type=' + inputs[input_arg].__name__ + ')')
codegen.writeline('parser.add_argument("_output_file", type=str)')
codegen.writeline('args = vars(parser.parse_args())')
codegen.writeline('')
codegen.writeline('if __name__ == "__main__":')
Expand Down Expand Up @@ -435,9 +434,6 @@ def _generate_pythonop(component_func, target_image, target_component_file=None)
'container': {
'image': target_image,
'arguments': [],
'fileOutputs': {
'output': '/output.txt'
}
}
}
for input in input_names:
Expand All @@ -446,6 +442,7 @@ def _generate_pythonop(component_func, target_image, target_component_file=None)
'type': 'str'
})
component_artifact['implementation']['container']['arguments'].append({'value': input})
component_artifact['implementation']['container']['arguments'].append({'output': 'output'})

target_component_file = target_component_file or getattr(component_func, '_component_target_component_file', None)
if target_component_file:
Expand Down
24 changes: 15 additions & 9 deletions sdk/python/tests/compiler/component_builder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,17 @@ def sample_component_func(a: str, b: int) -> float:
result = float(b + 5)
return result
def wrapper_sample_component_func(a,b):
def wrapper_sample_component_func(a,b,_output_file):
output = sample_component_func(str(a),int(b))
with open("/output.txt", "w") as f:
f.write(str(output))
from pathlib import Path
Path(_output_file).parent.mkdir(parents=True, exist_ok=True)
Path(_output_file).write_text(str(output))
import argparse
parser = argparse.ArgumentParser(description="Parsing arguments")
parser.add_argument("a", type=str)
parser.add_argument("b", type=int)
parser.add_argument("_output_file", type=str)
args = vars(parser.parse_args())
if __name__ == "__main__":
Expand All @@ -338,15 +340,17 @@ def sample_component_func_two(a: str, b: int) -> float:
result = float(b + 5)
return result
def wrapper_sample_component_func_two(a,b):
def wrapper_sample_component_func_two(a,b,_output_file):
output = sample_component_func_two(str(a),int(b))
with open("/output.txt", "w") as f:
f.write(str(output))
from pathlib import Path
Path(_output_file).parent.mkdir(parents=True, exist_ok=True)
Path(_output_file).write_text(str(output))
import argparse
parser = argparse.ArgumentParser(description="Parsing arguments")
parser.add_argument("a", type=str)
parser.add_argument("b", type=int)
parser.add_argument("_output_file", type=str)
args = vars(parser.parse_args())
if __name__ == "__main__":
Expand All @@ -359,13 +363,15 @@ def wrapper_sample_component_func_two(a,b):
def sample_component_func_three() -> float:
return 1.0
def wrapper_sample_component_func_three():
def wrapper_sample_component_func_three(_output_file):
output = sample_component_func_three()
with open("/output.txt", "w") as f:
f.write(str(output))
from pathlib import Path
Path(_output_file).parent.mkdir(parents=True, exist_ok=True)
Path(_output_file).write_text(str(output))
import argparse
parser = argparse.ArgumentParser(description="Parsing arguments")
parser.add_argument("_output_file", type=str)
args = vars(parser.parse_args())
if __name__ == "__main__":
Expand Down

0 comments on commit d012c51

Please sign in to comment.