tqdm progress bar for Mojo?

I cant get tqdm to work via Python interface , so i am wondering if any of you managed to use it in Mojo or if we have something similar already implemented in Mojo. Thx.
GitHub
GitHub - tqdm/tqdm: :zap: A Fast, Extensible Progress Bar for Pytho...
:zap: A Fast, Extensible Progress Bar for Python and CLI - tqdm/tqdm
12 Replies
benny
benny7mo ago
i’m thinking this is possible with some c calls but i’ll get back to you could be a good use case for the glibc project i was working on
Martin Dudek
Martin DudekOP7mo ago
Didnt you have something like that in Vodoo? Somewhere i saw something similar, cant recall right now where.
benny
benny7mo ago
oh you are right lol forgot about that yes it should be in the code still somewhere that’s how i left it i just haven’t updated in a while so i dont think it’ll run perfect
Martin Dudek
Martin DudekOP7mo ago
here what i came up with for now based on @benny's Voodoo magic
from time.time import sleep

fn progress_bar(n:Int,callback:fn(Int)->None, prefix:String='', bar_size:Int=60):
"""
A simple progress bar.

:param n: The number of iterations.
:param callback: Function to call in each iteration.
:param prefix: Prefix string to display before the progress bar.
:param size: The size of the progress bar.
"""

fn clear():
print(chr(27) + "[2J",end="")

var n_size = len(str(n))
var space = ""
if len(prefix)>0:
space = " "

@parameter
fn show(step:Int):
var bar:String=space
for j in range(bar_size):
if j < int((step * bar_size) / n):
bar += "█"
else:
bar += "░"

for _ in range(n_size-len(str(step))):
bar += " "

clear()
print("\n" + prefix + bar + " " + step + "/" + n + " ",end="")

show(0)
for step in range(n):
callback(step)
show(step+1)

fn one_step(i:Int):
sleep(0.02)

fn main():
progress_bar(n=167,callback=one_step, prefix='Epoch:',bar_size=50)
from time.time import sleep

fn progress_bar(n:Int,callback:fn(Int)->None, prefix:String='', bar_size:Int=60):
"""
A simple progress bar.

:param n: The number of iterations.
:param callback: Function to call in each iteration.
:param prefix: Prefix string to display before the progress bar.
:param size: The size of the progress bar.
"""

fn clear():
print(chr(27) + "[2J",end="")

var n_size = len(str(n))
var space = ""
if len(prefix)>0:
space = " "

@parameter
fn show(step:Int):
var bar:String=space
for j in range(bar_size):
if j < int((step * bar_size) / n):
bar += "█"
else:
bar += "░"

for _ in range(n_size-len(str(step))):
bar += " "

clear()
print("\n" + prefix + bar + " " + step + "/" + n + " ",end="")

show(0)
for step in range(n):
callback(step)
show(step+1)

fn one_step(i:Int):
sleep(0.02)

fn main():
progress_bar(n=167,callback=one_step, prefix='Epoch:',bar_size=50)
Thx @benny Just found out that instead of clearing the whole screen, \r might be a good alternative for this :
from time.time import sleep

fn progress_bar(n:Int,callback:fn(Int)->None, prefix:String='', bar_size:Int=60):
"""
A simple progress bar.

:param n: The number of iterations.
:param callback: Function to call in each iteration.
:param prefix: Prefix string to display before the progress bar.
:param size: The size of the progress bar.
"""

var n_size = len(str(n))
var space = " " if len(prefix)>0 else ""

@parameter
fn show(step:Int):
var bar:String=space
for j in range(bar_size):
if j < int((step * bar_size) / n):
bar += "█"
else:
bar += "░"

for _ in range(n_size-len(str(step))):
bar += " "

print("\r" + prefix + bar + " " + step + "/" + n + " ",end="")

show(0)
for step in range(n):
callback(step)
show(step+1)

fn one_step(i:Int):
sleep(0.02)

fn main():
progress_bar(n=167,callback=one_step, prefix='Epoch:',bar_size=50)
from time.time import sleep

fn progress_bar(n:Int,callback:fn(Int)->None, prefix:String='', bar_size:Int=60):
"""
A simple progress bar.

:param n: The number of iterations.
:param callback: Function to call in each iteration.
:param prefix: Prefix string to display before the progress bar.
:param size: The size of the progress bar.
"""

var n_size = len(str(n))
var space = " " if len(prefix)>0 else ""

@parameter
fn show(step:Int):
var bar:String=space
for j in range(bar_size):
if j < int((step * bar_size) / n):
bar += "█"
else:
bar += "░"

for _ in range(n_size-len(str(step))):
bar += " "

print("\r" + prefix + bar + " " + step + "/" + n + " ",end="")

show(0)
for step in range(n):
callback(step)
show(step+1)

fn one_step(i:Int):
sleep(0.02)

fn main():
progress_bar(n=167,callback=one_step, prefix='Epoch:',bar_size=50)
Martin Dudek
Martin DudekOP7mo ago
...
No description
Ryulord
Ryulord7mo ago
I actually made one recently. Just hadn't gotten around to putting it up in the community section yet. https://github.com/Ryul0rd/awdy
GitHub
GitHub - Ryul0rd/awdy: Are We Done Yet (awdy) is a tqdm-like progre...
Are We Done Yet (awdy) is a tqdm-like progress bar written in pure Mojo. - Ryul0rd/awdy
Martin Dudek
Martin DudekOP7mo ago
Fantastic. looking forward to see how to use it. Hillarious name, brilliant. Funny enough i improved on my code a little bit and put it on github https://github.com/dorjeduck/progressbar.mojo your implementation looks definitely more refined. I link from my repo to yours as this is the one people should use. For now I will leave my one on github ...
GitHub
GitHub - dorjeduck/progressbar.mojo: Basic Progress Bar for Mojo
Basic Progress Bar for Mojo. Contribute to dorjeduck/progressbar.mojo development by creating an account on GitHub.
toasty
toasty7mo ago
If you need some ansi terminal funcs, I have a bunch here: https://github.com/thatstoasty/mist/blob/nightly/mist/screen.mojo It seems like you're already handling cursor movement and screen clearing, but in case you need some text coloring/formatting as well this should be usable. Unless nightly broke it again 😅
Martin Dudek
Martin DudekOP7mo ago
great resource, thanks for sharing
Ryulord
Ryulord7mo ago
yeah I saw this and it looks nice but I didn't want to build a library with dependencies while mojo is both unstable and lacking an official package manager
toasty
toasty7mo ago
Agreed, it’s painful refactoring everything plus dependencies for some releases. Fingers crossed for some package management news soon
Martin Dudek
Martin DudekOP7mo ago
i wrapped up https://github.com/dorjeduck/progressbar.mojo today. It went partially other ways then expected, now including the possibility to "vectorize" the loop and runtime customization. Hopefully Mojo includes an Iterable trait soon, so @Ryulord's awdy can become even more tqdm like. Wonder if vectorizealso becomes like a wrapper for Iterable at one point.
GitHub
GitHub - dorjeduck/progressbar.mojo: Basic Progress Bar for Mojo
Basic Progress Bar for Mojo. Contribute to dorjeduck/progressbar.mojo development by creating an account on GitHub.
Want results from more Discord servers?
Add your server