Exporting Images
Any SankeyWidget
can be exported as either an raster (PNG) or vector (SVG) image.
from ipysankeywidget import SankeyWidget
from ipywidgets import Layout
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},
]
layout = Layout(width="500", height="200")
sankey = SankeyWidget(links=links, layout=layout)
sankey
SankeyWidget(layout=Layout(height='200', width='500'), links=[{'source': 'start', 'target': 'A', 'value': 2}, …
You can use IPython.display
classes to ensure embedded versions of your diagram will persist in your notebook, even without JavaScript!
from IPython.display import (
Image,
SVG
)
import base64
data = base64.decodebytes(bytes(sankey.png, 'ascii'))
Image(data)
SVG(sankey.svg)
One way of saving images of the diagrams is to display them as above, then right-click and download.
You can also save the image data to disk using save_png
and save_svg
:
!rm test.svg test.png
sankey.save_svg('test.svg')
%%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:
# This won't work
s = SankeyWidget(links=links, layout=layout)
s.save_png('test.png')
s
/tmp/ipysankeywidget/sankey_widget.py:150: UserWarning: No png image available! Try auto_save_png() instead? warnings.warn('No png image available! Try auto_save_png() instead?')
SankeyWidget(layout=Layout(height='200', width='500'), links=[{'source': 'start', 'target': 'A', 'value': 2}, …
%%html
<img src="test.png" />
# This does work
SankeyWidget(links=links, layout=layout).auto_save_png('test.png')
SankeyWidget(layout=Layout(height='200', width='500'), links=[{'source': 'start', 'target': 'A', 'value': 2}, …
%%html
<img src="test.png" />