from ipysankeywidget import SankeyWidget

"""
Explanation: Linking and Layout

<i class="fa fa-2x fa-paper-plane text-info fa-fw"> </i> Simple example
<i class="fa fa-2x fa-space-shuttle text-info fa-fw"> </i> Advanced examples
<i class="fa fa-2x fa-link text-info fa-fw"> </i> Linking and Layout
<i class="fa fa-2x fa-image text-info fa-fw"> </i> Exporting Images
End of explanation
"""


from ipywidgets import (
    VBox,
    HBox,
    IntSlider,
)

links = [
    {'source': 'start', 'target': 'A', 'value': 10},
    {'source': 'A', 'target': 'B', 'value': 10},
    {'source': 'C', 'target': 'A', 'value': 10},
    {'source': 'A', 'target': 'C', 'value': 10},
]

sankey = SankeyWidget(links=links)
sankey

"""
Explanation: <i class="fa fa-info-circle fa-2x fa-fw text-primary"></i> This uses the base ipywidgets for layout and data, but you can use any widgets!
End of explanation
"""


def slider(link, i, sankey):
    value = IntSlider(description="{source} → {target}".format(**link), min=0, max=10, step=1, value=10)

    def _change(change):
        sankey.links[i]["value"] = value.value
        sankey.send_state()
    
    value.observe(_change)
    
    return value

"""
Explanation: <i class="fa fa-gears fa-2x fa-fw text-info"></i> A convenience factory function
End of explanation
"""


sliders = [slider(link, i, sankey) for i, link in enumerate(links)]

box = HBox(children=[sankey, VBox(children=sliders)])
box

"""
Explanation: Build up a slider per link to control the value:
End of explanation
"""


