diff --git a/grand_challenge_forge/generation_utils.py b/grand_challenge_forge/generation_utils.py index 0e71854..a31ddce 100644 --- a/grand_challenge_forge/generation_utils.py +++ b/grand_challenge_forge/generation_utils.py @@ -36,6 +36,12 @@ def has_example_value(component_interface): ) +def escape_strings(arg): + if isinstance(arg, str): + return json.dumps(arg) + return arg + + def create_civ_stub_file(*, target_path, component_interface): """Creates a stub based on a component interface""" target_path.parent.mkdir(parents=True, exist_ok=True) diff --git a/grand_challenge_forge/partials/algorithm-template/inference.py.j2 b/grand_challenge_forge/partials/algorithm-template/inference.py.j2 index ce6ac73..14f4209 100644 --- a/grand_challenge_forge/partials/algorithm-template/inference.py.j2 +++ b/grand_challenge_forge/partials/algorithm-template/inference.py.j2 @@ -75,7 +75,7 @@ def run(): # For now, let us make bogus predictions {%- for ci in algorithm.outputs %} output_{{ ci.slug.replace("-", "_")}} = - {%- if ci | has_example_value %} {{ ci.example_value }} + {%- if ci | has_example_value %} {{ ci.example_value | escape_strings }} {%- elif ci | is_image %} numpy.eye(4, 2) {%- elif ci | is_json %} {"content": "should match the required format"} {%- elif ci | is_file %} "content: should match the required format" diff --git a/grand_challenge_forge/partials/example-algorithm/inference.py.j2 b/grand_challenge_forge/partials/example-algorithm/inference.py.j2 index ce9dc01..c040afd 100644 --- a/grand_challenge_forge/partials/example-algorithm/inference.py.j2 +++ b/grand_challenge_forge/partials/example-algorithm/inference.py.j2 @@ -74,7 +74,7 @@ def run(): # For now, let us make bogus predictions {%- for ci in phase.algorithm_outputs %} output_{{ ci.slug.replace("-", "_")}} = - {%- if ci | has_example_value %} {{ ci.example_value }} + {%- if ci | has_example_value %} {{ ci.example_value | escape_strings }} {%- elif ci | is_image %} numpy.eye(4, 2) {%- elif ci | is_json %} {"content": "should match the required format"} {%- elif ci | is_file %} "content: should match the required format" diff --git a/grand_challenge_forge/partials/filters.py b/grand_challenge_forge/partials/filters.py index 4d0983e..b8efb7a 100644 --- a/grand_challenge_forge/partials/filters.py +++ b/grand_challenge_forge/partials/filters.py @@ -41,3 +41,8 @@ def has_file(arg): @register_simple_filter def has_example_value(arg): return generation_utils.has_example_value(arg) + + +@register_simple_filter +def escape_strings(arg): + return generation_utils.escape_strings(arg) diff --git a/tests/utils.py b/tests/utils.py index 2b75a46..2102fad 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -187,7 +187,7 @@ "kind": "Anything", "super_kind": "File", "relative_path": "yet-another-non-json-output-value", - "example_value": None + "example_value": "A string that needs to be escaped" } ] }