Python Scripting

From DreamFactory
Jump to: navigation, search

DreamFactory supports writing event scripts and custom scripting service scripts in Python.

Note: These scripts are not sandboxed like V8js so care must be taken to prevent unwanted access to system resources, e.g. the file system of the host server that is running DreamFactory. Please see example scripts for Python for more information. Python needs to installed on the host server to run your Python scripts.


DreamFactory Python scripting requires 'bunch' package to be installed on server. Your server might not include pip by default but it should be available via your Linux version's package manager, for instance on Debian/Ubuntu:

$ sudo apt install python-pip

Once installed you can install bunch like so:

$ sudo pip install bunch

If you're running DreamFactory 3 and would like to use Python 3, then please refer to the [instructions in our guide].

Accessing Resources

DreamFactory passes in two additional objects for use in the scripts. In Python, these resources are represented as native dictionaries and can be accessed as normal. See the examples below.

Using the event resource...

verb = event['request']['method']

if verb != 'GET':
    raise Exception('Only HTTP GET is allowed on this endpoint.')
# get resource, /math —> "", /math/add —> "add"
resource = event['resource']

# get query params from request
params = event['request']['parameters']

required = ['n1', 'n2']

if resource != "":
    for element in required:
        if params.get(element, "") == ""
            raise Exception('Missing ' + element + ' in params.')
    n1 = float(params['n1'])
    n2 = float(params['n2'])
if resource == "":
    # /math means return all supported resources
    result = {'resource':['add', 'subtract', 'multiply', 'divide']}
elif resource == "add":
    result = {'result':(n1+n2)}
elif resource == "subtract":
    result = {'result':(n1-n2)}
elif resource == "multiply":
    result = {'result':(n1*n2)}
elif resource == "divide":
    if n2 == 0:
        raise Exception('Divide by zero error.')
    result = {'result':(n1/n2)}
    raise Exception('Invalid or missing resource name.')

return result

Using the platform resource...

apiKey = platform['session']['api_key'];
jwt = platform['session']['session_token'];

Writing to the DreamFactory Log

It is often useful to log messages to the DreamFactory log (located in storage/logs/) during the script development process. To do so when writing Python scripts, use the print statement:

print 'This is an example log message';

After adding a print statement and running your script, you should see output in the DreamFactory log file that looks similar to this:

[2018-10-29 18:06:18] local.INFO: Script 'mysql._table.{table_name}.post.pre_process' output:
This is an example log message


Python's httplib module is used to perform the network calls, meaning you have access to all of the debugging capabilities of this module within your Python scripts. For instance if you are calling another API endpoint within your script and something isn't going right, you can return the HTTP status code and reason like so:

api = platform.api
result = api.get(url)
print result.status
print result.reason

Including Other Scripts

In your Python script you can use other Python libraries/modules by including them using the import statement. For example import json. Note: if you are going to use a third party module, you will need to make sure that the module is installed on the server where your DreamFactory instance is running.