Created
July 24, 2015 21:01
-
-
Save ajoros/adb2ab791ce7ee0910d7 to your computer and use it in GitHub Desktop.
Toggle2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from bokeh.plotting import figure | |
from bokeh.models import Plot, ColumnDataSource | |
from bokeh.properties import Instance | |
from bokeh.server.app import bokeh_app | |
from bokeh.server.utils.plugins import object_page | |
from bokeh.models.widgets import ( | |
HBox, | |
VBoxForm, | |
CheckboxGroup) | |
from os import listdir | |
# build up list of stock data in the daily folder | |
# input_fn = 'cumulative-reported-cases-all.csv' | |
# fn_list = find_csv_filenames('.') | |
# fn_list.remove(input_fn) | |
path_to_dir = 'D:/ajoros/Dropbox/AIC/SEIRD/output/' | |
# path_to_dir = 'E:/Dropbox/AIC/SEIRD/output/' | |
# cache stock data as dict of pandas DataFrames | |
pd_cache = {} | |
def get_filenames(path_to_dir, suffix=".csv"): | |
filenames = listdir(path_to_dir) | |
fns = [filename for filename in filenames if filename.endswith(suffix)] | |
return fns | |
def load_csv_to_dflist(path_to_dir, suffix=".csv"): | |
fns = get_filenames(path_to_dir, suffix='.csv') | |
df_list = [] | |
for file_ in fns: | |
df = pd.read_csv(path_to_dir + file_, | |
usecols=['Date', 'C', 'D', 'Cases', 'Deaths'], | |
parse_dates=['Date']) | |
df_list.append(df) | |
return df_list | |
#def get_filedata(ticker): | |
# mydata = pd.read_csv('guinea.csv',usecols=['Date','C','D','Cases','Deaths'], | |
# parse_dates=['Date']) | |
# xyvalues = OrderedDict(Deaths=mydata['Deaths'],Date=mydata['Date']) | |
# for x in xyvalues.keys(): | |
# xyvalues[x] = xyvalues[x][::-1] | |
# return xyvalues | |
class HackApp(HBox): | |
extra_generated_classes = [["HackApp", "HackApp", "HBox"]] | |
inputs = Instance(VBoxForm) | |
toggle = Instance(CheckboxGroup) | |
source1 = Instance(ColumnDataSource) | |
source2 = Instance(ColumnDataSource) | |
source3 = Instance(ColumnDataSource) | |
source4 = Instance(ColumnDataSource) | |
source5 = Instance(ColumnDataSource) | |
source6 = Instance(ColumnDataSource) | |
source7 = Instance(ColumnDataSource) | |
source8 = Instance(ColumnDataSource) | |
source9 = Instance(ColumnDataSource) | |
source10 = Instance(ColumnDataSource) | |
source11 = Instance(ColumnDataSource) | |
plot = Instance(Plot) | |
@classmethod | |
def create(cls): | |
obj = cls() | |
obj.source1 = ColumnDataSource(data=dict(x1=[], y1=[])) | |
obj.source2 = ColumnDataSource(data=dict(x2=[], y2=[])) | |
obj.source3 = ColumnDataSource(data=dict(x3=[], y3=[])) | |
obj.source4 = ColumnDataSource(data=dict(x4=[], y4=[])) | |
obj.source5 = ColumnDataSource(data=dict(x5=[], y5=[])) | |
obj.source6 = ColumnDataSource(data=dict(x6=[], y6=[])) | |
obj.source7 = ColumnDataSource(data=dict(x7=[], y7=[])) | |
obj.source8 = ColumnDataSource(data=dict(x8=[], y8=[])) | |
obj.source9 = ColumnDataSource(data=dict(x9=[], y9=[])) | |
obj.source10 = ColumnDataSource(data=dict(x10=[], y10=[])) | |
obj.source11 = ColumnDataSource(data=dict(x11=[], y11=[])) | |
fns = get_filenames(path_to_dir, suffix='.csv') | |
obj.toggle = CheckboxGroup( labels=fns, active=[0]) | |
print(obj.toggle) | |
toolset = "crosshair,pan,reset,resize,save,wheel_zoom" | |
plot = figure(title_text_font_size="12pt", | |
plot_height=400, | |
plot_width=800, | |
tools=toolset, | |
x_axis_type="datetime" | |
) | |
plot.line('x1', 'y1', source=obj.source1, | |
line_width=3, | |
line_alpha=0.6, | |
line_color="red" | |
) | |
plot.line('x2', 'y2', source=obj.source2, | |
line_width=3, | |
line_alpha=0.6, | |
line_color="blue" | |
) | |
plot.line('x3', 'y3', source=obj.source3, | |
line_width=3, | |
line_alpha=0.6, | |
line_color="green" | |
) | |
plot.line('x4', 'y4', source=obj.source4, | |
line_width=3, | |
line_alpha=0.6, | |
line_color="red" | |
) | |
plot.line('x5', 'y5', source=obj.source5, | |
line_width=3, | |
line_alpha=0.6, | |
line_color="blue" | |
) | |
plot.line('x6', 'y6', source=obj.source6, | |
line_width=3, | |
line_alpha=0.6, | |
line_color="green" | |
) | |
plot.line('x7', 'y7', source=obj.source7, | |
line_width=3, | |
line_alpha=0.6, | |
line_color="red" | |
) | |
plot.line('x8', 'y8', source=obj.source8, | |
line_width=3, | |
line_alpha=0.6, | |
line_color="blue" | |
) | |
plot.line('x9', 'y9', source=obj.source9, | |
line_width=3, | |
line_alpha=0.6, | |
line_color="green" | |
) | |
plot.line('x10', 'y10', source=obj.source10, | |
line_width=3, | |
line_alpha=0.6, | |
line_color="red" | |
) | |
plot.line('x11', 'y11', source=obj.source11, | |
line_width=3, | |
line_alpha=0.6, | |
line_color="blue" | |
) | |
obj.plot = plot | |
obj.update_data() | |
obj.inputs = VBoxForm( | |
children=[ | |
obj.toggle | |
] | |
) | |
obj.children.append(obj.inputs) | |
obj.children.append(obj.plot) | |
return obj | |
def checkbox_handler(self, active): | |
self.update_data(active) | |
def setup_events(self): | |
super(HackApp, self).setup_events() | |
# WHEN CHECKBOX CHANGES | |
self.toggle.on_click(self.checkbox_handler) | |
def input_change(self, obj, attrname, old, new): | |
self.update_data() | |
def update_data(self, hide=False): | |
df_list = load_csv_to_dflist(path_to_dir, suffix=".csv") | |
x1 = x2 = x3 = x4 = x5 = x6 = x7 = x8 = x9 = x10 = x11 = [] | |
y1 = y2 = y3 = y4 = y5 = y6 = y7 = y8 = y9 = y10 = y11 = [] | |
print(self.toggle.active, "self.toggle") | |
for p in self.toggle.active: | |
if p == 0: | |
x1 = df_list[p].Date | |
y1 = df_list[p].C | |
print(df_list[p].Date.head()) | |
print(df_list[p].C.head()) | |
if p == 1: | |
x2 = df_list[p].Date | |
y2 = df_list[p].C | |
print(df_list[p].Date.head()) | |
print(df_list[p].C.head()) | |
if p == 2: | |
x3 = df_list[p].Date | |
y3 = df_list[p].C | |
print(df_list[p].Date.head()) | |
print(df_list[p].C.head()) | |
if p == 3: | |
x4 = df_list[p].Date | |
y4 = df_list[p].C | |
if p == 4: | |
x5 = df_list[p].Date | |
y5 = df_list[p].C | |
if p == 5: | |
x6 = df_list[p].Date | |
y6 = df_list[p].C | |
if p == 6: | |
x7 = df_list[p].Date | |
y7 = df_list[p].C | |
if p == 7: | |
x8 = df_list[p].Date | |
y8 = df_list[p].C | |
if p == 8: | |
x9 = df_list[p].Date | |
y9 = df_list[p].C | |
if p == 9: | |
x10 = df_list[p].Date | |
y10 = df_list[p].C | |
if p == 10: | |
x11 = df_list[p].Date | |
y11 = df_list[p].C | |
self.source1.data = dict(x1=x1, y1=y1) | |
self.source2.data = dict(x2=x2, y2=y2) | |
self.source3.data = dict(x3=x3, y3=y3) | |
self.source4.data = dict(x4=x4, y4=y4) | |
self.source5.data = dict(x5=x5, y5=y5) | |
self.source6.data = dict(x6=x6, y6=y6) | |
self.source7.data = dict(x7=x7, y7=y7) | |
self.source8.data = dict(x8=x8, y8=y8) | |
self.source9.data = dict(x9=x9, y9=y9) | |
self.source10.data = dict(x10=x10, y10=y10) | |
self.source11.data = dict(x11=x11, y11=y11) | |
@bokeh_app.route("/bokeh/Toggle2/") | |
@object_page("sin") | |
def make_hack(): | |
app = HackApp.create() | |
return app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment