How to add variable on python code?

Hi, I’m newbie on pipedream and trying to do python integration.

My goal is to summarize content from url link from trigger using python code, It works when I add url manually on the python code, but if I add the variable from trigger, it’s always error. Is there anything I need to do so Python code can read url result from trigger correctly?

Here’s the screenshot, thank you everyone.

Hello @rifqihive, first of all welcome to Pipedream!

To have the steps data available in your Pythong step, you can wrap your code inside a handler function like this:

def handler(pd: "pipedream"):
  # Reference data from previous steps
  url = pd.steps["trigger"]["event"]["content"]

 # Your code here...

  # Return data for use in future steps
  return text

You can refer to Python Code examples by tapping “Code Examples” button

Hi @rifqihive

First off, welcome to the Pipedream community. Happy to have you!

That error message means that you have a SyntaxError.

A SyntaxError means that your Python code isn’t formatted correctly and it can’t be executed.

At the very end of the message, it says that line 4 is the issue.

I believe you have extra double quotes around your statement. Try removing those and see if that helps.

Hi Vunguyenhung,

Thanks for your help, really appreciated. So I’ve used your code and it said successfully exported, my next question is how we can use the result of the previous steps on next step? In my case it’s a url link.

hi Pierce,

Thank you for your respond, I’ve tried it and it’s not working, it seems the problem is that I’m using python step and there is different way to pass the variable between steps on pipedream, and I’m not sure how to do it. Cheers.

@rifqihive I found a doc here that guide you through developing Python code step. Could you read it through?

Hi Vunguyenhung thank you for the respond,

Yes I’ve actually read it and since I’m not well versed with coding and python, I think I’ve done the export but I’m not sure how exactly to put the result from previous steps into python workflow. In my case it’s a url from trigger.

from urllib.request import urlopen
from bs4 import BeautifulSoup

url = 'handler' ## what code to put here so the python code can load url result from my trigger result?
html = urlopen(url).read()
soup = BeautifulSoup(html, features="html.parser")

My goal is simply to make the python code can load the url data from previous trigger step, usually I can just paste the code, but it seems python has a different workflow?

Thanks again!

@rifqihive you should wrap your code inside a handler function and reference to the pd param, something like this

from urllib.request import urlopen
from bs4 import BeautifulSoup

def handler(pd: "pipedream"):
  # Reference data from trigger
  url = pd.steps["trigger"]["event"]["content"]

  # Open the URL and parse the HTML
  html = urlopen(url).read()
  soup = BeautifulSoup(html, features="html.parser")

  # Return data for use in future steps
  return soup

Note that I’m not familiar with Python (I’m more on Node.js side), and I have not tested the code above.

Alternatively, in case that BeautifulSoup does not work with Pipedream, you can use other online service for Web scraping, for example: Building low code web scraper with ScrapeNinja, Pipedream, and Google Sheets - YouTube

1 Like