You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there an accepted way to show the tqdm progress bar in a curses screen? This works, but feels very hacky:
importcursesfromtqdmimporttqdmclass_CursesIO:
"""Class faking io.StringIO so we can print tqdm output to a curses screen. Parameters ---------- stdscr : curses.Window The window to print to. x0 : int The x position at which to print. y0 : int The y position at which to print. """def__init__(self, stdscr, x0, y0):
self.stdscr=stdscrself.x0=x0self.y0=y0self.buffer=""# The string to writedefwrite(self, s):
"""Replace `self.buffer` with `s` and return number of characters 'written'."""self.buffer=sreturnlen(s)
defflush(self):
"""Print `self.buffer` to `self.stdscr` and clear buffer contents."""self.stdscr.addstr(self.y0, self.x0, self.buffer)
self.stdscr.refresh()
self.buffer=""defmain(stdscr):
height, width=stdscr.getmaxyx() # get dimensions of screencurses_file=_CursesIO(stdscr, 0, 0)
foriintqdm(range(100), file=curses_file, ascii=False, ncols=width):
curses.napms(100) # sleep for 100 msif__name__=="__main__":
curses.wrapper(main)
Also, if I use tqdm like this in a list comprehension like so,
defmain(stdscr):
height, width=stdscr.getmaxyx() # get dimensions of screencurses_file=_CursesIO(stdscr, 0, 0)
l= [curses.napms(100) foriintqdm(range(100), file=curses_file, ascii=False, ncols=width)]
if__name__=="__main__":
curses.wrapper(main)
and try to interrupt with Ctrl-C, curses doesn't exit cleanly, as if I didn't use curses.wrapper (the window contents are still shown on the terminal, and text I type doesn't show up).
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi all,
Is there an accepted way to show the tqdm progress bar in a curses screen? This works, but feels very hacky:
Also, if I use tqdm like this in a list comprehension like so,
and try to interrupt with Ctrl-C, curses doesn't exit cleanly, as if I didn't use
curses.wrapper
(the window contents are still shown on the terminal, and text I type doesn't show up).Is there a better way to use tqdm with curses?
Beta Was this translation helpful? Give feedback.
All reactions