-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbokeh-cli.py
226 lines (166 loc) · 6.15 KB
/
bokeh-cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import sys
from StringIO import StringIO
from collections import OrderedDict
import numpy as np
import pandas as pd
from bokeh.plotting import *
from bokeh.palettes import brewer
from bokeh.objects import HoverTool
import pdb
import click
palettes = u', '.join(sorted(brewer.keys()))
help = u"""Colors palette to use. The following palettes are available:
%s
You can also provide your own custom palette by specifying a list colors. I.e.:
"#a50026,#d73027,#f46d43,#fdae61,#fee08b,#ffffbf,#d9ef8b,#a6d96a,#66bd63"
""" % palettes
@click.command()
@click.option('--source_filename',
default='sample_data/stocks_data.csv',
help='path to the series data file (i.e.: /source/to/my/data.csv')
@click.option('--output', default='file://cli_output.html',
#prompt='Your name',
help='''Selects the plotting output, which could either be sent to an html '''
'''file or a bokeh server instance. Syntax convention for this option '''
'''is as follows: <output_type>://<type_options>
where:
- output_type: 'file' or 'server'
- 'file' type options: path_to_output_file
- 'server' type options syntax: docname[@url][@name]
Defaults to: --output file://cli_output.html
Examples:
--output file://cli_output.html
--output file:///home/someuser/bokeh_rocks/cli_output.html
--output server://clidemo
'''
)
@click.option('--title', default='Bokeh CLI')
@click.option('--plot_type', default='circle, line')
@click.option(
'--x_axis_name',
default='',
help="Name of the data serie to be used as x axis when plotting. By "
"default the first serie found on the input file is taken."
)
@click.option(
'--tools',
default='pan,wheel_zoom,box_zoom,reset,previewsave,hover',
)
@click.option(
'--series',
default='',
help="Name of the series from the source to include in the plot. "
"If not specified all source series will be included."
)
@click.option('--palette', default="RdYlGn", help=help)
@click.option(
'--buffer',
default='f',
help="""Reads data source as String from input buffer. Usage example:
cat stocks_data.csv | python cli.py --buffer t
"""
)
@click.option('--x_axis_type', default='auto')
def cli(source_filename, output, title, plot_type, tools, series, palette,
x_axis_name, buffer, x_axis_type='auto'):
"""
Bokeh Command Line Tool is a minimal client to access high level plotting
functionality provided by Bokeh plotting library.
Examples:
>> python bokeh-cli.py --title "My Nice Plot" --series "High,Low,Close"
--plot_type "circle,line" --palette Reds --source_filename sample_data/stocks_data.csv
>> cat sample_data/stocks_data.csv | python bokeh-cli.py --buffer t
>> python bokeh-cli.py --help
"""
if buffer != 'f':
data = sys.stdin.read()
source = pd.read_csv(StringIO(data))
else:
# setup data source
source = pd.read_csv(source_filename)
# define user selected plot functions
plot_foos = parse_plot_foos(plot_type)
# bokeh boilerplate code
colors = brewer[palette][len(source.columns)]
output_type, output_options = parse_output(output)
if output_type == 'file':
output_file(output_options, title = title)
elif output_type == 'server':
output_options = output_options.split("@")
attrnames = ['docname', 'url', 'name']
# unpack server output parametrs in order to pass them to the plot
# creation function
kws = dict(
(attrname, value) for attrname, value in zip(
attrnames, output_options)
)
output_server(**kws)
else:
print "Unknown output type %s found. Valid values are: file|server" % (
output_type
)
return
hold()
if not x_axis_name:
# if not source column was picked as x axis we take a default "range"
#x_axis_name = source.columns[0]
x_axis_name = 'ranged_x_axis'
# add the new x range data to the source dataframe
source[x_axis_name] = range(len(source[source.columns[0]]))
if x_axis_type == 'datetime':
# in case the x axis type is datetime that column must be converted to
# datetime
source[x_axis_name] = pd.to_datetime(source[x_axis_name])
series = filter_series(series, x_axis_name, source.columns)
# define plot options that are to be specified only on the first plot
extra = dict(tools=tools, x_axis_type=x_axis_type, title=title)
# generate all the plots
for i, colname in enumerate(series):
if colname in series:
serie = source[colname]
for plot_foo in plot_foos:
plot_foo(
source[x_axis_name],
serie,
color=colors[i],
legend=colname,
**extra
)
extra = {}
# TODO: Hover tools should only be added and configured if hover is
# included in the list of the tools enabled
# add the hover toos
hover = curplot().select(dict(type=HoverTool))
hover.tooltips = OrderedDict([
('index', "$index"),
('(x,y)', "($x,$y)"),
])
show()
def parse_plot_foos(plot_type):
"""
Receives the plot type(s) specified by the user and build a list of
the their related functions.
example:
>> parse_plot_foos('line,circle')
[line, circle]
"""
if ',' in plot_type:
plot_foos = [globals()[str(pt.strip())] for pt in plot_type.split(',')]
else:
plot_foos = [globals()[str(plot_type)]]
return plot_foos
def filter_series(series, x_axis_name, source_columns):
"""
If series is empty returns source_columns excluding the column
where column == x_axis_name.
Otherwise returns the series.split(',')
"""
if not series:
return [c for c in source_columns if c != x_axis_name]
else:
return series.split(',')
def parse_output(output):
output_type, output_options = output.split('://')
return output_type, output_options
if __name__ == '__main__':
cli()