Skip to content

Support plots with initial selection #6746

@yairchu

Description

@yairchu

Description

When a Plotly or Altair plot is constructed with an initial selection value, the wrapping mo.ui.plotly/mo.ui.altair objects's ranges and values properties are not populated until the selection is modified by the user.

See example:

import altair as alt
import marimo as mo
import numpy as np
import pandas as pd
import plotly.graph_objects

df = pd.DataFrame(
    {
        "x": np.random.random(100),
        "y": np.random.random(100),
    }
)

plotly_fig = plotly.graph_objects.Figure()
plotly_fig.add_scatter(
    x=df["x"],
    y=df["y"],
    mode="markers",
)
plotly_fig.add_selection(x0=0.2, x1=0.5, y0=0.3, y1=0.9)
plotly_fig.update_layout(width=400, height=400)
plotly_mo = mo.ui.plotly(plotly_fig)

alt_chart = (
    alt.Chart(df, width=400, height=400)
    .mark_point()
    .encode(
        x="x",
        y="y",
    )
    .add_params(
        alt.selection_interval(
            encodings=["x", "y"],
            value={"x": [0.2, 0.5], "y": [0.3, 0.9]},  # initial selection
        )
    )
)
alt_mo = mo.ui.altair_chart(alt_chart)

mo.hstack([plotly_mo, alt_mo])

### Next cell ###

plotly_mo.ranges, plotly_mo.value, alt_mo.selections, alt_mo.value

Suggested solution

Add support for initial selections in plots.
This would save users from needing to take redundant steps for the values to propagate.

Are you willing to submit a PR?

  • Yes

Alternatives

  • Require users to take extra steps
  • Treat missing selection as initial selection (but no selection is also a valid state)

Additional context

Regarding submitting PRs: I can try to investigate the issue but am not sure if I'll be able to solve it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions