• Instructions to Ask a Question

    Click on the "Ask a Question" button and select the application for which you would like to ask questions.

    We have 5 different products namely - Pabbly Connect, Pabbly Subscription Billing, Pabbly Email Marketing, Pabbly Form Builder, Pabbly Email Verification.

    The turnaround time is 24 hrs (Business Hours - 10.00 AM to 6.00 PM IST, Except Saturday and Sunday). So your kind patience will be highly appreciated!

    🚀🚀Exclusive Discount Offer

    Just in case you're looking for any ongoing offers on Pabbly, you can check the one-time offers listed below. You just need to pay once and use the application forever -
     

    🔥 Pabbly Connect One Time Plan for $249 (🏆Lifetime Access) -  View offer 

    🔥 Pabbly Subscription Billing One Time Plan for $249 (🏆Lifetime Access) - View offer

Python Code by Pabbly

Status
Not open for further replies.

Introduction​

You can extend the functionality of your Pabbly Connect workflow by using the popular programming language Python, with Code by Pabbly : Run Python action.

Some of use cases include:
  • Make an API request and manipulate the response as per need.
  • Do some math operations on the dynamic values from the preceding steps in your workflow.
  • Process your data in a way that you think is better than other apps onboard.
  • Do your personalized processing or calculations which aren’t currently possible with other apps.
  • Perform multiple tasks in a single step.
Warning - This step involves programming, so be careful! To use this, you probably need to be a coder, though you are welcome to play about with it. There is no technical help for errors in your code!

Note: The environment is Python 3.9. Your code can only run for a limited amount of time i.e. up to 25 seconds and within a limited amount of memory i.e. up to 128MB. If you exceed those limits - your script will be killed.


Section 1.

Variable and function declarations​

  • Entire content you write in or enter into the Python Code field is internally wrapped in a function, like this:
    Python:
    # imported modules:
    import json
    import math
    import logging
    import requests
    
    # the wrapper function
    
    def funcion_name():
        # predefined variable
        output=None
     
        # named logger
        logger = logging.getLogger('code')
    
        # Your code:
        ... ... ...
        ... ... ...
    
        # default return
        return output
  • Some frequently used modules, namely json, math, logging, and requests, have been already imported for your convenience.
  • Note that the wrapper function has a predefined variable, output, and a named logger, labeled as 'code'.
  • You can define your own set of variables by avoiding already defined identifiers.


Section 2.

Return a value: obtain the final output/result of the execution​

The returned a value will be available under the label Output in the step Response.
You have two ways to return your required value - using a return statement, using the `output` variable instead of a return statement. Follow any one of them, as demonstrated below.

2.1. Using a return statement:​

Python:
# Convert a given paragraph to uppercase and then extract sentences from it:-

para = 'Code by Pabbly is a freedom to process data in my own way. It allows me to perform multiple tasks in a single step. It supports JavaScript and Python.'

modification1 = para.upper()
modification2 = modification1.replace('. ', '.*')
sentences = modification2.split('*')

return sentences
Opera Snapshot_2023-09-12_121708_connect.pabbly.com.png

2.2. Using the `output` variable instead of a return statement:​

Assign your required value to our predefined variable output. The last assigned value of output will be returned as your execution result.​
Python:
# Convert a given paragraph to uppercase and then extract sentences from it:-

para = 'Code by Pabbly is a freedom to process data in my own way. It allows me to perform multiple tasks in a single step. It supports JavaScript and Python.'

modification1 = para.upper()
modification2 = modification1.replace('. ', '.*')
output = modification2.split('*')
Opera Snapshot_2023-09-12_122832_connect.pabbly.com.png

Notice that the outcome (the step Response) is the same.


Section 3.

Logging: printing a variable for testing/debugging​

Logging is optional. In order to check (print) intermediate values of the execution, you can use either print() function or the already named logger (labeled as 'code').
All the printings will be visible under the label Logs in the step Response.

Python:
# Convert a given paragraph to uppercase and then extract sentences from it:-

para = 'Code by Pabbly is a freedom to process data in my own way. It allows me to perform multiple tasks in a single step. It supports JavaScript and Python.'

modification1 = para.upper()
print(modification1)                        # print

modification2 = modification1.replace('. ', '.*')
sentences = modification2.split('*')
logger.info(sentences)                    # info

return sentences
image_2023-09-12_185104864.png
Important:
Logging/printing option is not for obtaining the final output of the workflow step. You should always use return statements or output variable (as demonstrated in section 2 above) to obtain the result (final output) and use it in subsequent actions.



Section 4.

Frequently used modules​

Some frequently used modules, namely json, math, logging, and requests, have been already imported by us, for your convenience. So, you don't need to import them again in your code.
Here are some examples covering uses of these modules.

4.1. json

Here's an example of encoding a Python dictionary into JSON format:​
Python:
my_dict = {"name": "John", "age": 30, "city": "New York"}

# Encoding the dictionary to JSON format
json_data = json.dumps(my_dict)

print(json_data)

4.2. math

The math library in Python provides various mathematical functions. Here's an example of how to use the sqrt() function to calculate the square root of a number:​

Python:
x = 16

# Calculate the square root of x
y = math.sqrt(x)

return y

4.3. logging

As mentioned earlier, internally we've imported the logging module and defined a named logger for your convenience. We've named (labeled) the logger as 'code'. So, just directly call the logger methods.​

Python:
# Log some messages
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')

1694530163461.png

4.4. requests

To easily make HTTP requests we have imported a third party module, namely requests. Here's an example of how to make a GET request:​

Python:
response = requests.get('https://hub.dummyapis.com/products?noofRecords=10&idStarts=1001¤cy=usd')

response.raise_for_status()

def test():
 return {'result': response.json()[0]}
 
return test()

1676895374588.png



Section 5.

Other Python Modules​

  • Our Python environment provides all the standard modules available in Python 3.9.
  • Exceptions: We have blocked only three specific modules, namely `os`, `subprocess`, and `sys`.
  • In addition to the standard modules, a popular third-party module, namely `requests`, is available for you.
  • Internally we've imported four frequently used modules, namely json, math, logging, and requests. As elaborated above, you can directly use them without importing the same again. To use any of other modules, you'll have to import it first.
  • You can check our full list of available Python modules.

5.1. Example: Importing modules

Python:
# Import required modules:
import datetime
import time

# Use modules:
current_date = datetime.date.today()
current_time = time.time()
current_date_time = time.strftime("%Y-%m-%d %H:%M:%S")

# Create a dictionary with the key-value pairs:
output = {
    "current_date": str(current_date),
    "current_time": current_time,
    "current_date_time ": current_date_time
}
image_2023-09-12_212910237.png
 

Attachments

  • 1676894323257.png
    1676894323257.png
    52.5 KB · Views: 517
  • 1676895896240.png
    1676895896240.png
    47.3 KB · Views: 440
  • 1676896100990.png
    1676896100990.png
    66.9 KB · Views: 428
  • 1684052473235.png
    1684052473235.png
    48.8 KB · Views: 289
  • image_2023-09-12_121251456.png
    image_2023-09-12_121251456.png
    94.4 KB · Views: 70
  • image_2023-09-12_184959225.png
    image_2023-09-12_184959225.png
    105.6 KB · Views: 62
  • 1694526915333.png
    1694526915333.png
    59.9 KB · Views: 73
  • workaround-1-1.png
    workaround-1-1.png
    164.3 KB · Views: 73
Last edited by a moderator:
Status
Not open for further replies.
Top