Multiple files

Overview

Supporting multiple files in UbiFunctions improves flexibility and scalability. When building serverless functions, splitting code into multiple files improves maintainability, enables reuse, and aligns with common software development practices.

UbiFunctions allows you to use multiple files such as:

  • Source files: These files contain code that can be executed.

  • Config files: These are typically .json, .conf, .toml, or similar files. They do not contain executable code. Instead, they provide the data and parameters your logic needs to run with a particular behavior.

  • Data files: These are files that store information in various formats, such as .csv, .txt, or .json. They can contain structured, semi-structured, or unstructured data used as input for processing or analysis.

Creating a file

To create a file in your UbiFunction root directory, click the + icon in the files pane, then select New file. Enter a name. The new file is added to your UbiFunction:

Creating a folder

Folders help you organize modules more effectively. To create a folder, click the + icon in the files pane, then select New folder. Enter a name. The new folder is added to your UbiFunction:

Creating files or folders within a folder

To create a file or folder inside an existing folder, hover over the folder, click the three-dot menu, and select New file or New folder.

File structure and imports

When you add files or folders to a UbiFunction, they behave like a directory on your local machine, with the root directory as the working directory. For example, if you add decoder.py|js alongside the main script, you can import it like this:

Similarly, if you create a folder utils and add a file parser.py or parser.js inside it, the structure behaves like a subdirectory on your computer. To import from it, use:

This means you can treat the UbiFunction file structure like a local project folder.

circle-info

UbiFunctions use a protected file system. You can create files and folders through the UI, but scripts run in a strictly read-only environment and cannot write files or create new files and folders.

Data and config files

UbiFunctions let you include data and configuration files, such as .json, .conf, .txt, and .csv, to provide inputs or settings for your functions. To read a file, handle it the same way you would in a local script:

Example

Suppose you have several device types on an LNS sending data continuously. For each uplink, you need to decode the payload and forward it to the corresponding device in Ubidots.

You can solve this with a UbiFunction that uses separate scripts for each device type and calls them from the main function.

For this example, create:

  • A folder called modules. Inside it, create:

    • A Python script called s2100.py. This is the decoder for the s2100 device.

    • A Python script called s210x.py. This is the decoder for the s210x device.

    • A Python script called utils.py. This file contains shared utility functions.

  • A configuration file called device_config.json that stores the configuration for each device type.

It should look like this:

The following are the contents of each script:

s2100.py script

s210x.py script

utils.py script

device_config.json

main.py

If you execute this UbiFunction with the following test payload:

It should output something like this:

Last updated

Was this helpful?