Open yaoyi92 opened 1 day ago
Hi @yaoyi92 👋 and thanks for the great question! So, you want to parametrise custom graph creation function by clicking a data point on the source chart (instead of selecting a value from the vm.Parameter directly). This is the upcoming feature we call "parameter_interaction" (similar to filter_interaction action) and it's in our development roadmap.
Even though it's currently unavailable to implement this behaviour completely natively through the Vizro configuration (e.g. by defining a "parameter_interaction" action on the source chart), it's possible to achieve the same in another way.
Here's an example where by clicking on the ag_grid "Graph_type" column cell, you parametrise the custom graph argument called "graph_type". The example is hosted on Py Cafe and you can access its source code by clicking on the "✏️ EDITOR" button in the top right corner.
The solution is made by utilising pure dash callback mechanism and the clicked value actually goes over the vm.Parameter control that is hidden with the CSS from the assets/custom.css
(you can comment out this CSS to see how it actually works). Your case is pretty similar (as I understood), just instead of implementing a callback that propagates the clicked cell value from the ag_grid
, you should propagate the clicked data point from the scatter graph
.
Dear @petar-qb , nice to hear it's a feature under development.
Thank you for the solution. Using your strategy I can connect click on aggrid to the figure. However, I am still not sure how to connect to the scatter plot. The difficulty I am facing now is I am not sure how to get the underlying scatter plot id
.
vm.Graph(
id = "scatter_chart",
figure = px.scatter(df_perovskite, x="lattice_constant (AA)", y="bulk_modulus (eV/AA^3)", custom_data = ["material"]),
#actions=[vm.Action(function=select_interaction(), inputs=["scatter_chart.clickData"],outputs=["my_bio_figure.material"])]
),
I got TypeError: got an unexpected keyword argument 'id'
if I try to add an id in the px.scatter
arguments. Do we have an id
for the px.scatter?
figure = px.scatter(df_perovskite, x="lattice_constant (AA)", y="bulk_modulus (eV/AA^3)", custom_data = ["material"], id="underlying_scatter_chart"),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/yiy/miniconda3/envs/molecular_simulation/lib/python3.12/site-packages/vizro/models/types.py", line 379, in wrapped
captured_callable: CapturedCallable = CapturedCallable(func, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/yiy/miniconda3/envs/molecular_simulation/lib/python3.12/site-packages/vizro/models/types.py", line 94, in __init__
self.__bound_arguments = inspect.signature(function).bind_partial(*args, **kwargs).arguments
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/yiy/miniconda3/envs/molecular_simulation/lib/python3.12/inspect.py", line 3266, in bind_partial
return self._bind(args, kwargs, partial=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/yiy/miniconda3/envs/molecular_simulation/lib/python3.12/inspect.py", line 3248, in _bind
raise TypeError(
TypeError: got an unexpected keyword argument 'id'
Another approach I tried is to use the action to change the control vm.Parameter.
actions=[vm.Action(function=select_interaction(), inputs=["scatter_chart.clickData"],outputs=["selector_material_id.value"])]
with
@capture("action")
def select_interaction(clickData):
"""Returns the input value."""
material = clickData['points'][0]['customdata'][0]
print(material)
return material
The dropdown value will change in this case, but it seems the value won't propagate to the final target of targets=["my_bio_figure.material"],
Hi @yaoyi92 👋
Thanks for pointing out to this unintuitive UX with IDs. We will explain it better in the docs.
So, vm.Graph
has only one ID (outer ID) that is used as input or output of the action (there's no underlying figure ID for graphs). However, objects like vm.Table
and vm.AgGrid
have two IDs, the outer
and and underlying
ID. Outer ID represents the ID of the html div that wraps the real table object (that has the underlying ID) and it's mostly used as the action's output (when you return entire new object from the action). And, underlying ID is used mostly as the action's input when you have to fetch clickedCell
or any similar property from the table object.
As the source of your app interaction is vm.Graph, it means you have to use its outer ID (the only one that exists). Also, we have to use the pure dash callback to propagate clicked graph point to vm.Parameter and that's the limitation we want to overcome natively in Vizro.
Here's another example that shows how you can make interaction from vm.Graph to the custom vm.Figure function argument -> https://py.cafe/app/petar-qb/vizro-graph-parameter-interaction
Question
Dear all,
I hope to use click data on the scatter plot to control the input of another figure. I can use the control component to achieve the function of control the figure. I am also able to extract clickData from the action. However, not sure how to pass the information in the custom action to another figure.
This works
This doesn't work
The full code
Thank you very much for your help.
Code/Examples
No response
Which package?
vizro
Code of Conduct