Skip to content

Handle files as output

UbiOps supports files as output of deployments or pipelines, but they do work a bit differently than other output data types. On this page you can find how to handle a file type output in your deployment code, and how to retrieve a file from a request response.

Handling files in deployment code

For UbiOps to be able to handle file output it simply needs the relative path to the file. UbiOps does the rest of the file handling automatically. Let's assume you have a deployment with output field csv which refers to a csv file output. In your deployment you can handle this output as follows:

import pandas as pd

class Deployment:

    def __init__(self, base_directory, context):
        print("Initialising My Deployment")

    def request(self, data):
        print("Processing request for My Deployment")

        # A random dataframe for illustration purposes
        dummy_data = {'Product': ['Desktop Computer','Tablet','Printer','Laptop'],
                'Price': [850,200,150,1300]
                }

        df = pd.DataFrame(dummy_data, columns= ['Product', 'Price'])

        # Here we create the new csv file
        rel_output_path = "output_csv.csv"
        df.to_csv(rel_output_path, index=False, header=True)

        # And we return the path to the newly created file
        return {
            "csv" : rel_output_path
        }

Retrieving request results with files

When the data type of one of your deployment output fields is a file, the request response will contain a file URI. If you are using the WebApp the file URI will automatically be used to fetch the right file and you will be provided with a download link, but if you're making the request programmatically you need to fetch the file yourself. Below you can check examples of how to do that with the Python client library. We are using the same deployment example from the previous section.

import ubiops

# Establish a connection to the UbiOps API
configuration = ubiops.Configuration()
configuration.api_key['Authorization'] = API_TOKEN
client = ubiops.ApiClient(configuration)
api = ubiops.CoreApi(client)

# We make a request:
response = api.deployment_requests_create(
    project_name=PROJECT_NAME, deployment_name=DEPLOYMENT_NAME, data={}
)

# Now we get the file URI
file_uri = response.result["csv"]

# And here we download the csv file
ubiops.utils.download_file(
    client,
    project_name=PROJECT_NAME,
    file_uri=file_uri,
    output_path='.',
    stream=True,
    chunk_size=8192
)

For more information on making requests with the Python client library, see how to make requests.