Skip to main content

How to create a custom function for Priceloop NoCode

Create a custom function, which fetches currency rates, so that it can be used in a spreadsheet formula. It only needs 11 lines of code!

static image

Priceloop NoCode Custom Function​

In this tutorial, I want to show you how you can write a custom function and use it on our NoCode platform. The entire process took me less than 5 minutes by taking advantage of the Python ecosystem. In particular, we’re building a function similar to GOOGLEFINANCE to get the current exchange rates by using the forex-python library.

google finance

We'll call our new function priceloopcurrency.

Getting started​

The easiest way to get started is to clone our custom function template on Github. There, we provide a few easy hello world examples to write custom functions:

git clone git@github.com:priceloop/external-functions-python.git

At the moment, we run those functions on AWS Lambda. That means, that many best practices and limitations on how to write lambda function in Python apply. An example limitation is a max timeout of 15 minutes (on our platform it is 1 minute currently). Another is the size limit of 50 MB when uploading the code to the Lambda service.

For our example we take the hello_world_advanced template, as we also want to use a custom Python library. The structure of the project looks like this:

priceloopcurrency
β”œβ”€β”€ event.json # for local testing
└── function.py
└── requirements.txt

Now let’s write the function. As already mentioned, many of the code blocks might look similar to AWS lambda functions e.g. handler and event:

# function.py
from forex_python.converter import CurrencyRates
def return_currency_rates(from_currency: str, to_currency: str) -> float:
c = CurrencyRates()
try:
return str(c.get_rate(from_currency, to_currency))
except:
return str(-1)
def handler(event, context):
return list(map(lambda row: return_currency_rates(row[0], row[1]), event["BatchedArgs"]))

πŸ‘‰ The most important part when writing custom functions is event["BatchedArgs"], which is the input you receive from Priceloop NoCode. For every call we send batches of max 300 arguments to the function. Here is an example batch:

# event.json
{
"BatchedArgs": [["EUR", "USD"], ["USD", "JPY"], ["EUR", "JPY"]]
}

The batch is generated from this data:

nocode data

Next, we add the forex-python library to the requirements.txt, as our CLI needs to to know that a custom module needs to be installed:

# requirements.txt
forex-python==1.8

πŸ§ͺ We recommend to test the lambda code locally by using python-lambda-local.

Using our Priceloop-CLI to deploy the custom function​

Once we wrote the code and tested it locally, we can use the priceloop-cli to easily deploy our function. Let’s install it via npm:

npm install -g @priceloop/cli

Then we need to authorize with the priceloop platform:

priceloop login-credentials --username "nocode_username" --password "nocode_password"

🌎 You can also authorize via webbrowser by typing priceloop login-web

After authorization, we're ready to create the function:

  • We need to define the function name via the flag --function
  • We need to choose a runtime --runtime, as of now we have three runtimes: python, python_pandas, python_numpy
  • We need to define the return-type --return-type which the column will take, we support number and string for now, more types will be available in the future
  • Finally, we need to define the input type β€”-input-type of the function and how many inputs we will have. This is important to provide type safety which is a major advantage when running in production. This is something we won’t get in Excel or Google Sheets.
priceloop create-external-function --function priceloopcurrency --runtime python --return-type number --parameter-types "string,string"

Once this is done, we can register our function:

  • We need to provide the function name that we just created
  • We now need to update the function with the code that we just created. When we created the function we only created the meta data for it and now we are filling the function with its logic
priceloop update-external-function --function priceloopcurrency --directory priceloopcurrency

Finally, let’s go to alpha.priceloop.ai, log in and use the new function:

priceloop currency example

  • Depending on your calculations it may take some time for the results to show up, for example, simple calculations like sum are pretty fast, whereas machine learning predictions are slower.
  • Once the results are there, we cache it for 24h. That means, if you call the function again with the same inputs, you will see the same function result instantaneously from the cache. We are planning to make that cache invalidation behavior configurable in the future.

πŸŽ‰ Now it’s time to create your own custom functions. Hopefully, you enjoyed this article!