Blueprints

Run a Python command that can takes an input using an environment variable

Source

yaml
id: python-input-as-env-variable
namespace: company.team

inputs:
  - id: uri
    type: URI
    defaults: https://www.google.com/

tasks:
  - id: code
    type: io.kestra.plugin.scripts.python.Commands
    namespaceFiles:
      enabled: true
    taskRunner:
      type: io.kestra.plugin.scripts.runner.docker.Docker
    containerImage: ghcr.io/kestra-io/pydata:latest
    commands:
      - python main.py
    env:
      URI: "{{ inputs.uri }}"

About this blueprint

Inputs Python

This workflow passes the input as an environment variable to the Python code. The code task, which is the python Commands task, has the env property that sets the environment variables. The value of these environment variables can be based on the inputs. The main.py file that gets executed using the python commands task can then use these environment variables which contain the inputs. For this workflow, the main.py file will have the following code:

python
# Perform the GET request response = requests.get(os.environ['URI'])
# Check if the request was successful if response.status_code == 200:
    # Print the content of the page
    print(response.text)
else:
    print(f"Failed to retrieve the webpage. Status code: {response.status_code}")

Notice how the python code uses the URI environment variable.

Commands

Docker

More Related Blueprints

New to Kestra?

Use blueprints to kickstart your first workflows.

Get started with Kestra