In [1]:
import pandas as pd

from bokeh.plotting import output_notebook, show, figure
from bokeh.resources import CDN
from bokeh.models import ColumnDataSource

import yaml
from yaml import SafeLoader, Loader, BaseLoader

%reload_ext yamlmagic

output_notebook(resources=CDN)


BokehJS successfully loaded.

Warning: Requesting CDN BokehJS version '0.9.3dev' from Bokeh development version '0.9.3dev-d2ef2fa'. This configuration is unsupported and may not work!


In [2]:
class PlotLoader(SafeLoader):
    pass

Add constructors to the loader to load remote data and create a Bokeh figure


In [3]:
def cds_constructor(loader, node):
    """
    Use pandas IO tools to easily load local and remote data files
    """
    bits = loader.construct_mapping(node, deep=True)
    # Pandas io read method as the key
    read_method = [key for key in bits.keys()][0]

    # Read value can be a file or url
    read_value = [value for value in bits.values()][0]
    
    #return a dataframe
    return getattr( pd, read_method)(read_value)

def figure_constructor(loader, node):
    """
    A YAML constructor for the bokeh.plotting module
    http://bokeh.pydata.org/en/latest/docs/reference/plotting.html
    """
    figure_data = loader.construct_mapping(node, deep=True)
    
    # Create the figure, using the ``figure`` key
    p = figure(
        **figure_data['figure']
    )
    
    # Add glyphs to the figure using the ``glyphs`` key
    glyphs = figure_data['glyphs']

    for glyph in glyphs:
        tmp = list(glyph.values())[0]
        if 'source' in tmp:
            # Convert source to column data source
            tmp['source'] = ColumnDataSource(
                tmp['source']
            )
        getattr( p, list(glyph.keys())[0] )(**tmp)
        
    return p

PlotLoader.add_constructor("!io", cds_constructor)
PlotLoader.add_constructor("!figure", figure_constructor)

Test with Fishers Iris data


In [4]:
%%yaml plot -l PlotLoader
# &fisher is an anchor/alias provided by yaml
fisher-iris-data: &fisher !io
    read_csv: http://www.math.uah.edu/stat/data/Fisher.csv
bokeh: !figure
    figure: 
        width: 800
        height: 400
        x_axis_label: Petal Length
        y_axis_label: Sepal Width
    glyphs:
    - circle: 
        x: PL
        y: SW
        fill_color: red
        size: 10
        line_color: 
        # use alias to reference the column data source
        source: *fisher


Show the Bokeh Figure


In [5]:
show(plot['bokeh'])


Data is available as a dataframe


In [6]:
plot['fisher-iris-data']


Out[6]:
Type PW PL SW SL
0 0 2 14 33 50
1 1 24 56 31 67
2 1 23 51 31 69
3 0 2 10 36 46
4 1 20 52 30 65
5 1 19 51 27 58
6 2 13 45 28 57
7 2 16 47 33 63
8 1 17 45 25 49
9 2 14 47 32 70
10 0 2 16 31 48
11 1 19 50 25 63
12 0 1 14 36 49
13 0 2 13 32 44
14 2 12 40 26 58
15 1 18 49 27 63
16 2 10 33 23 50
17 0 2 16 38 51
18 0 2 16 30 50
19 1 21 56 28 64
20 0 4 19 38 51
21 0 2 14 30 49
22 2 10 41 27 58
23 2 15 45 29 60
24 0 2 14 36 50
25 1 19 51 27 58
26 0 4 15 34 54
27 1 18 55 31 64
28 2 10 33 24 49
29 0 2 14 42 55
... ... ... ... ... ...
120 1 18 48 28 62
121 2 15 45 30 56
122 0 2 14 32 46
123 0 4 15 44 57
124 1 24 56 34 63
125 1 16 58 30 72
126 1 21 59 30 71
127 1 18 56 29 63
128 2 12 42 30 57
129 1 23 69 26 77
130 2 13 56 29 66
131 0 2 15 34 52
132 2 10 37 24 55
133 0 2 15 31 46
134 1 19 61 28 74
135 0 3 13 35 50
136 1 18 63 29 73
137 2 15 47 31 67
138 2 13 41 30 56
139 2 13 43 29 64
140 1 22 58 30 65
141 0 3 14 35 51
142 2 14 47 29 61
143 1 19 53 27 64
144 0 2 16 34 48
145 1 20 50 25 57
146 2 13 40 23 55
147 0 2 17 34 54
148 1 24 51 28 58
149 0 2 15 37 53

150 rows × 5 columns


In [ ]: