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!
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.
We'll call our new function
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 firstname.lastname@example.org: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:
├── event.json # for local testing
Now let’s write the function. As already mentioned, many of the code blocks might look similar to AWS lambda functions e.g.
from forex_python.converter import CurrencyRates
def return_currency_rates(from_currency: str, to_currency: str) -> float:
c = CurrencyRates()
return str(c.get_rate(from_currency, to_currency))
def handler(event, context):
return list(map(lambda row: return_currency_rates(row, row), 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:
"BatchedArgs": [["EUR", "USD"], ["USD", "JPY"], ["EUR", "JPY"]]
The batch is generated from this 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:
🧪 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
After authorization, we're ready to create the function:
- We need to define the function name via the flag
- We need to choose a runtime
--runtime, as of now we have three runtimes:
- We need to define the return-type
--return-typewhich the column will take, we support
stringfor now, more types will be available in the future
- Finally, we need to define the input type
—-input-typeof 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:
- Depending on your calculations it may take some time for the results to show up, for example, simple calculations like
sumare 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!