chriskiehl / Gooey

Turn (almost) any Python command line program into a full GUI application with one line

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Progress bar time remaining goes missing when progress_regex not a match

simularis opened this issue · comments

  • OS: windows 11 x64
  • Python Version: 3.10.6
  • Gooey Version: 1.2.1-release branch
  • Thorough description of problem
    • Expected Behavior
      When using progress bar and 'show_time_remaining':True, console lines not matching the progress_regex should be ignored and not affect the time remaining display based on last matching progress update.
    • Actual Behavior
      Console lines not matching the progress_regex cause the time remaining to disappear until the next matching line.
  • A minimal code example
  • Screenshot (if visual quirk)
  • Anything else you may think will be helpful

Minimal code example

from time import sleep
from gooey import Gooey, GooeyParser
from itertools import cycle

@Gooey(
    terminal_font_family='Courier New',
    progress_regex=r"^progress: (?P<current>-?\d+)/(?P<total>\d+)$",
    progress_expr="current / total * 100",
    timing_options={
        'show_time_remaining':True,
        'hide_time_remaining_on_complete':False
    })
def parse_args():
    parser = GooeyParser(prog="example_progress_bar_5")
    parser.add_argument("alpha", type=float, help="Seconds per 10% progress", default=1)
    parser.add_argument("beta", type=int, help="Extra messages between progress updates",default=3)
    parser.add_argument("gamma", type=str, nargs='+', help="List of your messages",
        default=["info ...", "message ...", "warning ..."])
    args = parser.parse_args()
    return args.alpha, args.beta, args.gamma

def main():
    alpha, beta, gamma = parse_args()
    myprocess = cycle(gamma)

    print("Step 1")
    for i in range(0, 51, 10):
        sleep(alpha)
        print("progress: {}/{}".format(i,100),flush=True)

    print("Step 2")
    for i in range(50, 101, 10):
        sleep(alpha / (beta+1))
        for j in range(beta):
            # some extra text before progress
            print(next(myprocess),flush=True)
            sleep(alpha / (beta+1))
        print("progress: {}/{}".format(i,100),flush=True)

if __name__ == "__main__":
    main()

Screenshots

What Screenshot
Console Screenshot 2022-12-15 172822
Progress bar during step 1 (progress messages only) Screenshot 2022-12-15 172725
Progress bar during step 2 (progress messages and other messages) Screenshot 2022-12-15 172751

Relevant excerpts

ProcessController

def _forward_stdout(self, process):
'''
Reads the stdout of `process` and forwards lines and progress
to any interested subscribers
'''
while True:
line = process.stdout.readline()
if not line:
break
_progress = self._extract_progress(line)
pub.send_message(events.PROGRESS_UPDATE, progress=_progress)
if _progress is None or self.hide_progress_msg is False:
pub.send_message(events.CONSOLE_UPDATE,
msg=line.decode(self.encoding))
pub.send_message(events.EXECUTION_COMPLETE)

def _eval_progress(self, match):
'''
Runs the user-supplied progress calculation rule
'''
_locals = {k: safe_float(v) for k, v in match.groupdict().items()}
if "x" not in _locals:
_locals["x"] = [safe_float(x) for x in match.groups()]
try:
return int(eval(self.progress_expr, {}, _locals))
except:
return None

Timing

def _updateEstimate(self, *args, **kwargs):
prog = kwargs.get('progress')
if(not prog):
self.estimatedRemaining = None
return
if(prog > 0):
self.estimatedRemaining = estimate_time_remaining(prog,self.startTime)

Suggested fix

Update gooey/gui/util/time.py:

@@ -19,7 +19,7 @@ class Timing(object):
     def _updateEstimate(self, *args, **kwargs):
         prog = kwargs.get('progress')
         if(not prog): 
-            self.estimatedRemaining = None
+            # self.estimatedRemaining = None
             return
         if(prog > 0):
             self.estimatedRemaining = estimate_time_remaining(prog,self.startTime)