Skip to content

Script Development / Writing and Calling Functions

This document is the most basic guide for script development on DataFlux Func. After reading, you will be able to perform the most fundamental development and usage tasks on DataFlux Func.

1. Important Notice

During the use of DataFlux Func,

Please do not allow multiple users to log in with the same account, and avoid multiple users editing the same piece of code simultaneously.

This is to prevent issues such as code overwriting and loss.

2. Writing and Calling Your First Function

Writing code in DataFlux Func is not much different from writing regular Python code. For functions that need to be exposed as APIs, simply add the built-in @DFF.API(...) decorator.

The return value of the function is the response of the API. When the return value is a dict or list, the system will automatically return it as JSON.

A typical function looks like this:

Python
1
2
3
4
5
6
@DFF.API('Hello, world')
def hello_world(message=None):
    ret = {
        'message': message
    }
    return ret

The DataFlux Func platform provides multiple ways to call functions decorated with DFF.API(...):

Execution Function Characteristics Use Cases
Synchronous Function API Generates a synchronous HTTP API. Returns the processing result immediately after calling. Scenarios where processing time is short and the client needs immediate results.
Asynchronous Function API Generates an asynchronous HTTP API. Responds immediately upon calling but does not return the processing result. Scenarios where processing time is long, and the API call serves only as a trigger signal.
Scheduled Tasks Automatically executes based on Crontab syntax. Periodic data synchronization/caching, scheduled tasks, etc.

Here, by creating a Function API for this function, you can call it via HTTP over the public internet.

Assuming the "Function API" ID created for this function is func-api-xxxxx, the simplest way to call this function is as follows:

Text Only
1
GET /api/v1/al/func-api-xxxxx/s?message=Hello

The response is as follows (some content omitted):

Text Only
1
2
3
4
HTTP/1.1 200 OK
Content-Type: application/json

{"message":"Hello"}

3. Writing a Function that Supports File Upload

DataFlux Func also supports file uploads via Function APIs.

When you need to handle uploaded files, you can add a files parameter to the function to receive the uploaded file information. After a file is uploaded, DataFlux Func automatically stores it in a temporary upload directory for the script to process further.

Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Receives an Excel file and returns the contents of Sheet1
from openpyxl import load_workbook

@DFF.API('Read Excel')
def read_excel(files=None):
    excel_data = []
    if files:
        workbook = load_workbook(filename=files[0]['filePath'])
        for row in workbook['Sheet1'].iter_rows(min_row=1, values_only=True):
            excel_data.append(row)

    return excel_data

The files parameter is automatically populated by the DataFlux Func system, with the following content:

JSON
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[
    {
        "filePath"    : "<Temporary file storage path>",
        "originalname": "<Original filename>",
        "encoding"    : "<Encoding>",
        "mimetype"    : "<MIME type>",
        "size"        : "<File size>"
    },
    ...
]

For example commands on uploading files, see Script Development / Basic Concepts / Function API / POST Simplified Parameter Passing

4. Receiving Non-JSON, Non-Form Data

Added in version 1.6.9

In some cases, requests might be initiated by third-party systems or applications in their specific format, and the request body is not in JSON or Form format. In such scenarios, you can use **data as a parameter and call it using the POST simplified form.

When the system receives text or data it cannot parse, it automatically packages it as { "text": "<text>" } or { "base64": "<Base64-encoded binary data>"} and passes it to the function.

Example code is as follows:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import json
import binascii

@DFF.API('Function Accepting Any Body Format')
def tiger_balm(**data):
    if 'text' in data:
        # When the request body is text (e.g., Content-Type: text/plain)
        # The `data` parameter will contain a single `text` field with the content.
        return f"Text: {data['text']}"

    elif 'base64' in data:
        # When the request body is in an unparsable format (Content-Type: application/xxx)
        # The `data` parameter will contain a single `base64` field with the Base64 string of the request body.
        # The Base64 string can be converted to Python binary data using `binascii.a2b_base64(...)`.
        b = binascii.a2b_base64(data['base64'])
        return f"Base64: {data['base64']} -> {b}"

When the Request Body is Text

The request is as follows:

Bash
1
curl -X POST -H "Content-Type: text/plain" -d 'hello, world!' http://localhost:8089/api/v1/al/auln-unknow-body/s

The output is as follows:

Text Only
1
Text: hello, world!

When the Request Body is in an Unknown Format

The request is as follows:

Bash
1
curl -X POST -H "Content-Type: unknow/type" -d 'hello, world!' http://localhost:8089/api/v1/al/auln-unknow-body/s

The output is as follows:

Text Only
1
Base64: aGVsbG8sIHdvcmxkIQ== -> b'hello, world!'