Skip to content

Instantly share code, notes, and snippets.

@andenacitelli
Last active April 12, 2024 20:19
Show Gist options
  • Save andenacitelli/20a98f8c45499fe21d55266b776d3071 to your computer and use it in GitHub Desktop.
Save andenacitelli/20a98f8c45499fe21d55266b776d3071 to your computer and use it in GitHub Desktop.
RegEx to extract TQDM information from text-only progress bar representation

Useful RegEx to extract information from a text-only TQDM stream. I personally found this useful to extract progress data from a Python child process and use it to update a progress bar in my front-end Electron UI.

The RegEx:

(.*): *(\d+%).*(\d+\/\d+) +\[(\d+:\d+)<(\d+:\d+), +(\d+.\d+.*\/s)\]

Capture Groups:

  1. Label
  2. Percent Complete
  3. Count (i.e. 13/15)
  4. Time Elapsed
  5. Estimated Time Remaining
  6. Rate

Capture Groups Example (credit: regexr.com): image

Example JS Usage:

const stringToTest = '' // Whatever string you're testing
const regex = /(.*): *(\d+%).*(\d+\/\d+) +\[(\d+:\d+)<(\d+:\d+), +(\d+.\d+.*\/s)\]/
const match = stringToTest.match(tqdmRegex);
const label = match[1];
const percent = match[2];
const count = match[3]; // Only the percent and label are really "necessary" - these are still available for easy addition in the future
const elapsed = match[4];
const left = match[5];
const rate = match[6];
console.log(`${label}: ${percent} (${elapsed} elapsed, ${left} left, ${rate}/s)`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment