-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_input.py
188 lines (132 loc) · 5.32 KB
/
file_input.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
# -*- coding: utf-8 -*-
"""
Created on Wed May 03 11:26:21 2017
@author: Kevin Anderson
https://github.com/bokeh/bokeh/issues/6096
"""
import pandas as pd
from bokeh.models import ColumnDataSource, CustomJS, TableColumn
from bokeh.layouts import row
import io
import base64
import graphs
class ImportData:
def __init__(self):
self.file_source = ColumnDataSource({'file_contents': [], 'file_name': []})
self.df = None
self.layout = None
self.doc = None
self.x_drop = None
self.y_drop = None
self.g_drop = None
self.dt = None
self.submit = None
self.g_label = None
self.x_label = None
self.y_label = None
self.plot_type = None
self.plot_label = None
self.cb = CustomJS(args=dict(file_source=self.file_source), code="""
function read_file(filename) {
var reader = new FileReader();
reader.onload = load_handler;
reader.onerror = error_handler;
// readAsDataURL represents the file's data as a base64 encoded string
reader.readAsDataURL(filename);
}
function load_handler(event) {
var b64string = event.target.result;
file_source.data = {'file_contents' : [b64string], 'file_name':[input.files[0].name]};
file_source.trigger("change");
}
function error_handler(evt) {
if(evt.target.error.name == "NotReadableError") {
alert("Can't read file!");
}
}
var input = document.createElement('input');
input.setAttribute('type', 'file');
input.onchange = function(){
if (window.FileReader) {
read_file(input.files[0]);
} else {
alert('FileReader is not supported in this browser');
}
}
input.click();
""")
self.file_source.on_change('data', self.file_callback)
def select_cols_x(self, attr, old, new):
if new == "null":
self.x_drop.value = None
self.x_drop.label = "None"
else:
self.x_drop.label = new
def select_cols_y(self, attr, old, new):
if new == "null":
self.y_drop.value = None
self.y_drop.label = "None"
else:
self.y_drop.label = new
def select_cols_g(self, attr, old, new):
if new == "null":
self.g_drop.value = None
self.g_drop.label = "None"
else:
self.g_drop.label = new
def load_preview(self):
source = ColumnDataSource(data=dict())
columns = []
for col in self.df.columns:
source.data.update({col: self.df.head(10)[col].tolist()})
columns.append(TableColumn(field=col, title=col))
self.dt.source = source
self.dt.columns = columns
self.x_drop.menu = [("None", None)] + list(zip(self.df.columns.tolist(), self.df.columns.tolist()))
self.y_drop.menu = [("None", None)] + list(zip(self.df.columns.tolist(), self.df.columns.tolist()))
self.g_drop.menu = [("None", None)] + list(zip(self.df.columns.tolist(), self.df.columns.tolist()))
self.doc.add_root(row([self.dt]))
self.doc.add_root(row([self.x_label, self.y_label, self.g_label]))
self.doc.add_root(row([self.x_drop, self.y_drop, self.g_drop]))
self.doc.add_root(row([self.plot_label]))
self.doc.add_root(row([self.plot_type]))
self.doc.add_root(row([self.submit]))
self.x_drop.on_change("value", self.select_cols_x)
self.y_drop.on_change("value", self.select_cols_y)
self.g_drop.on_change("value", self.select_cols_g)
self.submit.on_click(self.submit_callback)
def file_callback(self, attr, old, new):
raw_contents = self.file_source.data['file_contents'][0]
# remove the prefix that JS adds
prefix, b64_contents = raw_contents.split(",", 1)
file_contents = base64.b64decode(b64_contents)
file_io = io.StringIO(file_contents.decode("latin-1"))
self.df = pd.read_csv(file_io)
self.load_preview()
def submit_callback(self):
x = self.x_drop.value
y = self.y_drop.value
group = self.g_drop.value
plot_type = self.plot_type.labels[self.plot_type.active]
if group is None and y is not None:
gp = graphs.GraphPlot(x=self.df[x], y=self.df[y])
elif group is not None and y is not None:
gp = graphs.GraphPlot(x=self.df[x], y=self.df[y], group=self.df[group])
elif group is not None and y is None:
gp = graphs.GraphPlot(x=self.df[x], y=None, group=self.df[group])
elif group is None and y is None:
gp = graphs.GraphPlot(x=self.df[x], y=None)
else:
pass
if plot_type == "Scatter":
app_layout = gp.plot_scatter()
elif plot_type == "Line":
app_layout = gp.plot_line()
elif plot_type == "Bar":
app_layout = gp.plot_bar()
elif plot_type == "Histogram":
app_layout = gp.plot_histogram(7)
else:
app_layout = None
self.doc.clear()
self.doc.add_root(app_layout)