Skip to content

Utils module

We have created a Python module named ubiops.utils. The module contains functions that help you improve your workflow by making it easier to work with files, run a deployment locally, validate environment files, and wait for succesful builds. You can find a more extensive explanation and examples for each function on our GitHub page. On this page you can also find different ways of how you can estabilish and authorize a connection with your UbiOps environment.s

Work with files

Link to GitHub

You can use the utils library to upload or download files with one line of code:

import ubiops 

# Upload a file
ubiops.utils.upload_file(api_client, project_name, file_path, bucket_name=bucket_name, file_name=file_name)

# Download a file
ubiops.utils.download_file(api_client, project_name, bucket_name=bucket_name, file_name=file_name, output_path=output_path,
    stream=True, chunk_size=8192)

Run a deployment locally

Link to GitHub

You can use the run_local methods to run a deployment locally. It calls the request(s) function of the deployment in the same way as would happen when running it on UbiOps.

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}")

Validate

Link to GitHub

You can use the validate methods to validate your requirements.txt or ubiops.yaml file locally. If any inconsistencies/errors are found, they will be logged with the corresponding line numbers

from ubiops import utils

utils.validate_requirements_file(file_path="requirements.txt")

utils.validate_yaml_file(file_path="ubiops.yaml")

WaitFor

Link to GitHub

The wait_for methods can be used to improve your workflow. The function waits for a build or process (e.g. deployment version, environment or request) to be successful before returning. If a build or process fails or does not complete in the given timeout, an error will be raised. The parameter stream_logs=False/True can be used to print logs while waiting.

The function can be used for the following:

Subject Method Description
Deployment version wait_for_deployment_version Wait for deployment version to be available
Experiment wait_for_experiment** Wait for deployment version to be available
Environments wait_for_environment Wait for environment to build
Revisions wait_for_revision Wait for deployment version revision to be available
Deployment requests wait_for_deployment_request Wait for deployment request to complete
Deployment versions requests wait_for_deployment_version_request Wait for deployment version request to complete
Experiment runs wait_for_experiment_run Wait for experiment run to complete
Pipeline requests wait_for_pipeline_request Wait for pipeline request to complete
Pipeline versions requests wait_for_pipeline_version_request Wait for pipeline version request to complete
Export wait_for_export Wait for export to complete
Import wait_for_import Wait for import to complete

The code snippet below shows an example of how to use the wait_for_deployment_version function. More examples of the wait_for function can be found on our GitHub page.

import UbiOps

core_api = ubiops.CoreApi() 

core_api.deployment_versions_create(project_name, deployment_name, data=version_template) 
response = core_api.revisions_file_upload(project_name, deployment_name, version, file='deployment.zip')
print("Upload response is:", response)

# Wait for the deployment version to be available
ubiops.utils.wait_for_deployment_version(
        api_client, project_name, deployment_name, version, revision_id=response.revision,
        timeout=1800, quiet=False, stream_logs=False
)
print("Deployment version is available")