Blueprints

Create a Python inline script that takes input using an expression

Source

yaml
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}")

About this blueprint

Inputs Python

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.

Script

Docker

More Related Blueprints

New to Kestra?

Use blueprints to kickstart your first workflows.

Get started with Kestra