1

When running MetaFlow Flows, tqdm progress bars do not get displayed until the final iteration, which defeats the purpose of measuring progress. Is there a way to force MetaFlow to print out tqdm updates?

crypdick
  • 16,152
  • 7
  • 51
  • 74

1 Answers1

1

The problem is that tqdm writes to stderr, but MetaFlow hides output to stderr. The solution requires two tricks: using a context manager to redirect tqdm outputs to a logger, and setting that logger to write to stderr.

Example:

import logging
import sys
from time import sleep

import tqdm
from metaflow import FlowSpec, step
from tqdm.contrib.logging import tqdm_logging_redirect

# stream to stdout needed because MetaFlow hides output to stderr :C
logging.basicConfig(level=logging.INFO, stream=sys.stdout)


class TQDMFlow(FlowSpec):
    @step
    def start(self):
        print("Training...")
        with tqdm_logging_redirect():  # this context manager redirects tqdm output to logging
            for _ in tqdm.tqdm(range(20)):
                sleep(0.25)
        self.next(self.end)

    @step
    def end(self):
        pass


if __name__ == "__main__":
    TQDMFlow()

I also tried redirecting output directly to stdout (without the tqdm_logging_redirect context manager or logging) using tqdm(range(n), file=sys.stdout) but that did not work.

crypdick
  • 16,152
  • 7
  • 51
  • 74
  • 1
    With `tqdm==4.61.1` and `metaflow==2.3.2`, my call to `basicConfig` did not need the argument `stream=sys.stdout`. This is using Python 3.9.5 w/ bash in Ubuntu 20.04. – Rob Hall Jul 11 '21 at 17:51