Martin Kavalar / Feb 06 2019

Altair

We start by setting up a coder so when a cell returns an altair.Chart it's automatically displayed as an iframe.

def __is_altair_chart(value): 
  try:
    import altair 
    return isinstance(value, altair.Chart) 
  except: 
    return False 
  
def __write_altair(chart, path): 
  chart.save(path, format='html') 
  return {'content-type': 'text/html', 'kind': 'iframe'} 

register_coder('altair', lambda value: __is_altair_chart(value), lambda value, path: __write_altair(value, path))

Now let's try to use it:

import altair as alt
from vega_datasets import data

iris = data.iris()

alt.Chart(iris).mark_point().encode(
    x='petalLength',
    y='petalWidth',
    color='species'
)