Skip to content

Commit

Permalink
Merge pull request #16 from shrutimantri/inline-script
Browse files Browse the repository at this point in the history
feat: Python Script example that can take an input using an expression
  • Loading branch information
wrussell1999 authored Nov 22, 2024
2 parents cc14375 + 74d68f4 commit fac8321
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions python-use-input-in-inline-script.yaml
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit fac8321

Please sign in to comment.