How to add a directory trigger to a lattice#

In this guide we’ll illustrate how to use a DirTrigger to trigger workflow runs automatically whenever a file in a directory gets modified.

Let’s first import the required parts:

import covalent as ct
from covalent.triggers import DirTrigger
from pathlib import Path

For our test case, let’s create a new file in our current directory named my_text_file.txt and have 10 numbers in it.

with open("my_text_file.txt", "w") as f:
    for i in range(10):

Now, let’s create our DirTrigger object which performs a trigger action whenever something in the current directory gets modified.

dir_trigger = DirTrigger(dir_path=str(Path(".").resolve()), event_names="modified")

Let’s create a simple workflow now and assign this trigger to the lattice:

def my_workflow():
    return 42

Once we’ve made sure that the covalent server is running, we can perform the dispatch for my_workflow as such:

dispatch_id = ct.dispatch(my_workflow)()

Now, if you check the UI you’ll see that a new my_workflow gets dispatched whenever we add or remove a number from my_text_file.txt. If we want to stop that after a while, we can use the ct.stop_triggers function:

Triggers for following dispatch_ids have stopped observing:

The above will prevent any new dispatches from happening due to the trigger action on my_workflow lattice.