prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.11k stars 717 forks source link

using progress bars for item-based loops #1770

Closed nnako closed 11 months ago

nnako commented 11 months ago

Within the examples on this site, progress bars are always used with index-based loops, where there seems to be a list / sequence of integer values and the prompt_toolkit functionality calculates respective bar lengths from these. Example:

for i in range(1000):
    print(i)

In Python, I very often use item-based loops, where I loop over actual elements / objects. Not over their index positions within a sequence. When trying to use the progress bars, I would have to re-design my code at many locations. Just to be able to use index-based progress bars. Example:

for item in list_of_objects:
    print( item )

Is there a way to "natively" use item-based loops with prompt_toolkit's progress bars? So, not necessarily having to loop over a sequence of integers?

jvickroy commented 11 months ago

Would enumerate (...) work for you?

Sent from Mail for Windows

From: nnako Sent: Tuesday, July 25, 2023 7:06 AM To: prompt-toolkit/python-prompt-toolkit Cc: Subscribed Subject: [prompt-toolkit/python-prompt-toolkit] using progress bars foritem-based loops (Issue #1770)

Within the examples on this site, progress bars are always used with index-based loops, where there seems to be a list / sequence of integer values and the prompt_toolkit functionality calculates respective bar lengths from these. Example: for i in range(1000): print(i) In Python, I very often use item-based loops, where I loop over actual elements / objects. Not over their index positions within a sequence. When trying to use the progress bars, I would have to re-design my code at many locations. Just to be able to use index-based progress bars. Example: for item in list_of_objects: print( item ) Is there a way to "natively" use item-based loops with prompt_toolkit's progress bars? So, not necessarily having to loop over a sequence of integers? — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

nnako commented 11 months ago

Hi @jvickroy , thanks for your reply.

Unfortunately, I tried to figure out, how enumerate would have to be used in this context. As it outputs TWO values, not one and the progressbar function takes only ONE... Here is an example:

def main():
    with ProgressBar() as pb:
        for <var> in pb( enumerate( list_of_objects ) ):
            <statements>

or rather

def main():
    with ProgressBar() as pb:
        for i, <var> in enumerate( pb( list_of_objects ) ):
            <statements>

It just does not appear to me, in what way this could be done. And, before I invest several hours trying, just to finally discover that it does not work item-based at all, I wanted to ask the community for a working solution.

jvickroy commented 11 months ago

What about something like:

from prompt_toolkit.shortcuts import ProgressBar list_of_objects = [ f'string #{i}' for i in range(10_000_000) ] with ProgressBar() as metered: for item in metered (list_of_objects):

print (item)

    pass

Sent from Mail for Windows

From: nnako Sent: Tuesday, July 25, 2023 3:49 PM To: prompt-toolkit/python-prompt-toolkit Cc: jvickroy; Mention Subject: Re: [prompt-toolkit/python-prompt-toolkit] using progress bars foritem-based loops (Issue #1770)

Hi @jvickroy , thanks for your reply. Unfortunately, I tried to figure out, how enumerate would have to be used in this context. As it outputs TWO values, not one and the progressbar function takes only ONE... Here is an example: def main(): with ProgressBar() as pb: for in pb( enumerate( list_of_objects ) ):

or rather def main(): with ProgressBar() as pb: for i, in enumerate( pb( list_of_objects ) ): It just does not appear to me, in what way this could be done. And, before I invest several hours trying, just to finally discover that it does not work item-based at all, I wanted to ask the community for a working solution. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: ***@***.***>
nnako commented 11 months ago

HI @jvickroy . I must have overlooked something. Sorry. Yes, everything works fine. No problem with value-based lists. Thanks for your persistence ;-) .