tqdm-loggable
tqdm-loggable is a petite Python package (current version 0.4.1) that provides logging-friendly TQDM progress bars. It automatically converts standard tqdm progress bars into periodic log messages when running in non-interactive or headless environments (e.g., Docker, CI/CD, background workers), ensuring that progress is visible in logs rather than being hidden by a lack of a TTY. This prevents applications from appearing frozen during long-running tqdm operations by sending regular progress reports to the configured logging backend. The project has low activity on GitHub, suggesting an infrequent release cadence.
Warnings
- gotcha Do not confuse `tqdm-loggable` with `tqdm.contrib.logging`. They address different problems: `tqdm-loggable` converts progress bars to log messages in headless environments, while `tqdm.contrib.logging` redirects console log output to `tqdm.write()` to prevent interference with interactive bars.
- gotcha tqdm-loggable automatically detects interactive vs. non-interactive sessions to determine whether to show an interactive bar or log messages. This auto-detection can be overridden by setting the `TQDM_LOGGABLE_FORCE` environment variable to `stdout` (force interactive), `logging` (force log output), or `auto` (restore default behavior).
- gotcha The GitHub repository for `tqdm-loggable` indicates low activity. This means new feature requests or bug reports might experience slower response times.
- breaking When migrating from standard `tqdm` to `tqdm-loggable` in a headless environment, the expected output will change from no visible progress (or broken output) to periodic log messages. While this is the library's intended function, it is a significant behavioral change from `tqdm`'s default in non-interactive contexts.
Install
-
pip install tqdm-loggable
Imports
- tqdm
from tqdm_loggable.auto import tqdm
- tqdm_logging
from tqdm_loggable.tqdm_logging import tqdm_logging
Quickstart
import datetime
import logging
import time
import os
from tqdm_loggable.auto import tqdm
from tqdm_loggable.tqdm_logging import tqdm_logging
# Configure basic logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
# Optional: Set tqdm-loggable's log level and update rate
tqdm_logging.set_level(logging.INFO)
tqdm_logging.set_log_rate(datetime.timedelta(seconds=2))
# You can force logging mode by setting an environment variable:
# os.environ['TQDM_LOGGABLE_FORCE'] = 'logging' # Forcing log output
# os.environ['TQDM_LOGGABLE_FORCE'] = 'stdout' # Forcing console bar
# os.environ['TQDM_LOGGABLE_FORCE'] = 'auto' # Restore auto-detection
print("Starting a simulated task...")
for i in tqdm(range(20), desc="Processing items", unit="item"):
time.sleep(0.5)
if i == 5:
logger.info("Mid-task progress report")
print("Task completed.")