Skip to content

allow looping with conditionals #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: switch-split
Choose a base branch
from

Conversation

mt-ob
Copy link
Owner

@mt-ob mt-ob commented Aug 7, 2025

from metaflow import FlowSpec, step

class LoopingFlow(FlowSpec):
    @step
    def start(self):
        print("Flow starting. Initializing loop counter.")
        # This variable will track our position in the loop.
        self.count = 0
        # We will loop 5 times.
        self.max_iterations = 5
        self.next(self.loop_step)

    @step
    def loop_step(self):
        print(f"Executing loop iteration number: {self.count}")
        self.count += 1
        self.loop_status = 'continue' if self.count < self.max_iterations else 'exit'
        print(f"Loop status is '{self.loop_status}'. Deciding next step...")
        self.next(
            {
                'continue': self.loop_step,
                'exit': self.exit_loop
            },
            condition='loop_status'
        )

    @step
    def exit_loop(self):
        print(f"Loop finished after {self.count} iterations.")
        self.next(self.end)

    @step
    def end(self):
        print("Flow has finished successfully.")

if __name__ == '__main__':
    LoopingFlow()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant