Search code examples
pythoninkscape

How can I make a progress bar for Inkscape extension?


My Inkscape extension written in Python does some extensive work that takes rather long time. How can I add a progress bar to show a current percent of processed data and "Cancel" button?


Solution

  • Create a call back function that is called after every operation.

    Consider the following scenario. Imagine do_work is the method that is worker function. Create a function fallback that will be called after every operation for which you need a status update.

    def do_work(*args, **kwargs, fallback=None):
    
        while processing_some_condition:
    
            # You will need to find a way to get your total data value
            total_data = total_value
    
            """
                Do your processing call with *args & **kwargs
                ....
                ....
                ....
                ....
            """
    
            elapsed_data = some_value  # Get the remaining amount of data
    
            if elapsed_data == total_data:
                break
    
            if fallback:
                fallback(elapsed, total)
    
        return your_result
    

    Now, comes the question of how would you implement the fallback to show a progress bar. Here is a gist that explains that https://gist.github.com/ab9-er/843d1af20049e72e2016

    Another simple fallback could be something as simple as this

    def fallback(elapsed, total):
        tx_pc = lambda chunk, full: chunk * 100 / full
        print str(tx_pc(elapsed, total)) + "% operation completed"
    
        if tx_pc(elapsed, total) == 100:
            print "Operation complete: 100%"
    

    Hope this helps. Let me know if it doesn't.