Tutorial

PyTransitions with IFlowly

Using PyTransitions with IFlowly

PyTransitions is an object oriented python library that orchestrate states and is a lightweight state machine. It is very powerful and used in production by the community. The caveat with using it right now is that the states and triggers have to be hardcoded into your site. Meaning that any time you want to add/remove states or triggers, you'll need to update your codebase unless you're using some storage format for your states and triggers.

In this tutorial we'll walk through how to create a Flat flow in IFlowly and use IFlowly Python SDK to get your states and triggers then instantiate a StateMachine with PyTransitions

Since we want to look at a flat flow, we don't need to create a steps for that flow, we just want to configure the states and triggers from the UI. Then use PyTransitions and IFlowly SDK to do the rest.

Pre-requisites:

  1. PyTransition Installation
  2. IFlowly SDK Installation
  3. Create an API Key

After the setup above, we just need to create a new flow in your workspace and let's use the pre-existing states and triggers.

Creating a flat flow to use with State Machine extensions

In your code editor, copy the snippet below into a python session or into your code.


from transitions import Machine
from iflowly.client import IFlowly

class Matter(object):
    pass

lump = Matter()

iflowly = IFlowly(api_key='MY-API-KEY')
flow = iflowly.get_flow('PyTransition Flow')

states = [ state.name for state in flow.states ]

transitions = [
    { 'trigger':  trigger.name, 'source': trigger.source.name, 'dest': trigger.destination.name }
    for trigger in flow.triggers
]

machine = Machine(lump, states=states, transitions=transitions, initial=flow.initial_state.name)
lump.state # pending
lump.approve() # True
lump.state # approved

You're all set 🚀 🎉

IFlowly is a simple storage hub for our states and triggers, with some extra capabilities that could be done from the UI. Hope you've enjoyed this article, take a look at how to build a simple approval workflow with IFlowly