-1

If we assume that the following code is a huge and complicated code and lasts for minutes or hours and we want to inform the user how many percents of the code are passing, what should I do?

num=1

for i in range(1,100000):
    num=num*i
print(num)

I want to inform users with progression bar, similar to installing something. I checked here but I did not understand how to write a progression bar depending on my code progression.

In the examples similar to the mentioned link, they are defining the sleep or delaying time. this is not acceptable. Because we do not know the calculation time of Python in different code with different functions.

Community
  • 1
  • 1
David
  • 63
  • 1
  • 11
  • We won't write code for you or give suggestions unless you show what you gave tried so far. – FHTMitchell May 18 '18 at 11:31
  • The question that you link has good solutions to your problem. For example, if you take the function from the accepted answer you would just need to add `printProgressBar(i, 100000)` to your loop. What is it that you cannot work out about them? – jdehesa May 18 '18 at 11:31
  • @FHTMitchell why did you give me negative vote? – David May 18 '18 at 11:39
  • @David probably because _We won't write code for you or give suggestions unless you show what you tried so far._ check [ask], in particular how to provide a [mcve] – Gsk May 18 '18 at 11:44
  • @jdehesa The problem is, they are defining delaying time, while we don't know the amount of calculation time of Python in each step. – David May 18 '18 at 11:45
  • @David I'm not sure I follow. The examples use `sleep` just as a placeholder for actual computation work, you are supposed to replace that with your actual code. – jdehesa May 18 '18 at 11:48

1 Answers1

0

If your index i corresponds to your actuall progress, the tqdm package is a good option. A simple example:

from tqdm import tqdm
import time

for i in tqdm(range(1000)):
    time.sleep(0.01)  # sleep 0.01s

Output:

1%|          | 1010/100000 [00:10<16:46, 98.30it/s]

Edit: The progress bar also works if the progress is not known.

def loop_without_known_length():
    # same as above, but the length is not known outside of this function.
    for i in range(1000):
        yield i

for i in tqdm(loop_without_known_length()):
    time.sleep(0.01)

Output:

60it [00:00, 97.23it/s]
physicsGuy
  • 3,437
  • 3
  • 27
  • 35
  • in this code your are defining the delaying time `0.01` but we do not know how many seconds it lasts to get the answer. even if you write it for my sample simple code it you do not know how much time it is needed to calculate each steps. – David May 18 '18 at 11:36
  • another problem is, this code doea not show a filling bar, it print percents under each other – David May 18 '18 at 11:38
  • @David I added a code snippet which does not display the time. – physicsGuy May 18 '18 at 16:56
  • @David If you print something while the progress bar is running, it will continue in a new line. Otherwise it should in principle show the progress in the same line. – physicsGuy May 18 '18 at 16:59
  • `0%| | 0/1000 [00:00, ?it/s] 1%| | 7/1000 [00:00<00:15, 64.00it/s] 1%|1 | 13/1000 [00:00<00:16, 59.43it/s] 2%|1 | 17/1000 [00:00<00:18, 51.81it/s] 2%|2 | 21/1000 [00:00<00:21, 46.34it/s] 2%|2 | 25/1000 [00:00<00:23, 41.03it/s] 3%|2 | 29/1000 [00:00<00:25, 38.67it/s] 3%|3 | 32/1000 [00:00<00:26, 36.57it/s] 4%|3 | 36/1000 [00:00<00:26, 36.57it/s] ` – David May 18 '18 at 18:42
  • I believe this is a separate issue, and you need to provide more details on where you are printing the output. (Terminal, interactive console, jupyter, etc...) Best open a new question. – physicsGuy May 18 '18 at 19:26