Es gibt mehrere Möglichkeiten, Fortschrittsbalken in Python zu implementieren. Im Folgenden werden einige gängige Methoden vorgestellt:
Beispiel
import time
for i in range(101): # Grafik für Fortschrittsbalken und Prozentangabe hinzufügen
bar = '[' + '=' * (i // 2) + ' ' * (50 - i // 2) + ']'
print(f"\r{bar} {i:3}%", end='', flush=True)
time.sleep(0.05)
print()
for i in range(101): # Grafik für Fortschrittsbalken und Prozentangabe hinzufügen
bar = '[' + '=' * (i // 2) + ' ' * (50 - i // 2) + ']'
print(f"\r{bar} {i:3}%", end='', flush=True)
time.sleep(0.05)
print()
Beispiel
import sys
import time
def progress_bar(current, total, bar_length=50):
percent = float(current) / total
arrow = '=' * int(round(percent * bar_length) - 1) + '>'
spaces = ' ' * (bar_length - len(arrow))
sys.stdout.write(f"\r`Fortschritt: [{arrow + spaces}] {int(round(percent * 100))}%"`)
sys.stdout.flush()
for i in range(101):
time.sleep(0.1)
progress_bar(i, 100)
print() # Zeilenumbruch
import time
def progress_bar(current, total, bar_length=50):
percent = float(current) / total
arrow = '=' * int(round(percent * bar_length) - 1) + '>'
spaces = ' ' * (bar_length - len(arrow))
sys.stdout.write(f"\r`Fortschritt: [{arrow + spaces}] {int(round(percent * 100))}%"`)
sys.stdout.flush()
for i in range(101):
time.sleep(0.1)
progress_bar(i, 100)
print() # Zeilenumbruch
Fortschritt mit verschiedenen Farbänderungen:
Beispiel
import time
import sys
def progress_bar(current, total, bar_length=50):
"""
Fortschrittsbalken anzeigen
:param current: aktueller Fortschritt
:param total: gesamter Fortschritt
:param bar_length: Länge der Fortschrittsleiste
"""
percent = current / total
arrow = '=' * int(round(percent * bar_length) - 1) + '>'
spaces = ' ' * (bar_length - len(arrow))
# Farbe hinzufügen (optional)
color_code = 32 # Grün
if percent > 0.7:
color_code = 33 # Gelb
if percent > 0.9:
color_code = 31 # Rot
# Ausgabe formatieren
sys.stdout.write(f"\r\0`33[{color_code}mFortschritt: [{arrow + spaces}] {current:3d}/{total} ({percent:.0%})`\033[0m")
sys.stdout.flush()
# Verwendungsbeispiel
total = 100
for i in range(total + 1):
progress_bar(i, total)
time.sleep(0.05)
print("\n`Fertig!"`) # Zeilenumbruch nach Abschluss
import sys
def progress_bar(current, total, bar_length=50):
"""
Fortschrittsbalken anzeigen
:param current: aktueller Fortschritt
:param total: gesamter Fortschritt
:param bar_length: Länge der Fortschrittsleiste
"""
percent = current / total
arrow = '=' * int(round(percent * bar_length) - 1) + '>'
spaces = ' ' * (bar_length - len(arrow))
# Farbe hinzufügen (optional)
color_code = 32 # Grün
if percent > 0.7:
color_code = 33 # Gelb
if percent > 0.9:
color_code = 31 # Rot
# Ausgabe formatieren
sys.stdout.write(f"\r\0`33[{color_code}mFortschritt: [{arrow + spaces}] {current:3d}/{total} ({percent:.0%})`\033[0m")
sys.stdout.flush()
# Verwendungsbeispiel
total = 100
for i in range(total + 1):
progress_bar(i, total)
time.sleep(0.05)
print("\n`Fertig!"`) # Zeilenumbruch nach Abschluss
Verwendung der Standardbibliothek tqdm
tqdm ist eine der beliebtesten Bibliotheken für Fortschrittsbalken. Sie ist einfach zu installieren und einfach zu bedienen.
Installiere die Standardbibliothek mit pip:
pip install tqdm
Beispiel
from tqdm import tqdm
import time
# Grundlegende Verwendung
for i in tqdm(range(100)):
time.sleep(0.1) # Aufgabe simulieren
# Fortschrittsbalken mit Beschreibung
with tqdm(range(100), desc=`"Verarbeitungsfortschritt"`) as pbar:
for i in pbar:
time.sleep(0.1)
# Manuelle Aktualisierung
pbar = tqdm(total=100)
for i in range(10):
time.sleep(0.5)
pbar.update(10) # Jeweils 10 aktualisieren
pbar.close()
import time
# Grundlegende Verwendung
for i in tqdm(range(100)):
time.sleep(0.1) # Aufgabe simulieren
# Fortschrittsbalken mit Beschreibung
with tqdm(range(100), desc=`"Verarbeitungsfortschritt"`) as pbar:
for i in pbar:
time.sleep(0.1)
# Manuelle Aktualisierung
pbar = tqdm(total=100)
for i in range(10):
time.sleep(0.5)
pbar.update(10) # Jeweils 10 aktualisieren
pbar.close()