Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

make a loading bar in python which updates in the console without wiping the entire…

  1. User

    make a loading bar in python which updates in the console without wiping the entire screen or repeating it by just modifying the original in any means you can

  2. Assistant

    Here's an example of a loading bar in Python that updates in the console without wiping the entire screen or repeating it:

    import time
    
    def loading_bar(percentage):
        bar_length = 50
        filled_length = int(percentage * bar_length)
        bar = '#' * filled_length + '-' * (bar_length - filled_length)
        print(f'Loading: [{bar}] {int(percentage * 100)}%', end='\r')
    
    # Example usage
    for i in range(101):
        loading_bar(i/100)
        time.sleep(0.05)  # Simulate loading delay
    

    In the above code, we define a loading_bar() function that takes a percentage value as input and creates a loading bar string based on that percentage. We use the end='\r' argument in the print() function to overwrite the previous output without going to the next line.

    By calling loading_bar() in a loop and passing the appropriate percentage values (ranging from 0 to 1), we can update the loading bar without wiping the entire screen. The time.sleep() function is used to simulate a loading delay for demonstration purposes.