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

Allow replacement of cell outputs by index #1417

Closed
seantur opened this issue May 20, 2024 · 1 comment · Fixed by #1466
Closed

Allow replacement of cell outputs by index #1417

seantur opened this issue May 20, 2024 · 1 comment · Fixed by #1466
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@seantur
Copy link
Contributor

seantur commented May 20, 2024

Description

I would like to be able to replace a certain index of a cell output. My use case is that I want to be able to have a progress bar at output index 0, and occasionally update a plot at output index 1. If I clear the output to show the new plot, I lose the progress bar.

I would be happy to submit a PR if there's an API for this that you like.

Suggested solution

I've been using this, which seems to work okay (modified from the functions in _output.py):

def replace_output_idx(value: object, idx: int):
    ctx = get_context()
    if ctx.execution_context is None:
        return

    if len(ctx.execution_context.output) < idx:
        raise Exception
    elif len(ctx.execution_context.output) == idx:
        ctx.execution_context.output.append(formatting.as_html(value))
    else:
        current_fig = ctx.execution_context.output[idx]
        ctx.execution_context.output[idx] = formatting.as_html(value)
    write_internal(cell_id=ctx.execution_context.cell_id, value=vstack(ctx.execution_context.output))

Alternative

Another approach that would be a little bit more work is to make mo.output behave more like a list with a __getitem__ method, and maybe an insert method:

mo.output.append(1)

mo.output[0] = 2

mo.output.insert(1, 3)

mo.output.clear()

Additional context

No response

@mscolnick
Copy link
Contributor

@seantur I think a mo.output.replace_at_index. I do like the list-api on it, but mo.output[0] = 2 looks like a mutation and marimo tries to push for more more immutable practices, so that could be confusing. if we want to later, we can make it a list-like api.

A contribution would be greatly appreciated!

@akshayka akshayka added enhancement New feature or request good first issue Good for newcomers labels May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants