python
language runtime in language dropdowndef handler(pd:"pipedream"): # Reference data from previous steps print(pd.steps["trigger"]["context"]["id"]) # Return data for use in future steps return {"foo": {"test":True}}
print
at any time in a Python code step to log information as the script is running.The output for theprint
logs will appear in theResults
section just beneath the code editor.requests
for making HTTP requestssqlalchemy
for retrieving or inserting data in a SQL databasepandas
for working with complex datasetsimport requests
requirements.txt
or specify elsewhere in your workflow of which packages you need. Pipedream will automatically install the dependency for you.import
name differs from its PyPI package namepipreqs
package to detect package imports and install the associated package for you. Some packages, likepython-telegram-bot
, use animport
name that differs from their PyPI name:pip install python-telegram-bot
import telegram
# pipedream add-package python-telegram-botimport telegram
import
in your step.By default, Pipedream deploys the latest version of the PyPi package each time you deploy a change.There are many cases where you may want to specify the version of the packages you’re using. If you’d like to use aspecific version of a package in a workflow, you can add that version in amagic comment, for example:# pipedream add-package pandas==2.0.0import pandas
requests
HTTP client package available in Python to send HTTP requests.No need to runpip install
, justimport requests
at the top of your step’s code and it’s available for your code to use.See theMaking HTTP Requests with Python docs for more information.pd.respond()
method:def handler(pd:"pipedream"): pd.respond({ "status":200, "body": { "message":"Everything is ok" } })
body
andstatus
keys in yourpd.respond
argument. Thebody
must also be a JSON serializable object or dictionary.pd.respond
helper does not yet support responding with Streams.pd.steps
object.In this example, we’ll pretend this data is coming into our workflow’s HTTP trigger via POST request.// POST <our-workflows-endpoint>.m.pipedream.net{ "id":1, "name":"Bulbasaur", "type":"plant"}
pd.steps
object passed into thehandler
. Specifically, this data from the POST request into our workflow is available in thetrigger
dictionary item.def handler(pd:"pipedream"): # retrieve the data from the HTTP request in the initial workflow trigger pokemon_name= pd.steps["trigger"]["event"]["name"] pokemon_type= pd.steps["trigger"]["event"]["type"] print(f"{pokemon_name} is a{pokemon_type} type Pokemon")
return
the data in thehandler
function:# This step is named "code" in the workflowimport requestsdef handler(pd:"pipedream"): r= requests.get("https://pokeapi.co/api/v2/pokemon/charizard") # Store the JSON contents into a variable called "pokemon" pokemon= r.json() # Expose the data to other steps in the "pokemon" key from this step return { "pokemon": pokemon }
pokemon
data is accessible to downstream steps withinpd.steps["code"]["pokemon"]
os
module.import osdef handler(pd:"pipedream"): token= os.environ["AIRTABLE_API_KEY"] print(token)
import requestsimport osdef handler(pd:"pipedream"): token= os.environ["AIRTABLE_API_KEY"] url= "https://api.airtable.com/v0/your-airtable-base/your-table" headers= {"Authorization":f"Bearer{token}"} r= requests.get(url,headers=headers) print(r.text)
os
module to access your environment variables.os.environ["ENV_NAME_HERE"]
will raise an error that stops your workflow if that key doesn’t exist in your Pipedream account.Whereasos.environ.get("ENV_NAME_HERE")
willnot throw an error and instead returns an empty string.If your code relies on the presence of a environment variable, consider usingos.environ["ENV_NAME_HERE"]
instead.raise
an error to halt a step’s execution.raise NameError("Something happened that should not. Exiting early.")
user_id
contained in the event to look up information in an external API. If you can’t find data in the API tied to that user, you don’t want to proceed.return pd.flow.exit()
will end the execution of the workflow immediately. No remaining code in that step, and no code or destination steps below, will run for the current event.return pd.flow.exit()
to immediately exit the workflow. In contrast,pd.flow.exit()
on its own will end the workflow only after executing all remaining code in the step.def handler(pd:"pipedream"): return pd.flow.exit("reason") print("This code will not run, since pd.flow.exit() was called above it")
pd.flow.exit()
:def handler(pd:"pipedream"): return pd.flow.exit("Exiting early. Goodbye.") print("This code will not run, since pd.flow.exit() was called above it")
import randomdef handler(pd:"pipedream"): # Flip a coin, running pd.flow.exit() for 50% of events if random.randint(0,100)<= 50: return pd.flow.exit("reason") print("This code will only run 50% of the time");
/tmp
directory is accessible from your workflow steps for saving and retrieving files.You have full access to read and write both files in/tmp
.See theWorking with the filesystem in Python docs for more information.def handler(pd)
and thepipedream
package for Python code steps?ECONNRESET
error.If you needto use data from other steps orexport data to other steps in your workflow, we recommend using thepipedream
package module.If you need to use a Data Store in your workflow, we recommend using apre-built action to retrieve or store data orNode.js’s Data Store capabilities.Was this page helpful?