diff --git a/python-use-input-in-inline-script.yaml b/python-use-input-in-inline-script.yaml new file mode 100644 index 0000000..ab5ec62 --- /dev/null +++ b/python-use-input-in-inline-script.yaml @@ -0,0 +1,54 @@ +id: python-use-input-in-inline-script +namespace: company.team + +inputs: + - id: pokemon + type: STRING + defaults: pikachu + + - id: your_age + type: INT + defaults: 25 + +tasks: + - id: inline_script + type: io.kestra.plugin.scripts.python.Script + description: Fetch the pokemon detail and compare its experience + taskRunner: + type: io.kestra.plugin.scripts.runner.docker.Docker + containerImage: ghcr.io/kestra-io/pydata:latest + script: | + import requests + import json + + url = "https://pokeapi.co/api/v2/pokemon/{{ inputs.pokemon }}" + response = requests.get(url) + + if response.status_code == 200: + pokemon = json.loads(response.text) + print(f"Base experience of {{ inputs.pokemon }} is { pokemon.get('base_experience') }") + if pokemon.get('base_experience') > int("{{ inputs.your_age }}"): + print("{{ inputs.pokemon }} has more base experience than your age") + else: + print("{{ inputs.pokemon}} is too young!") + else: + print(f"Failed to retrieve the webpage. Status code: {response.status_code}") + +extend: + title: Create a Python inline script that takes input using an expression + description: >- + This flow shows how you can use an input using an expression in Python inline script. + + The script takes two inputs, a string and an integer. Both the variables are being using in the + Python code written as inline script. + + The Python script runs in a Docker container by default. In this case, we have explicitly mentioned the taskRunner as + Docker and provided an image for the Docker container. Kestra ensures that the inputs are resolved even when the script + runs using any of the taskRunners. + + tags: + - Python + - Inputs + ee: false + demo: true + meta_description: This flow shows how you can take an input using an expression and use it in inline Python script.