Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rich.progress support #2846

Open
wd60622 opened this issue Nov 12, 2024 · 4 comments
Open

rich.progress support #2846

wd60622 opened this issue Nov 12, 2024 · 4 comments

Comments

@wd60622
Copy link

wd60622 commented Nov 12, 2024

The rich.progress module provides progress bars for stdout. However, the result is not displayed until after the progress bar is complete which defeats the point.

I've tried many of the examples in the documentation here and I have not had luck with all I have tried.

I tried on the current main branch of the marimo (0.9.17) and version of rich 13.7.0

Example code
import marimo

__generated_with = "0.9.17"
app = marimo.App(width="full")


@app.cell
def __():
    import time
    from rich.progress import track, Progress, BarColumn, TextColumn
    from rich.table import Column

    for i in track(range(10), description="Processing..."):
        time.sleep(1)  # Simulate work being done
    return BarColumn, Column, Progress, TextColumn, i, time, track


@app.cell
def __(Progress, time):
    total = 10

    with Progress() as progress:
        task1 = progress.add_task("[red]Downloading...", total=total)
        task2 = progress.add_task("[green]Processing...", total=total)
        task3 = progress.add_task("[cyan]Cooking...", total=total)

        while not progress.finished:
            progress.update(task1, advance=0.5)
            progress.update(task2, advance=0.3)
            progress.update(task3, advance=0.9)
            time.sleep(0.02)
    return progress, task1, task2, task3, total


@app.cell
def __(Progress, progress):
    def do_work(task):
        print(task)

    with Progress(transient=True) as progress2:
        task = progress.add_task("Working", total=100)
        do_work(task)
    return do_work, progress2, task


@app.cell
def __(BarColumn, Column, Progress, TextColumn, progress, time):
    text_column = TextColumn(
        "{task.description}",
        table_column=Column(ratio=1),
    )
    bar_column = BarColumn(bar_width=None, table_column=Column(ratio=2))

    with Progress(text_column, bar_column, expand=True):
        for n in progress.track(range(10)):
            progress.print(n)
            time.sleep(0.1)
    return bar_column, n, text_column


if __name__ == "__main__":
    app.run()
@mscolnick
Copy link
Contributor

mscolnick commented Nov 12, 2024

We might need to add marimo support to rich directly https://github.com/Textualize/rich/blob/43d3b04725ab9731727fb1126e35980c62f32377/rich/console.py#L517-L534

Would you be open to create an issue on rich?

@wd60622
Copy link
Author

wd60622 commented Nov 12, 2024

We might need to add marimo support to rich directly https://github.com/Textualize/rich/blob/43d3b04725ab9731727fb1126e35980c62f32377/rich/console.py#L517-L534

Would you be open to create an issue on rich?

Sure. What is the logic to detect for marimo?

@mscolnick
Copy link
Contributor

mscolnick commented Nov 12, 2024

@wd60622, there is this logic from another open-source project:

https://github.com/run-house/runhouse/blob/2f80fe25879d7df0f3b8432cf62b3c0519d7ea36/runhouse/resources/module.py#L1073-L1080

        # Check if running in a marimo notebook
        try:
            import marimo
            return marimo.running_in_notebook()
        except (ImportError, ModuleNotFoundError):
            # marimo not installed
            return False

@smutch
Copy link

smutch commented Nov 21, 2024

In the short term, I've been using this to monkey-patch Rich and make it think it is outputting to a terminal:

import rich.console


_orig_console = rich.console.Console


class Console(_orig_console):
    def __init__(self, *args, **kwargs):
        kwargs["force_terminal"] = True
        super().__init__(*args, **kwargs)


rich.console.Console = Console

This allows the progress bar to update in real time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants