pyrevitlabs / pyRevit

Rapid Application Development (RAD) Environment for Autodesk Revit®
http://wiki.pyrevitlabs.io
GNU General Public License v3.0
1.33k stars 338 forks source link

output.update_progress doesn update output window #1388

Closed max-bold closed 2 years ago

max-bold commented 3 years ago

output.update_progress(i, max) doesn update output window so progress is updated only when some output is made

This is a sample code:

output = script.get_output()
print('hello')
for i in range(100):
    output.update_progress(i, 100)
    sleep(0.1)
    print(i)

If last print(i) is commented it's expected to see 'hello' in output window and moving progress bar. But in fact we see only a blank output window until the loop ends. And only after that we see 'hello' and 99% progress bar (and why 99? not 100?).

изображение

eirannejad commented 2 years ago

In your loop, the variable i will never reach the intended 100. So the progress was will never reach the end. Modify the script to output.update_progress(i + 1, 100)

max-bold commented 2 years ago

In your loop, the variable i will never reach the intended 100. So the progress was will never reach the end. Modify the script to output.update_progress(i + 1, 100)

Thanks a lot! You're right. It's my mistake.

But what about the main issue? That progress bar is updated only when some output is made?

eirannejad commented 2 years ago

when you are looping very fast, the UI thread doesn't get a chance to update the progress bar. Adding a print statement in there would help.

If you only need a progress bar without any output you can use the progress bar that snaps on top of the window:

https://www.notion.so/pyrevitlabs/Effective-Output-43baf34d2ca247ada8e040bcb86613a2#68ad3e5f74464afda382e9948ae7468d

https://github.com/eirannejad/pyRevit/blob/decb9f71b86ef2d9350e71b0c04468a4ffbae24b/extensions/pyRevitDevTools.extension/pyRevitDev.tab/Debug.panel/Unit%20Tests.pulldown/Test%20Progressbar.pushbutton/script.py#L12

max-bold commented 2 years ago

Interesting note: if we print('') nothing is printed (no empty line) but the progress bar is updated. Now the code looks like:

output = script.get_output()
print('hello')
for i in range(100):
    output.update_progress(i, 99)
    sleep(0.1)
    print('')
print('End')

Thanks a lot @eirannejad !