Using Python Logging and the Halo Library to Create Friendly CLI Progress Indicators
The article demonstrates how an automation development team can build a simple HTTP download tool in Python that reports its status via standard output, first using the built‑in logging module and then enhancing the user experience with the Halo spinner library for more attractive command‑line feedback.
Automation development teams often need to create test tools that communicate progress to users through standard output, such as showing "begin download", "downloading", and "download complete" messages for an HTTP download utility.
Below is a typical implementation using Python's logging module to emit debug, info, and error messages that indicate the download stages.
# -*- coding: utf-8 -*-
import time
import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
fmt = logging.Formatter("[%(asctime)s] [%(levelname)s] %(message)s")
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(fmt)
logger.addHandler(stream_handler)
"""
Simulate downloading a file from the given URL
using time.sleep as a placeholder for the actual download process
"""
def download(url):
time.sleep(3)
return True
def run():
url = "http://xxx"
logger.debug("begin download url %s" % url)
logger.debug("downloading, plz wait...")
if download(url):
logger.info("download succeed")
else:
logger.error("download failed")
run()The execution prints timestamped log entries that show the start, progress, and result of the simulated download.
To make the progress indication more user‑friendly, the halo library (inspired by Node.js's ora) can be used to display a spinner with success or failure messages.
# -*- coding: utf-8 -*-
import time
import logging
from halo import Halo
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
fmt = logging.Formatter("[%(asctime)s] [%(levelname)s] %(message)s")
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(fmt)
logger.addHandler(stream_handler)
"""
Simulate downloading a file from the given URL
using time.sleep as a placeholder for the actual download process
"""
def download(url):
time.sleep(3)
return True
def run():
url = "http://xxx"
logger.debug("begin download url %s" % url)
spinner = Halo("downloading, plz wait...", spinner="line")
spinner.start()
if download(url):
spinner.succeed("download succeed")
logger.info("download succeed")
else:
spinner.fail("download failed")
logger.error("download failed")
run()The spinner provides a dynamic, visually appealing indication while the download is simulated, and it reports success or failure with colored symbols.
Halo is inspired by the Node.js ora package; its source code is available at https://github.com/ManrajGrover/halo , and it can produce even more striking effects as shown in the accompanying GIFs.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
