Plotly Sankey - Click node to copy data to clipboard

Hi Plotly Community!

I’m currently creating Sankey-plots (exported to .html files) of the result of a data-migration, showing the move from source to target in 3 levels.

This will ideally be used to interact with Line-of-Business to explain whats happening to their data once we migrate.

What I would love to be able to do it to click a node and it will copy the label from level 0 to the level you’re clicking. Maybe even in a pandas dataframe format like: df[(df.level_0 == “[label_0]”) & (df.level_1 == "[label_1])] etc. such that I could easily take a look at the specific data that node relates to.

Is this at all possible?

1 Like

Hey hrhej!

Yup, that’s possible. The main components of a Sankey are:
-target
-source
-value

You can also update the label.

So just have a callback from a dropdown that has all the node options and update the above values in the figure in the output like this:

@app.callback(
    [Output('sankey-graph', 'figure')],
    [Input('node-dropdown','value')]
)
def update_sankey(node):
fig = go.Figure(###code here)
return fig

If you want to get fancy, you could have the a click return value (or info that you need) that then updates these above values. The latter you need to use some javascript, here is an example:

Cheers

I did a little digging and you can do it in Python as well. The following link shows some examples:

https://dash.plot.ly/interactive-graphing

Basically what you would like to do is update the clickData attribute, which you only care about the node name, and have that update the figure as follows:

@app.callback(
    Output('sankey-graph', 'figure'),
    [Input('basic-interactions', 'clickData')])
def update_sankey(clickData):
    fig = go.Figure(###update values based on value)
    return fig

Hey Louise,

Thanks a lot for your answers! Maybe I wasn’t very good at explaining what I wanted to do, but I don’t want to update the figure at all when clicking. I simply want to copy the labels of the node(s) to my clipboard so I can paste it in a Jupyter Notebook for instance.

Example: Let’s say the label of the node in level 0 was: “some_node_label” and the label of the top node in level 1 was “some_other_node_label”, then clicking the top node in my graph would copy the label of the previous node label in level 0, as well as the node label of the actual node I’m clicking, such that when I paste I get the following: df[(df[“level_0”] == “some_node_label”) & (df[“level_1”] == “some_other_node_label”)]

Does that make sense?