Adding a Directory Trigger to a Lattice#

This example illustrates how to use a covalent.trigger.DirTrigger to trigger workflow dispatches automatically whenever a file in a directory is modified.


  1. Import Covalent, the trigger, and the Path library.

import covalent as ct
from covalent.triggers import DirTrigger
from pathlib import Path
  1. Create a file in the current directory named my_text_file.txt and populate it with some numbers.

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


  1. Create a directory trigger. A DirTrigger object performs a trigger action whenever a file is modified in the current directory.

dir_trigger = DirTrigger(dir_path=str(Path(".").resolve()), event_names="modified")
  1. Create a simple workflow called my_workflow and assign the trigger to the lattice with the triggers named keyword argument.

def my_workflow():
    return "File was changed."
  1. Dispatch the my_workflow lattice.

dispatch_id = ct.dispatch(my_workflow)()
# Show the dispatch ID
  1. Monitor the Covalent UI. Watch the Dashboard for new dispatches of my_workflow.

  2. Make a change to the file: Add or remove a number and save the file.

  3. In the Covalent UI, observe that a new my_workflow is dispatched whenever you change my_text_file.txt.

  4. To stop the triggering behavior, use the ct.stop_triggers function.


No new dispatches are triggered on my_workflow. To demonstrate this, save another change to the my_text_file.txt file.

Note that the stop_triggers function disables all triggers attached to the specified dispatch.

See Also#

Adding a Time Trigger to a Lattice

Adding a Database Trigger to a Lattice

Adding a SQLite Trigger to a Lattice

[ ]: