rsalmei / alive-progress

A new kind of Progress Bar, with real-time throughput, ETA, and very cool animations!
MIT License
5.53k stars 206 forks source link

Displaying messages, disable `on {item_number}:` #271

Closed brianmezini closed 7 months ago

brianmezini commented 7 months ago

So this is not actually an issue with module itself but I guess I'm just too new with it.

My use case: I have a Python script that allows me to run a bunch of pipelines at the same time so I dont have to go and click each one of them manually. While running, the script displays a table (I'm using tabulate) with information for all the pipelines being run, like the status, the result, name and URL. This table is updated in "real time" (I'm using print("\n\033[H\033[J") to clear the screen). Now I wanted to add a progress bar to show me how many the pipelines are completed... so enter alive-progress. I got it to work exactly like I wanted but there's one visual issue that I don't like. Every time the table is refreshed it also shows on {item_number}: pushing the table to the right. I tried using bar.text but that only prints the header of the table.

My question is: Is there a way to disable the on {item_number}: when I print something from within the alive_bar context manager?

The relevant code:

class PipelineRun
    ...

    @staticmethod
    async def wait_pipelines(pipeline_runs: list["PipelineRun"]):

        group = asyncio.gather(
            *[pipeline_run.wait() for pipeline_run in pipeline_runs if pipeline_run.state != "FAILED_TO_START"]
        )

        with alive_bar(len(pipeline_runs), manual=True, unit=" pipelines", receipt=False) as bar:
            while True:
                if not group.done():
                    await asyncio.sleep(1)
                    PipelineRun.print_results_table(pipeline_runs, bar)
                else:
                    break

    @staticmethod
    def print_results_table(pipeline_runs: list["PipelineRun"], bar: Optional[Callable] = None):
        table = []
        table_headers = ["Repository", "Pipeline", "Branch", "Build #", "Status", "Result", "URL"]

        for pipeline_run in pipeline_runs:
            table.append([
                pipeline_run.repo,
                pipeline_run.pipeline,
                pipeline_run.branch,
                pipeline_run.build_number,
                pipeline_run.state,
                pipeline_run.result,
                pipeline_run.ui_url
            ])

        total_pipelines     = len(pipeline_runs)
        completed_pipelines = len([pipeline_run for pipeline_run in pipeline_runs if pipeline_run.state == "COMPLETED"])
        failed_to_start     = len([pipeline_run for pipeline_run in pipeline_runs if pipeline_run.state == "FAILED_TO_START"])

        print("\033[H\033[J") # clear screen
        print("\nWaiting for pipelines to complete...\n")

        print(tabulate(table, headers=table_headers))
        print("\n")

        if bar is not None:
            bar((completed_pipelines+failed_to_start)/total_pipelines)

Sample output:

image
TheTechRobo commented 7 months ago

Add "enrich_print=False" to the alive_bar function.

brianmezini commented 7 months ago

Feeling dumb rn... I even read the documentation thoroughly and did not manage to figure that out... Thank you @TheTechRobo !