def test_session_show_adds_obj_to_curdoc_if_necessary(m): session = ClientSession() session._document = Document() p = Plot() assert session.document.roots == [] session.show(p) assert session.document.roots == [p]
def test_session_show_adds_obj_to_curdoc_if_necessary(m): session = ClientSession() session._document = Document() p = Plot() assert session.document.roots == [] session.show(p) assert session.document.roots == [p]
class ScatterBokeh(Plot): def __init__(self, plot_title: str, number_of_objectives: int, ws_url: str = 'localhost:5006'): super(ScatterBokeh, self).__init__(plot_title, number_of_objectives) if self.number_of_objectives == 2: self.source = ColumnDataSource(data=dict(x=[], y=[], str=[])) elif self.number_of_objectives == 3: self.source = ColumnDataSource(data=dict(x=[], y=[], z=[], str=[])) else: raise Exception( 'Wrong number of objectives: {0}'.format(number_of_objectives)) self.client = ClientSession(websocket_url='ws://{0}/ws'.format(ws_url)) self.doc = curdoc() self.doc.title = plot_title self.figure_xy = None self.figure_xz = None self.figure_yz = None self.__initialize() def __initialize(self) -> None: """ Set-up tools for plot. """ code = ''' selected = source.selected['1d']['indices'][0] var str = source.front.str[selected] alert(str) ''' callback = CustomJS(args=dict(source=self.source), code=code) self.plot_tools = [ TapTool(callback=callback), WheelZoomTool(), 'save', 'pan', HoverTool(tooltips=[('index', '$index'), ('(x,y)', '($x, $y)')]) ] def plot(self, front: List[S], reference: List[S] = None, output: str = '', show: bool = True) -> None: # This is important to purge front (if any) between calls reset_output() # Set up figure self.figure_xy = Figure(output_backend='webgl', sizing_mode='scale_width', title=self.plot_title, tools=self.plot_tools) self.figure_xy.scatter(x='x', y='y', legend='solution', fill_alpha=0.7, source=self.source) self.figure_xy.xaxis.axis_label = self.xaxis_label self.figure_xy.yaxis.axis_label = self.yaxis_label x_values, y_values, z_values = self.get_objectives(front) if self.number_of_objectives == 2: # Plot reference solution list (if any) if reference: ref_x_values, ref_y_values, _ = self.get_objectives(reference) self.figure_xy.line(x=ref_x_values, y=ref_y_values, legend='reference', color='green') # Push front to server self.source.stream({ 'x': x_values, 'y': y_values, 'str': [s.__str__() for s in front] }) self.doc.add_root(column(self.figure_xy)) else: # Add new figures for each axis self.figure_xz = Figure(title='xz', output_backend='webgl', sizing_mode='scale_width', tools=self.plot_tools) self.figure_xz.scatter(x='x', y='z', legend='solution', fill_alpha=0.7, source=self.source) self.figure_xz.xaxis.axis_label = self.xaxis_label self.figure_xz.yaxis.axis_label = self.zaxis_label self.figure_yz = Figure(title='yz', output_backend='webgl', sizing_mode='scale_width', tools=self.plot_tools) self.figure_yz.scatter(x='y', y='z', legend='solution', fill_alpha=0.7, source=self.source) self.figure_yz.xaxis.axis_label = self.yaxis_label self.figure_yz.yaxis.axis_label = self.zaxis_label # Plot reference solution list (if any) if reference: ref_x_values, ref_y_values, ref_z_values = self.get_objectives( reference) self.figure_xy.line(x=ref_x_values, y=ref_y_values, legend='reference', color='green') self.figure_xz.line(x=ref_x_values, y=ref_z_values, legend='reference', color='green') self.figure_yz.line(x=ref_y_values, y=ref_z_values, legend='reference', color='green') # Push front to server self.source.stream({ 'x': x_values, 'y': y_values, 'z': z_values, 'str': [s.__str__() for s in front] }) self.doc.add_root( row(self.figure_xy, self.figure_xz, self.figure_yz)) self.client.push(self.doc) if output: self.__save(output) if show: self.client.show() def update(self, front: List[S], reference: List[S], new_title: str = '', persistence: bool = False) -> None: # Check if plot has not been initialized first if self.figure_xy is None: self.plot(front, reference) if not persistence: rollover = len(front) else: rollover = None self.figure_xy.title.text = new_title x_values, y_values, z_values = self.get_objectives(front) if self.number_of_objectives == 2: self.source.stream( { 'x': x_values, 'y': y_values, 'str': [s.__str__() for s in front] }, rollover=rollover) else: self.source.stream( { 'x': x_values, 'y': y_values, 'z': z_values, 'str': [s.__str__() for s in front] }, rollover=rollover) def __save(self, file_name: str): # env = Environment(loader=FileSystemLoader(BASE_PATH + '/util/')) # env.filters['json'] = lambda obj: Markup(json.dumps(obj)) html = file_html(models=self.doc, resources=CDN) with open(file_name + '.html', 'w') as of: of.write(html) def disconnect(self): if self.is_connected(): self.client.close() def is_connected(self) -> bool: return self.client.connected