Skip to main content

Use libraries

One of the best parts about python is its large ecosystem of supporting libraries that can be reused in your projects.

For this example we will show how to bring the scipy library into your ArupCompute project, but the principal is the same for any other library.

See here for more detail on which libraries are supported in ArupCompute projects.

Install the library locally

Open up VS Code in the folder you are using for development.

Ensure that the correct virtual environment has been associated with the folder, then open up the integrated terminal with ctrl + '.

Then run the command pip install scipy.

pip-install-scipy

Our function will take advantage of the scipy interpolation feature to provide intermediate values between some known data.

from scipy import interpolate

def interpolation(x: float) -> float:
"""Interpolate known data

Interpolates between some known data. X-range 0 to 2.

Args:
x: Test x value to interpolate with
"""
x_vals = [0,1,2]
y_vals = [0,10,5]

f = interpolate.interp1d(x_vals,y_vals)

y = f(x).item() # returns an ndarray, we just want a plain number

return y

If we run this function via the REPL we will see that intermediate values can be calculated.

repl-interpolation

Defining our requirements

Before we reupload our library to ArupCompute we need to create an extra file to let ArupCompute know what libraries we depend upon.

Create a file called requirements.txt in the same folder as your python files, and enter this text:

scipy==1.8.0
tip

The numbers after the package name are its version. It is good practice to specify which version of a dependency you need to ensure the behaviour on ArupCompute is the same as you experience locally.

To see what packages and versions you have installed open up terminal, activate your environment, then run pip list

pip-list

Now all we need to do is package up as before, making sure to include requirements.txt in the zip package.

zip-with-requirements

After reuploading and finding the function we will find that it works in the web browser.

web-interpolate

This same approach should work for most libraries within the python ecosystem. The ArupCompute team have undertaken testing with some common libraries, however there are may be some libraries that cannot be supported due to the sandbox that ArupCompute functions operate in. If you encounter difficulties please reach out to the ArupCompute team.