Skip to content

Commit

Permalink
feat: add blueprint on python command that can take an input using an… (
Browse files Browse the repository at this point in the history
#20)

* feat: add blueprint on python command that can take an input using an environment variable

* Update python-input-as-env-variable.yaml

---------

Co-authored-by: Will Russell <[email protected]>
  • Loading branch information
shrutimantri and wrussell1999 authored Dec 11, 2024
1 parent 6b6f00c commit 082aca9
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions python-input-as-env-variable.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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 }}"

extend:
title: Run a Python command that can takes an input using an environment variable
description: >-
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
import requests
import os
# 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.
tags:
- Python
- Inputs
ee: false
demo: true
meta_description: This flow runs the python file that takes the input using an environment variable.

0 comments on commit 082aca9

Please sign in to comment.