def _get_columns(self, element, data): columns = [] for d in element.dimensions(): col = dimension_sanitizer(d.name) kind = data[col].dtype.kind if kind == 'i': formatter = NumberFormatter() editor = IntEditor() elif kind == 'f': formatter = NumberFormatter(format='0,0.0[00000]') editor = NumberEditor() elif kind == 'M' or (kind == 'O' and len(data[col]) and type(data[col][0]) in datetime_types): dimtype = element.get_dimension_type(col) dformat = Dimension.type_formatters.get( dimtype, '%Y-%m-%d %H:%M:%S') formatter = DateFormatter(format=dformat) editor = DateEditor() else: formatter = StringFormatter() editor = StringEditor() column = TableColumn(field=dimension_sanitizer(d.name), title=d.pprint_label, editor=editor, formatter=formatter) columns.append(column) return columns
def initialize_plot(self, ranges=None, plot=None, plots=None, source=None): """ Initializes a new plot object with the last available frame. """ # Get element key and ranges for frame element = self.hmap.last key = self.keys[-1] self.current_frame = element self.current_key = key style = self.lookup_options(element, 'style')[self.cyclic_index] data, _, style = self.get_data(element, ranges, style) if source is None: source = self._init_datasource(data) self.handles['source'] = source columns = [] dims = element.dimensions() for d in dims: col = dimension_sanitizer(d.name) kind = data[col].dtype.kind if kind == 'i': formatter = NumberFormatter() editor = IntEditor() elif kind == 'f': formatter = NumberFormatter(format='0,0.0[00000]') editor = NumberEditor() elif kind == 'M' or (kind == 'O' and len(data[col]) and type(data[col][0]) in datetime_types): dimtype = element.get_dimension_type(0) dformat = Dimension.type_formatters.get( dimtype, '%Y-%m-%d %H:%M:%S') formatter = DateFormatter(format=dformat) editor = DateEditor() else: formatter = StringFormatter() editor = StringEditor() column = TableColumn(field=d.name, title=d.pprint_label, editor=editor, formatter=formatter) columns.append(column) style['reorderable'] = False table = DataTable(source=source, columns=columns, height=self.height, width=self.width, **style) self.handles['plot'] = table self.handles['glyph_renderer'] = table self._execute_hooks(element) self.drawn = True for cb in self.callbacks: cb.initialize() return table
def make_timeseries_datatable_bokeh_server(df): source = ColumnDataSource(df) columns = [ TableColumn(field="Date", title="Date", formatter=DateFormatter()), TableColumn(field="Target", title="Target Timeseries", formatter=StringFormatter(font_style="bold", text_color='red')), TableColumn(field="Hedge", title="Hedge Timeseries", formatter=StringFormatter(font_style="bold", text_color='blue')), TableColumn(field="Correlation", title="Correlation", formatter=StringFormatter(font_style="bold", text_color='darkgreen')) ] data_table = DataTable(source=source, columns=columns, width=1000) return data_table
def _build_optresult_selector(self, optresults): # 1. build a dict with all params and all user columns data_dict = defaultdict(list) for optres in optresults: for param_name, _ in optres[0].params._getitems(): param_val = optres[0].params._get(param_name) data_dict[param_name].append(param_val) for usercol_label, usercol_fnc in self._usercolumns.items(): data_dict[usercol_label].append(usercol_fnc(optres)) # 2. build a pandas DataFrame df = DataFrame(data_dict) # 3. now sort and limit result if self._sortcolumn is not None: df = df.sort_values(by=[self._sortcolumn], ascending=self._sortasc) if self._num_result_limit is not None: df = df.head(self._num_result_limit) # 4. build column info for Bokeh table tab_columns = [] for colname in data_dict.keys(): formatter = NumberFormatter(format='0.000') if (len(data_dict[colname]) > 0 and isinstance(data_dict[colname][0], int)): formatter = StringFormatter() tab_columns.append( TableColumn(field=colname, title=f'{colname}', sortable=False, formatter=formatter)) cds = ColumnDataSource(df) selector = DataTable( source=cds, columns=tab_columns, height=150, # fixed height for selector width=0, # set width to 0 so there is no min_width sizing_mode='stretch_width', fit_columns=True) return selector, cds
def _build_optresult_selector( self, optresults) -> Tuple[DataTable, ColumnDataSource]: # 1. build a dict with all params and all user columns data_dict = defaultdict(list) for optres in optresults: for param_name, _ in optres[0].params._getitems(): param_val = optres[0].params._get(param_name) data_dict[param_name].append(param_val) for usercol_label, usercol_fnc in self._usercolumns.items(): data_dict[usercol_label].append(usercol_fnc(optres)) # 2. build a pandas DataFrame df = DataFrame(data_dict) # 3. now sort and limit result if self._sortcolumn is not None: df = df.sort_values(by=[self._sortcolumn], ascending=self._sortasc) if self._num_result_limit is not None: df = df.head(self._num_result_limit) # 4. build column info for Bokeh table tab_columns = [] for colname in data_dict.keys(): formatter = NumberFormatter(format='0.000') if len(data_dict[colname]) > 0 and isinstance( data_dict[colname][0], int): formatter = StringFormatter() tab_columns.append( TableColumn(field=colname, title=f'{colname}', sortable=False, formatter=formatter)) # TODO: currently table size is hardcoded cds = ColumnDataSource(df) selector = DataTable(source=cds, columns=tab_columns, width=1600, height=150) return selector, cds
def _get_columns(self): if self.value is None: return [] index = [self.value.index.name or 'index'] col_names = index + list(self.value.columns) columns = [] for col in col_names: if col in self.value.columns: data = self.value[col] else: data = self.value.index kind = data.dtype.kind if kind == 'i': formatter = NumberFormatter() editor = IntEditor() elif kind == 'f': formatter = NumberFormatter(format='0,0.0[00000]') editor = NumberEditor() elif isdatetime(data) or kind == 'M': formatter = DateFormatter(format='%Y-%m-%d %H:%M:%S') editor = DateEditor() else: formatter = StringFormatter() editor = StringEditor() if col in self.editors: editor = self.editors[col] if col in self.formatters: formatter = self.formatters[col] if str(col) != col: self._renamed_cols[str(col)] = col width = self.widths.get(str(col)) column = TableColumn(field=str(col), title=str(col), editor=editor, formatter=formatter, width=width) columns.append(column) return columns
str(int(current['churn_risk'].iloc[0] / 100) + 1) + '%', current['sentiment'].iloc[0], 'Group ' + str(np.random.randint(low=1, high=4)), 'Auto Loan', '$' + str(500 + np.random.randint(low=0, high=499)) + ',' + str(100 + np.random.randint(low=0, high=899)) ] } columns = [ TableColumn(field="name", title="Name", width=120), TableColumn(field="phone_number", title="Phone", width=100), TableColumn(field="tenure", title="Tenure", width=60, formatter=StringFormatter()), TableColumn(field="email", title="Email", width=150) # TableColumn(field="salary", title="Income", formatter=NumberFormatter(format="0.000%")), ] customer_directory_table = DataTable(source=customer_directory_source, columns=columns, row_headers=False, editable=True, width=280, height=300, fit_columns=False) customer_directory_source.on_change( 'selected', lambda attr, old, new: selection_update(new)) customer_directory_source.data = {
def create(self): manufacturers = sorted(mpg["manufacturer"].unique()) models = sorted(mpg["model"].unique()) transmissions = sorted(mpg["trans"].unique()) drives = sorted(mpg["drv"].unique()) classes = sorted(mpg["class"].unique()) manufacturer_select = Select(title="Manufacturer:", value="All", options=["All"] + manufacturers) manufacturer_select.on_change('value', self.on_manufacturer_change) model_select = Select(title="Model:", value="All", options=["All"] + models) model_select.on_change('value', self.on_model_change) transmission_select = Select(title="Transmission:", value="All", options=["All"] + transmissions) transmission_select.on_change('value', self.on_transmission_change) drive_select = Select(title="Drive:", value="All", options=["All"] + drives) drive_select.on_change('value', self.on_drive_change) class_select = Select(title="Class:", value="All", options=["All"] + classes) class_select.on_change('value', self.on_class_change) columns = [ TableColumn(field="manufacturer", title="Manufacturer", editor=SelectEditor(options=manufacturers), formatter=StringFormatter(font_style="bold")), TableColumn(field="model", title="Model", editor=StringEditor(completions=models)), TableColumn(field="displ", title="Displacement", editor=NumberEditor(step=0.1), formatter=NumberFormatter(format="0.0")), TableColumn(field="year", title="Year", editor=IntEditor()), TableColumn(field="cyl", title="Cylinders", editor=IntEditor()), TableColumn(field="trans", title="Transmission", editor=SelectEditor(options=transmissions)), TableColumn(field="drv", title="Drive", editor=SelectEditor(options=drives)), TableColumn(field="class", title="Class", editor=SelectEditor(options=classes)), TableColumn(field="cty", title="City MPG", editor=IntEditor()), TableColumn(field="hwy", title="Highway MPG", editor=IntEditor()), ] data_table = DataTable(source=self.source, columns=columns, editable=True) xdr = DataRange1d() ydr = DataRange1d() plot = Plot(title=None, x_range=xdr, y_range=ydr, plot_width=800, plot_height=300) xaxis = LinearAxis(plot=plot) plot.below.append(xaxis) yaxis = LinearAxis(plot=plot) ygrid = Grid(plot=plot, dimension=1, ticker=yaxis.ticker) plot.left.append(yaxis) cty_glyph = Circle(x="index", y="cty", fill_color="#396285", size=8, fill_alpha=0.5, line_alpha=0.5) hwy_glyph = Circle(x="index", y="hwy", fill_color="#CE603D", size=8, fill_alpha=0.5, line_alpha=0.5) cty = GlyphRenderer(data_source=self.source, glyph=cty_glyph) hwy = GlyphRenderer(data_source=self.source, glyph=hwy_glyph) tooltips = [ ("Manufacturer", "@manufacturer"), ("Model", "@model"), ("Displacement", "@displ"), ("Year", "@year"), ("Cylinders", "@cyl"), ("Transmission", "@trans"), ("Drive", "@drv"), ("Class", "@class"), ] cty_hover_tool = HoverTool(plot=plot, renderers=[cty], tooltips=tooltips + [("City MPG", "@cty")]) hwy_hover_tool = HoverTool(plot=plot, renderers=[hwy], tooltips=tooltips + [("Highway MPG", "@hwy")]) select_tool = BoxSelectTool(plot=plot, renderers=[cty, hwy], dimensions=['width']) plot.tools.extend([cty_hover_tool, hwy_hover_tool, select_tool]) plot.renderers.extend([cty, hwy, ygrid]) controls = VBox(children=[ manufacturer_select, model_select, transmission_select, drive_select, class_select ], width=200) top_panel = HBox(children=[controls, plot]) layout = VBox(children=[top_panel, data_table]) return layout
def create(self): print("running create...") manufacturers = sorted(mpg["manufacturer"].unique()) models = sorted(mpg["model"].unique()) transmissions = sorted(mpg["trans"].unique()) drives = sorted(mpg["drv"].unique()) classes = sorted(mpg["class"].unique()) manufacturer_select = Select(title="Manufacturer:", value="All", options=["All"] + manufacturers) manufacturer_select.on_change('value', self.on_manufacturer_change) model_select = Select(title="Model:", value="All", options=["All"] + models) model_select.on_change('value', self.on_model_change) transmission_select = Select(title="Transmission:", value="All", options=["All"] + transmissions) transmission_select.on_change('value', self.on_transmission_change) drive_select = Select(title="Drive:", value="All", options=["All"] + drives) drive_select.on_change('value', self.on_drive_change) class_select = Select(title="Class:", value="All", options=["All"] + classes) class_select.on_change('value', self.on_class_change) columns = [ TableColumn(field="manufacturer", title="Manufacturer", editor=SelectEditor(options=manufacturers), formatter=StringFormatter(font_style="bold")), TableColumn(field="model", title="Model", editor=StringEditor(completions=models)), TableColumn(field="displ", title="Displacement", editor=NumberEditor(step=0.1), formatter=NumberFormatter(format="0.0")), TableColumn(field="year", title="Year", editor=IntEditor()), TableColumn(field="cyl", title="Cylinders", editor=IntEditor()), TableColumn(field="trans", title="Transmission", editor=SelectEditor(options=transmissions)), TableColumn(field="drv", title="Drive", editor=SelectEditor(options=drives)), TableColumn(field="class", title="Class", editor=SelectEditor(options=classes)), TableColumn(field="cty", title="City MPG", editor=IntEditor()), TableColumn(field="hwy", title="Highway MPG", editor=IntEditor()), ] data_table = DataTable(source=self.source, columns=columns, editable=True, width=1300) plot = Plot(title=None, x_range=DataRange1d(), y_range=DataRange1d(), plot_width=1000, plot_height=300) # Set up x & y axis plot.add_layout(LinearAxis(), 'below') yaxis = LinearAxis() plot.add_layout(yaxis, 'left') plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker)) # Add Glyphs cty_glyph = Circle(x="index", y="cty", fill_color="#396285", size=8, fill_alpha=0.5, line_alpha=0.5) hwy_glyph = Circle(x="index", y="hwy", fill_color="#CE603D", size=8, fill_alpha=0.5, line_alpha=0.5) cty = plot.add_glyph(self.source, cty_glyph) hwy = plot.add_glyph(self.source, hwy_glyph) # Add the tools tooltips = [ ("Manufacturer", "@manufacturer"), ("Model", "@model"), ("Displacement", "@displ"), ("Year", "@year"), ("Cylinders", "@cyl"), ("Transmission", "@trans"), ("Drive", "@drv"), ("Class", "@class"), ] cty_hover_tool = HoverTool(renderers=[cty], tooltips=tooltips + [("City MPG", "@cty")]) hwy_hover_tool = HoverTool(renderers=[hwy], tooltips=tooltips + [("Highway MPG", "@hwy")]) select_tool = BoxSelectTool(renderers=[cty, hwy], dimensions='width') plot.add_tools(cty_hover_tool, hwy_hover_tool, select_tool) controls = WidgetBox(manufacturer_select, model_select, transmission_select, drive_select, class_select) top_panel = Row(controls, plot) layout = Column(top_panel, data_table) return layout
from bokeh.embed import file_html from bokeh.models import ColumnDataSource from bokeh.models.widgets import StringFormatter, TableColumn, GroupingInfo, SumAggregator, DataCube from bokeh.resources import INLINE from bokeh.util.browser import view source = ColumnDataSource(data=dict( d0=['A', 'E', 'E', 'E', 'J', 'L', 'M'], d1=['B', 'D', 'D', 'H', 'K', 'L', 'N'], d2=['C', 'F', 'G', 'H', 'K', 'L', 'O'], px=[10, 20, 30, 40, 50, 60, 70], )) target = ColumnDataSource(data=dict(row_indices=[], labels=[])) formatter = StringFormatter(font_style='bold') columns = [ TableColumn(field='d2', title='Name', width=80, sortable=False, formatter=formatter), TableColumn(field='px', title='Price', width=40, sortable=False), ] grouping = [ GroupingInfo(getter='d0', aggregators=[SumAggregator(field_='px')]), GroupingInfo(getter='d1', aggregators=[SumAggregator(field_='px')]) ]
from bokeh.browserlib import view from bokeh.sampledata.autompg2 import autompg2 as mpg source = ColumnDataSource(mpg) manufacturers = sorted(mpg["manufacturer"].unique()) models = sorted(mpg["model"].unique()) transmissions = sorted(mpg["trans"].unique()) drives = sorted(mpg["drv"].unique()) classes = sorted(mpg["class"].unique()) columns = [ TableColumn(field="manufacturer", title="Manufacturer", editor=SelectEditor(options=manufacturers), formatter=StringFormatter(font_style="bold")), TableColumn(field="model", title="Model", editor=StringEditor(completions=models)), TableColumn(field="displ", title="Displacement", editor=NumberEditor(step=0.1), formatter=NumberFormatter(format="0.0")), TableColumn(field="year", title="Year", editor=IntEditor()), TableColumn(field="cyl", title="Cylinders", editor=IntEditor()), TableColumn(field="trans", title="Transmission", editor=SelectEditor(options=transmissions)), TableColumn(field="drv", title="Drive", editor=SelectEditor(options=drives)),
template_sim = """ <div style="color: <%= (function colorfromint(){ if(sim>0.4){return('green')} else if (sim>0.2){return('orange')} else if (sim>=0) {return('red')} else {return('black')} }()) %>;"> <%= value %> </font> </div> """ template_sim_formatter = HTMLTemplateFormatter(template=template_sim) index_formatter = StringFormatter(font_style='italic', text_align='right', text_color=(142, 142, 161)) formatters = [index_formatter] + [date_formatter] + [text_tooltip]*(len(ord_names)-4) + \ [url_formatter] + [template_sim_formatter] #TableColumn object necessary to build the Bokeh datatable Columns = [ TableColumn(field=Ci, title=Ti, width=Wi, formatter=Fi) for Ci, Ti, Wi, Fi in zip(ord_kept_columnns, ord_names, ord_kept_columnns_len, formatters) ] #custom template for a Div object that will be included in the Bokeh App to display the current number of job offers template_div = (""" <div class='content'> <div class='name'> {site_name} </div>
headshot.text = str('<img src=') + image_file + str(' alt="face" width="150">') machine_learning_table.data = { 'Characteristic': ['Churn Risk', 'Sentiment', 'Persona', 'Upsell', 'Lifetime Value'], 'Prediction': [str(int(current['churn_risk'].iloc[0] / 100) + 1) + '%', current['sentiment'].iloc[0], 'Group ' + str(np.random.randint(low=1, high=4)), 'Auto Loan', '$' + str(500 + np.random.randint(low=0, high=499)) + ',' + str( 100 + np.random.randint(low=0, high=899))] } columns = [ TableColumn(field="name", title="Name", width=120), TableColumn(field="phone_number", title="Phone", width=100), TableColumn(field="tenure", title="Tenure", width=60, formatter=StringFormatter()), TableColumn(field="email", title="Email", width=150) # TableColumn(field="salary", title="Income", formatter=NumberFormatter(format="0.000%")), ] customer_directory_table = DataTable(source=customer_directory_source, columns=columns, row_headers=False, editable=True, width=280, height=300, fit_columns=False) customer_directory_source.on_change('selected', lambda attr, old, new: selection_update(new)) customer_directory_source.data = { 'name': customer_directory_df.name, 'phone_number': customer_directory_df.phone_number, 'tenure': ((customer_directory_df.tenure / 365).astype(int)).astype(str) + 'yr', 'email': customer_directory_df.email }
def generate_optresult_model(self, optresult: Union[bttypes.OptResult, bttypes.OrderedOptResult], columns=None, num_item_limit=None) -> Model: """Generates and returns an interactive model for an OptResult or an OrderedOptResult""" cds = ColumnDataSource() tab_columns = [] col_formatter_num = NumberFormatter(format='0.000') col_formatter_str = StringFormatter() opts = optresult if bttypes.is_optresult(optresult) else [ x.result for x in optresult.optresult ] if bttypes.is_ordered_optresult(optresult): benchmarks = [ x.benchmark for x in Bokeh._get_limited_optresult( optresult.optresult, num_item_limit) ] cds.add(benchmarks, "benchmark") tab_columns.append( TableColumn(field='benchmark', title=optresult.benchmark_label, sortable=False, formatter=col_formatter_num)) for idx, strat in enumerate(opts[0]): # add suffix when dealing with more than 1 strategy strat_suffix = '' if len(opts[0]) > 1: strat_suffix = f' [{idx}]' for name, val in strat.params._getitems(): # get value for the current param for all results pvals = [] formatter = col_formatter_num for opt in Bokeh._get_limited_optresult(opts, num_item_limit): param = opt[idx].params._get(name) if inspect.isclass(param): paramstr = param.__name__ else: paramstr = param pvals.append(paramstr) if len(pvals) > 0 and isinstance(pvals[0], str): formatter = col_formatter_str tab_columns.append( TableColumn(field=f"{idx}_{name}", title=f'{name}{strat_suffix}', sortable=False, formatter=formatter)) cds.add(pvals, f"{idx}_{name}") # add user columns specified by parameter 'columns' if columns is not None: for k, v in columns.items(): ll = [ str(v(x)) for x in Bokeh._get_limited_optresult( optresult, num_item_limit) ] cds.add(ll, k) tab_columns.append( TableColumn(field=k, title=k, sortable=False, formatter=col_formatter_str)) selector = DataTable(source=cds, columns=tab_columns, width=1600, height=150) model = column([selector, self.plot_and_generate_model(opts[0])]) def update(_name, _old, new): if len(new) == 0: return stratidx = new[0] model.children[-1] = self.plot_and_generate_model(opts[stratidx]) cds.selected.on_change('indices', update) return model
# Select the last row in the table #sel = Selection( indices = [len(result)-2, len(result)-1]) #sel = Selection( indices = [len(result)-1]) #source.selected = sel print(" Done (%2d sec)." % (time.time() - startTimestamp)) divUpdate.text = "Updated. (Next: %s)" % ( nextUpdateTime.time().strftime("%H:%M:%S")) columns = [ TableColumn(field='msg', title='History', formatter=StringFormatter(text_align='right', text_color='#000000')), ] dataTable = DataTable(source=source, columns=columns, width=800) # If no row(s) selected and dataTable 'data' changed then this scrolls down to the last lines. data_table_force_change = CustomJS(args=dict(source=dataTable), code=""" source.change.emit() source.source.change.emit() """) #dataTable.source.js_on_change('data', data_table_force_change) divTimeHeader = Div(text='Smoketest time:', width=100, height=15) divTime = Div(text=time.strftime("%H:%M:%S"), width=80, height=20)
from bokeh.models.layouts import Column from bokeh.embed import file_html from bokeh.resources import INLINE from bokeh.util.browser import view from bokeh.sampledata.autompg2 import autompg2 as mpg source = ColumnDataSource(mpg) manufacturers = sorted(mpg["manufacturer"].unique()) models = sorted(mpg["model"].unique()) transmissions = sorted(mpg["trans"].unique()) drives = sorted(mpg["drv"].unique()) classes = sorted(mpg["class"].unique()) columns = [ TableColumn(field="manufacturer", title="Manufacturer", editor=SelectEditor(options=manufacturers), formatter=StringFormatter(font_style="bold")), TableColumn(field="model", title="Model", editor=StringEditor(completions=models)), TableColumn(field="displ", title="Displacement", editor=NumberEditor(step=0.1), formatter=NumberFormatter(format="0.0")), TableColumn(field="year", title="Year", editor=IntEditor()), TableColumn(field="cyl", title="Cylinders", editor=IntEditor()), TableColumn(field="trans", title="Transmission", editor=SelectEditor(options=transmissions)), TableColumn(field="drv", title="Drive", editor=SelectEditor(options=drives)), TableColumn(field="class", title="Class", editor=SelectEditor(options=classes)), TableColumn(field="cty", title="City MPG", editor=IntEditor()), TableColumn(field="hwy", title="Highway MPG", editor=IntEditor()), ] data_table = DataTable(source=source, columns=columns, editable=True, width=1000, index_position=-1, index_header="row index", index_width=60) plot = Plot(title=None, plot_width=1000, plot_height=300)
def initialize_layout(self): self._logger("!!! LAYOUT PREPARATION...") #labels = list(np.unique(self.df_rfm[self.cfc])) self.color_mapper = CategoricalColorMapper(factors=list(self.levels), palette=pal) # Make a slider object: slider self.slider = Slider(start=2, end=6, step=1, value=self.nr_clusters, title='Numarul de clustere', width=200) # Attach the callback to the 'value' property of slider self.slider.on_change('value', self.on_cluster_change) self.DivText1 = Div(text="") self.DivText2 = Div(text="") self.slider_tree = Slider(start=2, end=4, step=1, value=self.nr_tree_lvl, title='Numarul de nivele arbore', width=200) self.slider_tree.on_change('value', self.on_update_tree_lvl) self.slider_table = Slider(start=10, end=10000, step=100, value=self.nr_shown_records, title='Numarul de inregistrari afisate', width=200) self.slider_table.on_change('value', self.on_update_table) if self.nr_fields > 2: opt = ["F1/F2", "F1/F3", "F2/F3"] else: opt = ["F1/F2"] self.cb_select_rfm = Select(title="Option:", value=self.default_cluster_view, options=opt) self.cb_select_rfm.on_change('value', self.on_sel_img_change) self.cluster_figure = figure(webgl=True, tools='box_select', plot_width=500, plot_height=500) tip1 = self.tooltip1 tip2 = self.tooltip2 self.hover = HoverTool(tooltips=[ ("Segment", "@" + self.cfc), ("Client", "@" + self.cID), (tip1, "@" + self.hover_fields[tip1]), (tip2, "@" + self.hover_fields[tip2]), ]) if self.FULL_DEBUG: self._logger(" Tooltips: {}".format(self.hover.tooltips)) self.cluster_figure.add_tools(self.hover) self.update_cluster_image(self.current_cluster_view) self.empty_selection = self.current_cluster_glph_renderer.data_source.selected self.btn_reset = Button(label="Deselectare", width=20) self.btn_reset.on_click(self.on_reset_selection) self.btn_save = Button(label="Salvare", width=50) self.btn_save.on_click(self.on_save) self.btn_save_local = Button(label="Salvare doar local", width=50) self.btn_save_local.on_click(self.on_save_local) columns = list() for col in self.df_rfm.columns: if col == self.cfc: tblcol = TableColumn( field=col, title=col, width=200, formatter=StringFormatter(font_style="bold")) else: tblcol = TableColumn( field=col, title=col, width=50, formatter=NumberFormatter(format="0[.]00")) columns.append(tblcol) self.data_table = DataTable(source=self.cds_select, columns=columns, width=1200, height=500, fit_columns=False) self.cb_select_k = Select(title="Vizualizare segment:", value=self.current_segment, options=self.segments) self.cb_select_k.on_change("value", self.on_view_cluster) self.cb_scaling = Select(title="Metoda de scalare/normalizare:", value="MinMax", options=['MinMax', 'ZScore']) self.cb_scaling.on_change("value", self.on_cb_scaling) self.TableViewText = Div(text="N/A") self.ckbox_transf = CheckboxGroup( labels=["F1 LogTransform", "F2 LogTransform", "F3 LogTransform"], active=self.scale_cf) self.ckbox_transf.on_click(self.on_log_check) self.text_ClusterName = TextInput( value=self.cluster_config['Name'] + " ({} segments)".format(self.nr_clusters), title="Nume model:", width=400) self.cb_ClusterGrade = Select(title="Calitatea output:", value='PRODUCTION', width=300, options=['TESTS', 'PRODUCTION']) self.text_ClusterDesc = TextInput(value=self.ClusterDescription, title="Descriere:", width=700) self.text_Author = TextInput(value='Andrei Ionut Damian', title="Autor:") self.text_Algorithm = TextInput(value=self.sAlgorithm, title="Algoritm:", width=700) self.text_F1 = TextInput(value=self.cluster_config['Fields'][0], title="Descriere camp F1:") self.text_F2 = TextInput(value=self.cluster_config['Fields'][1], title="Descriere camp F2:") self.text_F3 = TextInput(value=self.cluster_config['Fields'][2], title="Descriere camp F3:") ## ## done with the controls ## ## ## now to layout preparation ## self.row_btns = row(self.btn_reset, self.btn_save) self.mytabs = list() # tab1: clusters self.tab1_col1_controls = column(widgetbox(self.slider), widgetbox(self.cb_select_rfm), widgetbox(self.slider_tree), widgetbox(self.cb_scaling), widgetbox(self.ckbox_transf), self.DivText1, self.DivText2, self.btn_reset, self.text_ClusterName, widgetbox(self.cb_ClusterGrade), self.text_Author) self.tab1_col2_controls = column(self.cluster_figure, self.text_ClusterDesc, self.text_Algorithm, self.text_F1, self.text_F2, self.text_F3, self.btn_save, self.btn_save_local) self.tab1_layout = row(self.tab1_col1_controls, self.tab1_col2_controls) self.tab1 = widgets.Panel(child=self.tab1_layout, title='Segmentare') self.mytabs.append(self.tab1) #tab 2 table view self.tab2_controls = row(widgetbox(self.slider_table), widgetbox(self.cb_select_k), self.TableViewText) self.tab2_layout = column(self.tab2_controls, self.data_table) self.tab2 = widgets.Panel(child=self.tab2_layout, title='Vizualizare date') self.mytabs.append(self.tab2) # tab 3 tree view #self.tab3_empty_tree_fig = figure(x_range=(0,40), y_range=(0,10)) self.DivTreeImage = Div(text="") dt3 = "Arborele de decizie al segmentarii clientilor." #dt3+= " 'Value' reprezinta numarul de elemente din clasele {}".format(self.class_names) self.DivText3 = Div(text=dt3, width=1000, height=50) self.tab3_layout = column(self.DivText3, self.DivTreeImage) self.tab3 = widgets.Panel(child=self.tab3_layout, title='Vizualizare arbore') self.mytabs.append(self.tab3) # finalizare layout self.update_png() self.update_texts(self.last_tree_accuracy, self.last_clustering_error) self.final_layout = widgets.Tabs(tabs=self.mytabs) self._logger("!!! DONE LAYOUT PREPARATION.\n") if self.FastSave: self.on_save() self._logger("SHUTTING DOWN ...") os.kill(os.getpid(), 9) return
width=400) eta_life.callback = CustomJS(args=dict(source=fake_callback_source2), code=""" source.data = { value: [cb_obj.value] } """) regenerate = Button(label='Regenerate the Sample of Detected Candidates', width=400, button_type='success') regenerate.on_click(recalc_data) # create the table of planet bins eta_table_source = ColumnDataSource(et.eta_table()) eta_columns = [ TableColumn(field="ptype", title="Planet Type", formatter=StringFormatter(text_color='#000000')), TableColumn(field="radii", title="R/R_Earth", formatter=StringFormatter(text_color='#000000')), TableColumn(field="a", title="a", formatter=StringFormatter(text_color='#000000')), TableColumn(field="eta", title="Eta", formatter=StringFormatter(text_color='#000000')) ] eta_table = DataTable(source=eta_table_source, columns=eta_columns, width=450, height=980)
def make_correlation_datatable(correl_df): """ the input datframe must have columns ['Target (as string)', 'Hedge (as string)', 'Correlation ( as float )'] :param correl_df: :return: """ correl_df.reset_index(inplace=True) source = ColumnDataSource(correl_df) target_ts_asset = sorted(correl_df["Target"].unique()) hedge_ts_asset = sorted(correl_df["Hedge"].unique()) columns = [ TableColumn(field="Target", title="Target Timeseries", formatter=StringFormatter(font_style="bold", text_color='red')), TableColumn(field="Hedge", title="Hedge Timeseries", formatter=StringFormatter(font_style="bold", text_color='blue')), TableColumn(field="Correlation", title="Correlation", formatter=StringFormatter(font_style="bold", text_color='darkgreen')) ] data_table = DataTable(source=source, columns=columns, editable=False, width=1000) plot = Plot(title=Title( text="Correlations, Target vs. Hedge Timeseries)", align="center"), x_range=DataRange1d(), y_range=DataRange1d(), plot_width=1000, plot_height=300) # Set up x & y axis plot.add_layout(LinearAxis(), 'below') yaxis = LinearAxis() plot.add_layout(yaxis, 'left') plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker)) # Add Glyphs correlation_glyph = Circle(x="index", y="Correlation", fill_color="#396285", size=8, fill_alpha=0.5, line_alpha=0.5) target_glyph = Circle(x="index", y="Target", fill_color="#396285", size=8, fill_alpha=0.5, line_alpha=0.5) hedge_glyph = Circle(x="index", y="Hedge", fill_color="#396285", size=8, fill_alpha=0.5, line_alpha=0.5) correlation = plot.add_glyph(source, correlation_glyph) target = plot.add_glyph(source, target_glyph) hedge = plot.add_glyph(source, hedge_glyph) # Add the tools tooltips = [("Correlation", "@Correlation"), ("Target", "@Target"), ("Hedge", "@Hedge")] correlation_hover_tool = HoverTool(renderers=[correlation], tooltips=tooltips) target_hover_tool = HoverTool(renderers=[target], tooltips=tooltips) hedge_hover_tool = HoverTool(renderers=[hedge], tooltips=tooltips) select_tool = BoxSelectTool(renderers=[target, hedge, correlation], dimensions='width') plot.add_tools(target_hover_tool, hedge_hover_tool, correlation_hover_tool, select_tool) layout = Column(plot, data_table) the_doc = Document() the_doc.add_root(layout) return the_doc
def make_example_datatable(): source = ColumnDataSource(mpg) print(source.column_names) manufacturers = sorted(mpg["manufacturer"].unique()) models = sorted(mpg["model"].unique()) transmissions = sorted(mpg["trans"].unique()) drives = sorted(mpg["drv"].unique()) classes = sorted(mpg["class"].unique()) columns = [ TableColumn(field="manufacturer", title="Manufacturer", editor=SelectEditor(options=manufacturers), formatter=StringFormatter(font_style="bold")), TableColumn(field="model", title="Model", editor=StringEditor(completions=models)), TableColumn(field="displ", title="Displacement", editor=NumberEditor(step=0.1), formatter=NumberFormatter(format="0.0")), TableColumn(field="year", title="Year", editor=IntEditor()), TableColumn(field="cyl", title="Cylinders", editor=IntEditor()), TableColumn(field="trans", title="Transmission", editor=SelectEditor(options=transmissions)), TableColumn(field="drv", title="Drive", editor=SelectEditor(options=drives)), TableColumn(field="class", title="Class", editor=SelectEditor(options=classes)), TableColumn(field="cty", title="City MPG", editor=IntEditor()), TableColumn(field="hwy", title="Highway MPG", editor=IntEditor()), ] data_table = DataTable(source=source, columns=columns, editable=True, width=1000) plot = Plot(title=None, x_range=DataRange1d(), y_range=DataRange1d(), plot_width=1000, plot_height=300) # Set up x & y axis plot.add_layout(LinearAxis(), 'below') yaxis = LinearAxis() plot.add_layout(yaxis, 'left') plot.add_layout(Grid(dimension=1, ticker=yaxis.ticker)) # Add Glyphs cty_glyph = Circle(x="index", y="cty", fill_color="#396285", size=8, fill_alpha=0.5, line_alpha=0.5) hwy_glyph = Circle(x="index", y="hwy", fill_color="#CE603D", size=8, fill_alpha=0.5, line_alpha=0.5) cty = plot.add_glyph(source, cty_glyph) hwy = plot.add_glyph(source, hwy_glyph) # Add the tools tooltips = [ ("Manufacturer", "@manufacturer"), ("Model", "@model"), ("Displacement", "@displ"), ("Year", "@year"), ("Cylinders", "@cyl"), ("Transmission", "@trans"), ("Drive", "@drv"), ("Class", "@class"), ] cty_hover_tool = HoverTool(renderers=[cty], tooltips=tooltips + [("City MPG", "@cty")]) hwy_hover_tool = HoverTool(renderers=[hwy], tooltips=tooltips + [("Highway MPG", "@hwy")]) select_tool = BoxSelectTool(renderers=[cty, hwy], dimensions='width') plot.add_tools(cty_hover_tool, hwy_hover_tool, select_tool) layout = Column(plot, data_table) doc = Document() doc.add_root(layout) return doc
source.data = { 'msg' : result } # Select the last row in the table #sel = Selection( indices = [len(result)-2, len(result)-1]) #sel = Selection( indices = [len(result)-1]) #source.selected = sel print(" Done (%2d sec)." % (time.time()-startTimestamp)) divUpdate.text = "Updated. (Next: %s)" % (nextUpdateTime.time().strftime("%H:%M:%S")) columns = [ TableColumn(field='msg', title= 'History', formatter=StringFormatter(text_align='right', text_color='#000000')), ] dataTable = DataTable(source=source, columns=columns, width=800) # If no row(s) selected and dataTable 'data' changed then this scrolls down to the last lines. data_table_force_change = CustomJS(args=dict(source=dataTable), code=""" source.change.emit() source.source.change.emit() """) #dataTable.source.js_on_change('data', data_table_force_change) divTimeHeader = Div(text = 'Smoketest time:', width=100, height = 15) divTime = Div(text=time.strftime("%H:%M:%S"), width=80, height=20) divUpdateHeader = Div(text='Refresh status', width=100, height=15)
def show_kvm_exit_types(df, task_re, label): df = df[df['event'] == 'kvm_exit'] df = df[df['task_name'].str.match(task_re)] # the next_comm column contains the exit code exit_codes = Series(KVM_EXIT_REASONS) # add new column congaining the exit reason in clear text df['exit_reason'] = df['next_comm'].map(exit_codes) time_span_msec = get_time_span_msec(df) df.drop( ['cpu', 'duration', 'event', 'next_pid', 'pid', 'next_comm', 'usecs'], inplace=True, axis=1) # Get the list of exit reasons, sorted alphabetically reasons = pandas.unique(df.exit_reason.ravel()).tolist() reasons.sort() # group by task name then exit reasons gb = df.groupby(['task_name', 'exit_reason']) # number of exit types size_series = gb.size() df = size_series.to_frame('count') df.reset_index(inplace=True) p = Bar(df, label='task_name', values='count', stack='exit_reason', title="KVM Exit types per task (%s, %d msec window)" % (label, time_span_msec), legend='top_right', tools="resize,hover,save", width=1000, height=800) p._xaxis.axis_label = "Task Name" p._xaxis.axis_label_text_font_size = "12pt" p._yaxis.axis_label = "Exit Count (sum)" p._yaxis.axis_label_text_font_size = "12pt" # Cannot find a way to display the exit reason in the tooltip # from bokeh.models.renderers import GlyphRenderer # glr = p.select(dict(type=GlyphRenderer)) # bar_source = glr[0].data_source # print bar_source.data # bar_source = glr[1].data_source # bar_source.data['exit_reason'] = ['HOHO'] hover = p.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("task", "$x"), # {"reason", "@exit_reason"}, ("count", "@height") ]) # specify how to output the plot(s) # table with counts gb = df.groupby(['exit_reason']) keys = gb.groups.keys() dfr_list = [] for reason in keys: dfr = gb.get_group(reason) # drop the exit reason column dfr = dfr.drop(['exit_reason'], axis=1) # rename the count column with the reason name dfr.rename(columns={'count': reason}, inplace=True) # set the task name as the index dfr.set_index('task_name', inplace=True) dfr_list.append(dfr) # concatenate all task columns into 1 dataframe that has the exit reason as the index # counts for missing exit reasons will be set to NaN dft = pandas.concat(dfr_list, axis=1) dft.fillna(0, inplace=True) # Add a total column dft['TOTAL'] = dft.sum(axis=1) sfmt = StringFormatter(text_align='center', font_style='bold') nfmt = NumberFormatter(format='0,0') col_names = list(dft.columns.values) col_names.sort() # move 'TOTAL' at end of list col_names.remove('TOTAL') col_names.append('TOTAL') # convert index to column name dft.reset_index(level=0, inplace=True) dft.rename(columns={'index': 'Task'}, inplace=True) columns = [ TableColumn(field=name, title=name, formatter=nfmt) for name in col_names ] columns.insert(0, TableColumn(field='Task', title='Task', formatter=sfmt)) table = DataTable(source=ColumnDataSource(dft), columns=columns, width=1000, row_headers=False, height='auto') output_html(vplot(p, table), 'kvm-types', task_re) '''