3
Good evening, everyone.
I have the following function to create a bar Progress:
def progress_bar(p_value, p_max: int):
"""
:70 = É o tamanho da barra
:param p_value: Valor que está sendo iterado de um laço de repetição
:param p_max: É a quantidade total do dados que está sendo iterado
:return:
"""
chars = int(p_value * 40 / float(p_max))
percent = int((p_value / float(p_max)) * 100)
sys.stdout.write(" ") + sys.stdout.write("#" * chars)
sys.stdout.write(" " * (40 - chars + 2))
if p_value >= p_max:
sys.stdout.write("done. \n\n")
else:
sys.stdout.write("[%3i%%]\r" % percent)
sys.stdout.flush()
Now I need to call it inside the loop below so that the bar is inside the frame created by the top and bottom print. The top print is fine, creates good, but the bottom one only creates after the loop is finished.
How do I make it all together and load the bar Progress inside the square?
is more for the sake of aesthetics.
print(" ╔════════════════════════════════════════════════════════════════════════════╗")
for i, item in enumerate(p_list):
progress_bar(i + 1, len(p_list))
print(" ║ " + item.ljust(75) + "║")
print(" ╚════════════════════════════════════════════════════════════════════════════╝")
Grateful for the support.
What is the usual value of p_list?
– Murilo Sitonio
The progressi_bar p_list is an integer received by Len(), which in this case comes from a list.
– Cleber Nandi
I guess I didn’t quite understand your question... you want '##############################################done. ' to appear after the last print?
– Murilo Sitonio
No. I need you to create the frame, that is the first print and the last print and continue running the progressi_bar appearing gradually, usually the ########
– Cleber Nandi
'normally ######'....? If you do if p_value > p_max the rectangle is created normally (but without the #####). Do jeito que está ''######################################## done. ' appears before the last two prints (last item in the list and bottom of the rectangle). You can be a little more specific?
– Murilo Sitonio
I don’t know how to be more specific than that. I’m just trying to make the progressi_bar look better. Not to appear the # alone in a vacuum, you see. Make them appear inside a square.
– Cleber Nandi
I think I got it.
– Murilo Sitonio
Good. I’m looking forward to it.. I’m still trying something here too.
– Cleber Nandi
I think I understand the problem, but I don’t know very well the sys library and my test script is exhibiting a strange behavior (it only enters Else if I put a print in there...). Anyway, have you ever tried to work the line that writes ##? Something like sys.stdout.write(" ") + sys.stdout.write(" " + ("#" * chars) + "". ljust(75 - chars) + "")
– Murilo Sitonio
I’ll test that suggestion.
– Cleber Nandi
I found something similar in the English stack but it’s too much for my tic and tic and I couldn’t apply it. https://stackoverflow.com/questions/21239032/print-2-lines-in-the-console-concurrently-in-python
– Cleber Nandi