Skip to content

Testing your deployment locally

When you have made your deployment package you might want to test it out before you deploy it to UbiOps, to make sure your code runs as expected and to quickly filter out small mistakes. You can do so by simulating how UbiOps would initialize your deployment and how it would handle a request. We provide two different implementations, one for Python and one for R.
For Python, we make use of the ubiops.utils.run_local function from the UbiOps Python client library and the ubiops run_local command from the CLI.
For R, we provide a small R script that mimics the same functionality. Both implementations are explained in the following sections.

For both implementations, we assume the following file structure:

└── Root Folder
    ├── Deployment Package Directory (e.g. "deployment_package")
    └── Local Testing Script (Python or R file containing the relevant code snippets)

Python

To test your deployment locally, the ubiops.utils.run_local function from the UbiOps Python client library or the ubiops run_local command from the CLI can be used. Both implementations are explained in the following sections. Both methods will initialize your deployment and make a request to it, just like UbiOps would, but in your local environment.
These methods run the request function of your deployment in the current (virtual) environment. This means that all necessary libraries should be installed in the current (virtual) environment.

Python Client Library

The ubiops.utils.run_local function is part of the UbiOps Python client library. This library can be installed using pip:

pip install ubiops

After installing the Client Library, the ubiops.utils.run_local can be used with the following arguments:

Name Type Description
deployment_directory str The name of the deployment directory, absolute or relative to the current working directory, e.g. 'deployment_package'
data dict or str Input data of the deployment request function
init_context dict [optional] Context data of the deployment init function, defaults to Dummy init context
request_context dict [optional] Context data of the deployment request function, defaults to Dummy request context

Example

from ubiops import utils

# TODO: Enter your local relative/absolute path here
deployment_directory = "deployment_package"

# TODO: Adjust this example to mimic your input data
request_data = {"input": 123}

result = utils.run_local(deployment_directory, request_data)
print(f"Result: {result}")

CLI

The ubiops run_local command is part of the UbiOps CLI. This CLI can be installed using pip:

pip install ubiops-cli

After installing the CLI, the ubiops run_local command can be used with the following options:

Options Required/Optional Description
-dir/--directory Required Path to a directory that contains at least a 'deployment.py'
--data Optional The input data of the request
This option can be provided multiple times in a single command
-f/--json_file Optional Path to json file containing the input data of the request
--plain Optional Set the input data as plain text

Examples

ubiops run_local -dir deployment_package --data '{"input": 123}'
ubiops run_local -dir deployment_package -f input.json
ubiops run_local -dir deployment_package --plain "This is plain text"

R

To test your deployment locally, you can use the provided R script. This script will initialize your deployment and make a request to it, just like UbiOps would, but in your local environment.

# Deployment_directory points to the base folder of the deployment, we assume 
# your deployment directory is called 'deployment_package'.
# TODO enter your local absolute path here.
deployment_directory <- file.path("YOUR_LOCAL_ABS_PATH", "deployment_package")

# Import the deployment package
source(file.path(deployment_directory, "deployment.R"))

init( base_directory = deployment_directory, context = list() )

# Mock input data. When a request is made, this named list is initialized.
# In case of deployments with structured data, it will contain the keys defined
# by the user upon deployment creation via the platform. In case of a deployment
# that receives a plain input, it will be a string.

# TODO Adjust this example to mimic your input data to test deployment input and
# request processing
input_data <- list(input_field_1=10.9)

# Make the request. UbiOps calls this method in exactly the same way
request_result <- request( 
    input_data = input_data,
    base_directory = deployment_directory,
    context = list() 
)

print(paste("Deployment request result:", request_result))

If you are having any troubles in using the provided code snippets, do not hesitate to ask for help in our Slack community.