#!/usr/bin/env python # coding: utf-8 # # Exporting Images # # > [ Simple example](./Simple%20example.ipynb) # > # > [ Advanced examples](./More%20examples.ipynb) # > # > [ Linking and Layout](./Linking%20and%20Layout.ipynb) # > # > 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) 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) # In[6]: SVG(sankey.svg) # ## 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]: get_ipython().system('rm test.svg test.png') # In[8]: sankey.save_svg('test.svg') # In[9]: get_ipython().run_cell_magic('html', '', '\n') # 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]: get_ipython().run_cell_magic('html', '', '\n') # In[17]: # This does work SankeyWidget(links=links, layout=layout).auto_save_png('test.png') # In[19]: get_ipython().run_cell_magic('html', '', '\n')