Skip to content

update_*() batch methods #1484

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

Closed
nicolaskruchten opened this issue Mar 28, 2019 · 5 comments
Closed

update_*() batch methods #1484

nicolaskruchten opened this issue Mar 28, 2019 · 5 comments

Comments

@nicolaskruchten
Copy link
Contributor

Given that px produces figures which often have non-trivial numbers of fairly 'regular' axes/annotations/traces, it would be very convenient to have something like Figure.update_traces( <patch>, <selector> )-type methods e.g.

px.scatter(..., marginal_y="violin").update_traces(
    patch=dict(marker_line_color="black"), 
    selector=dict(type="scatter")
)

In this example, px.scatter() generates some scatter traces but also some violin ones, so the patch will only apply to the scatter traces. Also note that this example uses some magic-underscores from #1481

This would also be very convenient for changing axis-level attributes in the case of a facetted px plot with many axes, ditto the annotation-based facet titles etc. In fact, I could see this being useful pretty much anywhere we have a collection of things either in a list (annotations, traces, shapes, etc) or with a number suffix (xaxis, yaxis, geo, scene, etc).

A more uniform API where all the method are called batch_update like Figure.data.batch_update() or Figure.layout.xaxis.batch_update() might work as well? The latter case maybe less nice, but there is no "collection" of x-axes on which to attach the method there :)

@jonmmease
Copy link
Contributor

I like it. Here are some places that come to mind where I can see this being immediately useful.

fig.update_traces()
fig.layout.update_xaxes()
fig.layout.update_yaxes()
fig.layout.update_geos()
fig.layout.update_polars()
fig.layout.update_scenes()
fig.layout.update_annotations()
fig.layout.update_shapes()

parcoords.update_dimensions()
parcats.update_dimensions()

Another option, would be to support JSONPath-style keys in the existing .update method (See https://github.com/json-path/JsonPath#path-examples).

# Update marker line color of all scatter traces
fig.update({'data[?(@.type == 'scatter')].marker.line.color': 'green'})

# Update colorbar title font family across all colorbars, and all nesting levels
fig.update({'..colorbar.title.font.family': 'Rockwell'})

This would offer pretty complete flexibility, at the cost of perhaps a slightly higher learning curve. I'm also not sure how much we could reuse from existing libraries like https://github.com/kennknowles/python-jsonpath-rw.

@nicolaskruchten
Copy link
Contributor Author

I think the JSONPath idea is a good one, but likely not a total replacement for either what I'm proposing here or magic-underscores...

One other thing I want to throw into the mix is a more swiss-army-knifey for_each_trace(callback, selector) variant (which could be used internally in combination with the normal .update(patch) methods to build update_traces(patch, selector)) that would allow for people to write more complicated logic like uppercasing trace names or whatever.

@jonmmease
Copy link
Contributor

Merged in #1534. It's not everything discussed in this issue, but it's the things that motivated the initial use case so I'm going to close this. We can open a new issue if there are chunks here that warrant further discussion.

@nicolaskruchten
Copy link
Contributor Author

For real px customizability we will need easy ways to batch-update xaxis and yaxis... If you facet and want to format your ticks you'll need to update multiple axes, unless we add another match-style attr in the JS layer that matches all formatting attributes across axes as well as range (which is not a bad idea actually), and then people could just update xaxis and yaxis. hmmm :)

@jonmmease
Copy link
Contributor

For real px customizability we will need easy ways to batch-update xaxis and yaxis

You're right. Done in #1548

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

2 participants