Execute functions update Progress bar and Status update

Sharing a function that simplifies the main code by getting a status update message, updating the progress bar and clearing it at the end, posting time spent on each function, and executing each function while optionally returning a value. Thought someone might find it useful. Show bellow if you can improve it.

import streamlit as st

class Progress:
    """Progress bar and Status update."""
    def __init__(self, number_of_functions: int):
        self.n = number_of_functions
        self.bar = st.progress(0)
        self.progress = 1
        self.message = ""
        self.message_container = st.empty()

    def go(self, msg, function, *args, **kwargs):
        self.message += msg
        self.message_container.info(self.message)
        s = t()
        result = function(*args, **kwargs)
        self.message += f" [{t() - s:.2f}s]. "
        self.message_container.info(self.message)
        self.bar.progress(self.progress / self.n)
        self.progress += 1
        if self.progress > self.n:
            self.bar.empty()
        return result

def main():
        p = Progress(number_of_functions=4)
        result1 = p.go("Message 1", function1, arg10, arg11, arg12)
        p.go("Message 2", function2, arg20)
        p.go("Message3", function3)
        result4 = p.go("Message 4", function4, arg40=100)
1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.