# Creating automations

# Workflow

# An overview of the steps you'll need to take to deploy and run a new automation in Prefix.

The basic steps for creating an automation in Prefix are:

  1. Perform the task once manually, and identify what inputs and outputs you expect from it. Is the automation a scraper, or a transaction (i.e. writing data)?
  2. Log in to the Orchestrator and confirm accounts are created for each user working with the Studio application. You're free to create as few or as many as you'd like.
  3. Use the Studio or our Python API to incrementally build and test your automation, and end up with either a *.pxm or *.zip file. Feel free to request some example automation files from us for your team to bootstrap with.
    • Using the Record feature can dramatically accelerate this process
    • If you require specific features that prove impediments to getting fully automated, check in with us! It's often the case these can get rolled out quickly.
  4. Navigate to the Publisher page in the Studio, and upload your model to the orchestrator for remote execution.
  5. Your automation is now available for use on your orchestrator! Go to the Jobs page, select a corresponding Tenant & Workspace from the dropdowns at the top of the screen, and create a job that uses the model you've created.

And you're done! Click the name of your new job in the list to view the Job Details page, then click View Schedule Settings to see the API endpoint required to start a job programmatically. Click Instant Job Run to start the job manually. If your agent server is correctly configured, you should find it executing your automation.

# Studio models

Models can also be developed without code in Prefix Studio. This allows non-technical teams to take control over automation efforts, and can provide developers with an accelerated approach to developing and maintaining automations.

Information on where to download and how to use our studio application can be found at Studio Docs.

Though we eventually expect to have some compatibility with other operating systems in the future, please note that, at the moment, only Windows 7 and above, along with recent versions of OSX are supported.

# Python models

Developing automations in Python is currently the quickest and most extensible way to get started with Prefix. You'll need Python 3.8 or above installed either on your computer or a virtual environment to follow this guide.

# Creating a Python model

To get started, extract the following zip file somewhere you'd like to develop Prefix automations on your computer.

Python Model Template (opens new window)

Make another copy of the "web_waiter" folder (this is an example Python model), and rename the copied folder with the name of another model you wish to develop. Inside the folder, you should see a file called automation.py which contains the following code:

import sys
import time

sys.path.append("..")

from automations.base.automation_base import BaseAutomation, LogEvent
from playwright.sync_api import sync_playwright

class Automation(BaseAutomation):
    def run(self):
        """
        Prefix main method.
        """
        # Mark job as started
        self.started()

        try:
            self.log("Example automation started")

            data = self.data

            with sync_playwright() as playwright:
                browser = playwright.chromium.launch(headless=False)
                context = browser.new_context()
                page = context.new_page()
                # -------------------------
                # Browser automation code goes here
                # Go here for more info:
                # https://playwright.dev/python/docs/intro/

                # (Example code) Keep browser open for 3 seconds
                time.sleep(3)

                # -------------------------
                page.close()
                context.close()
                browser.close()

                # Mark job as completed (success)
                self.completed()
        except Exception as e:
            self.log_error(str(e))

            # Mark job as failed
            self.errored()

        return True

if __name__ == "__main__":
    automation = Automation()
    automation.run()

To build this automation, put whatever you'd like Prefix to run in your automation in the run method in automation.py. You're free to add however many files you want to the folder containing automation.py, but they must ultimately be imported and run in automation.py's run method. Your overall directory structure should look like the following:

python-model-template
    automations/    
        __init__.py
        base/
            __init__.py
            automation_base.py
    
    web-waiter/
        __init__.py
        automation.py
    
    my-test-model/
        __init__.py
        automation.py
        *.* (any other files required)
        
    (any other models you develop)

Test automations by running automation.py in model folders locally. To simulate Prefix passing data to your automation, set attribute values of automation in the main method. The exact attributes you can set here are present at the bottom of the page.

Currently, the only external package supported for web automations is Playwright, so you'll need to set it up on your local system (or a virtual environment) to test automation.py on your computer. You can do so easily by running the following (Python 3.8+):
python pip install playwright playwright install

From here, you're free to work on your automation as you like, just make sure to implement the Automation.run interface from the template file, and not to delete the automations folder. Once you've modified automation.py with your specific workflow, compress the folder containing automation.py into a .zip file. This is your completed Python model.

To confirm, your model .zip file should have the following structure:

my-test-model.zip
    my-test-model/
        __init__.py
        automation.py
        *.* (any other files)

# Deploying the automation

Now that you have a Python automation Prefix can understand, you need to upload the corresponding zip file to the orchestrator.

Navigate to the Models page in the Orchestrator, select a Tenant/Workspace, and click Add New. Select Python from the Type dropdown, then click the upload button and select your zip file from the previous step. Set the Status of the model to its particular step in your development process, and hit Create.

Now, go to jobs, select a Tenant/Workspace, and click Add New. Select your Model from the dropdown, give your job a Name, and click Retain Log to make sure you'll be able to see what goes wrong as you continue iterating on your automation.

With the creation of your job, you're all set to run your automation through Prefix. Use the Run Again action from the jobs list, or click the job title to go to the job details page, and click Instant Job Run. Either one will manually trigger your automation.

If you'd like to be able to pass data to this automation every time you run it, check out the section on Job Data, and then use the self.data attribute in your code below.

# The BaseAutomation API

In our example code, you may have noticed a couple methods & attributes BaseAutomation gives you access to that integrate with data in Prefix. Here's a summary of them:

# Methods

Method Description
self.started() Sets the status of the automation to "Dispatched" in the Orchestrator.
self.completed() Sets the status of the automation to "Completed" in the Orchestrator.
self.errored() Sets the status of the automation to "Failed" in the Orchestrator.
self.log(message) Writes a message to the log saved in Prefix.
self.log_error(messag) Writes a message with level "ERROR" to the log saved in Prefix.
self.get_file_url(filename) Returns a file path at which a new file can be saved as filename in Prefix.

# Attributes

Method Description
self.started() Sets the status of the automation to "Dispatched" in the Orchestrator.
self.completed() Sets the status of the automation to "Completed" in the Orchestrator.
self.errored() Sets the status of the automation to "Failed" in the Orchestrator.
self.log(message) Writes a message to the log saved in Prefix.
self.log_error(messag) Writes a message with level "ERROR" to the log saved in Prefix.
self.get_file_url(filename) Returns a file path at which a new file can be saved as filename in Prefix.