In [1]:
from ipysankeywidget import SankeyWidget
from ipywidgets import Layout
In [2]:
links = [
{'source': 'start', 'target': 'A', 'value': 2},
{'source': 'A', 'target': 'B', 'value': 2},
{'source': 'C', 'target': 'A', 'value': 2},
{'source': 'A', 'target': 'C', 'value': 2},
]
In [3]:
layout = Layout(width="500", height="200")
sankey = SankeyWidget(links=links, layout=layout)
sankey
You can use IPython.display
classes to ensure embedded versions of your diagram will persist in your notebook, even without JavaScript!
In [4]:
from IPython.display import (
Image,
SVG
)
In [5]:
import base64
data = base64.decodebytes(bytes(sankey.png, 'ascii'))
Image(data)
Out[5]:
In [6]:
SVG(sankey.svg)
Out[6]:
In [7]:
!rm test.svg test.png
In [8]:
sankey.save_svg('test.svg')
In [9]:
%%html
<img src="test.svg" />
Because the diagram is actually drawn in the browser, it is not immediately available to save to disk. It can be convenient to display the diagram in the notebook and simultaneously save it, so auto_save_png
and auto_save_svg
methods are available which save the images as soon as they are available:
In [10]:
# This won't work
s = SankeyWidget(links=links, layout=layout)
s.save_png('test.png')
s
In [16]:
%%html
<img src="test.png" />
In [17]:
# This does work
SankeyWidget(links=links, layout=layout).auto_save_png('test.png')
In [19]:
%%html
<img src="test.png" />