Exporting Images

Simple example

Advanced examples

Linking and Layout

Exporting Images

Any SankeyWidget can be exported as either an raster (PNG) or vector (SVG) image.

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)

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 (
In [5]:
import base64
data = base64.decodebytes(bytes(sankey.png, 'ascii'))
In [6]:
start → A 2.00A → B 2.00C → A 2.00A → C 2.00startstartAACCBB

Saving images to files

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:

In [7]:
!rm test.svg test.png
In [8]:
In [9]:
<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)
/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?')
In [16]:
<img src="test.png" />
In [17]:
# This does work
SankeyWidget(links=links, layout=layout).auto_save_png('test.png')
In [19]:
<img src="test.png" />