How to build an external function on Priceloop NoCode

Dat Tran
Priceloop Tech Blog
4 min readNov 2, 2022

Building an external function to get currency rates in 11 lines of code and using it on Priceloop NoCode

Priceloop NoCode External Function

In this article, I want to show you how you can write an external 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 just get current exchange rate by using the forex-python library.

Our function is called priceloopcurrency .

Getting started

The easiest way to get started is to clone our external function template on Github

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

There we provide a few easy hello world examples to write external functions. In general, we use AWS Lambda at the moment to run our external functions so many best practices on how to write lambda function in Python also applies here but also the limitations that comes with it e.g. max timeout of 15 minutes (on our platform it is 1 mins currently), 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 an external Python library. The structure of the project is then just like this

priceloopcurrency
├── event.json #for local testing
└── function.py
└── requirements.txt

Now let’s write the function. As already said many of the code blocks might look similar due to AWS lambda 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 external function is about the event["BatchedArgs"] which is the input that that you would get from Priceloop NoCode. As of now for every call we send 300 rows in batches to the lambda service. Here is an example on how the data looks like:

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

🧪 We recommend to test the lambda code locally by using python-lambda-local

On NoCode this would look like this:

Now we also need to put the forex-python library in the requirements.txt as our CLI needs to to know that a custom module needs to be installed:

# requirements.txt
forex-python==1.8

Using our Priceloop-CLI to deploy the external function

Once we finished with the code and everything works locally, we can use the priceloop-cli to easily deploy our function. Let’s install it first via npm:

npm install -g @priceloop/cli

Then we need to authorize first:

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

🌎 You can also authorize with the webbrowser via priceloop login-web

After you are authorized we can 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 follow
  • Finally, we also need to define the input type —-input-type of the function and also how many inputs we will have. This is important to provide type safety which is a major advantage when running in production. This is what we won’t get in Excel or Google sheet.
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

Eventually, let’s go to alpha.priceloop.ai, log into your account and then test out the new function:

  • We use polling to trigger our computation; depending on your calculations it may take up 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 24hrs (time-to-live variable) as of now which means if you call the function again with the same input, you will see the function result instantaneously (pure functions). We are working on making that time-to-live variable user-definable in the near future.

🎉 Now it’s your time to create your own external functions. Hopefully, you enjoyed this article. If you found this article useful, give me a high five 👏🏻 so others can find it too, and share it with your friends. Follow me here on Medium (Dat Tran) or on Twitter (@datitran) to stay up-to-date with my work. Thanks for reading!

--

--

Dat Tran
Priceloop Tech Blog

CTO & Co-Founder @ Priceloop (https://priceloop.ai/). Ex-Pivotal, Ex-idealo, Ex-Axel-Springer. More on @datitran.