Rich Progress with instantiation and update from another class is failing

45 Views Asked by At

I'm trying to implement a Progress Bar in a class using the Rich module, and I've an issue when the progress is instantiated from another class.

Here is the code I'm using :

class Element:

    progress_bar: Optional[Any] = None
    progress_task: Optional[Any] = None

    def instantiate_progress(self, nb_steps):
        with Progress() as self.progress_bar:
            self.progress_task = self.progress_bar.add_task("[green]Processing...", total=nb_steps)

    def update_progress(self, text):
        try:
            self.progress_bar.update(self.progress_task, advance=1, description=text)
        except Exception as exc:
            console.print(exc)
    
class Test:
      
    def __init__(self) -> None:
        pass

    def launch(self):
        self.element = Element()
        self.element.instantiate_progress(3)
        self.function1()
        self.function2()
        self.function3()
        console.print('FINISHED !!!')
        
    def function1(self):
        <doing stuff>
        self.element.progress_bar.update(self.device.progress_task, advance=1, description='Function 1')
        
    def function2(self):
        <doing stuff>
        self.element.progress_bar.update(self.device.progress_task, advance=1, description='Function 1')
        
    def function3(self):
        <doing stuff>
        self.element.progress_bar.update(self.device.progress_task, advance=1, description='Function 1')

With this code, the progress bar is well displayed in the terminal, but it is never updated (Stays with the first description and to 0%). While if I add some other code in the 'upgrade_progress', this other code is executed successfully - So the function is triggered. And I don't have any Exception raised.

Is there something I'm missing ?

Thanks a lot for your feedbacks.

0

There are 0 best solutions below