Skip to main content

Build arbitray workflows with Python!

Project description

carefree-workflow

carefree-workflow is a lightweight library for building arbitray workflows with Python.

Highlights

  • Async: async is by design.
  • Parallel: nodes can be executed in parallel.
  • Powerful: complex locks / logics / dependencies can be handled.
    • You can even perform a loop with loop backs in the workflow!
  • Servable: all nodes, as well as the workflow itself, can be automatically turned into RESTful APIs.
  • Extensible: you can easily extend the library with your own nodes.
  • Serializable: the workflow can be serialized into / deserialized from a single JSON file.
  • Human Readable: the workflow JSON file is human readable and easy to understand.
  • Lightweight: the library is lightweight (core implementation is ~500 lines of code in a single file core.py) and easy to use.

Installation

carefree-workflow requires Python 3.8 or higher.

pip install carefree-workflow

or

git clone https://github.com/carefree0910/carefree-workflow.git
cd carefree-workflow
pip install -e .

Usages

CLI

carefree-workflow is installed with a Command Line Interface (CLI) tool called cflow, which can help you execute / render workflows easily:

Workflow JSON examples can be found in examples/workflows.

cflow execute -f <workflow_json_file>

or

cflow render -f <workflow_json_file>

Detailed information can be found by:

cflow --help

Code

Since carefree-workflow is extensible, you can easily extend the library with your own nodes:

import asyncio

from cflow import *

@Node.register("hello")
class HelloNode(Node):
    async def execute(self):
        name = self.data["name"]
        return {"name": name, "greeting": f"Hello, {name}!"}

async def main():
    flow = (
        Flow()
        .push(HelloNode("A", dict(name="foo")))
        .push(HelloNode("B", dict(name="bar")))
        .push(
            EchoNode(
                "Echo",
                dict(messages=[None, None, "Hello, World!"]),
                injections=[
                    Injection("A", "name", "messages.0"),
                    Injection("B", "greeting", "messages.1"),
                ],
            )
        )
    )
    await flow.execute("Echo")

if __name__ == "__main__":
    asyncio.run(main())

Running the above codes will yield something like:

[17:30:27] foo
           Hello, bar!
           Hello, World!

More code snippets can be found in examples.

Serving

carefree-workflow is designed to be servable, here are some rules:

  • Only nodes with get_schema implemented can be automatically served.
  • Complex results (e.g. PIL.Image) can be handled if get_api_response is implemented.
    • See our template if you are interested!
  • The registered name will be turned into the API endpoint, with the dots (.) being replaced by slashes (/).
    • e.g. if the node is registered with Node.register("foo.bar"), the corresponding API endpoint will be /foo/bar.

You can use our CLI to launch a server under default settings easily:

cflow serve

More options can be found by cflow serve --help.

Examples

  • Code snippets can be found in examples.
  • Custom node templates can be found in cflow/nodes/templates.
  • Workflow JSON files can be found in examples/workflows.
    • These JSON files can be executed by cflow execute -f <workflow_json_file>.
    • More execution options can be found by cflow execute --help.

License

carefree-workflow is MIT licensed, as found in the LICENSE file.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

carefree-workflow-0.1.0.tar.gz (20.6 kB view details)

Uploaded Source

File details

Details for the file carefree-workflow-0.1.0.tar.gz.

File metadata

  • Download URL: carefree-workflow-0.1.0.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for carefree-workflow-0.1.0.tar.gz
Algorithm Hash digest
SHA256 709c26b2812c4ff937ac97dd16a05446407f3daa988c496284f78cd6a71d0417
MD5 220acaebad582a3bf55575100138a44f
BLAKE2b-256 96dc61bb8f3bc2dec4fc54f34dffd5c87619b1cbcc8648b87cb5c1c11ea7fbd6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page