def _widget(self): """ Create IPython widget for display within a notebook """ try: return self._cached_widget except AttributeError: pass try: from ipywidgets import Layout, VBox, HBox, IntText, Button, HTML, Accordion except ImportError: self._cached_widget = None return None layout = Layout(width="150px") if self.dashboard_link: link = '<p><b>Dashboard: </b><a href="%s" target="_blank">%s</a></p>\n' % ( self.dashboard_link, self.dashboard_link, ) else: link = "" title = "<h2>%s</h2>" % self._cluster_class_name title = HTML(title) dashboard = HTML(link) status = HTML(self._widget_status(), layout=Layout(min_width="150px")) if self._supports_scaling: request = IntText(0, description="Workers", layout=layout) scale = Button(description="Scale", layout=layout) minimum = IntText(0, description="Minimum", layout=layout) maximum = IntText(0, description="Maximum", layout=layout) adapt = Button(description="Adapt", layout=layout) accordion = Accordion( [HBox([request, scale]), HBox([minimum, maximum, adapt])], layout=Layout(min_width="500px"), ) accordion.selected_index = None accordion.set_title(0, "Manual Scaling") accordion.set_title(1, "Adaptive Scaling") def adapt_cb(b): self.adapt(minimum=minimum.value, maximum=maximum.value) update() adapt.on_click(adapt_cb) def scale_cb(b): with log_errors(): n = request.value with suppress(AttributeError): self._adaptive.stop() self.scale(n) update() scale.on_click(scale_cb) else: accordion = HTML("") box = VBox([title, HBox([status, accordion]), dashboard]) self._cached_widget = box def update(): status.value = self._widget_status() cluster_repr_interval = parse_timedelta( dask.config.get("distributed.deploy.cluster-repr-interval", default="ms")) pc = PeriodicCallback(update, cluster_repr_interval * 1000) self.periodic_callbacks["cluster-repr"] = pc pc.start() return box
def aristeia_dice_widget(): a = create_die_widgets() d = create_die_widgets() accordion = Accordion(children=[display_widgets(a), display_widgets(d)]) accordion.set_title(0, 'Attacker') accordion.set_title(1, 'Defender') return accordion
def create_mulligan(team1, team2): check1 = create_checkbox(team1) check2 = create_checkbox(team2) accord = Accordion(children=[check1, check2]) accord.set_title(0, 'Team 1') accord.set_title(1, 'Team 2') return accord, check1, check2
class SideBar(VBox): slice_index = Int() def __init__(self, **kwargs): self._stats_view = StatsView() self._tools = Tools() self._side_bar = Accordion(children=[self._stats_view, self._tools]) self._side_bar.set_title(0, 'Statistics') self._side_bar.set_title(1, 'Tools') super().__init__(children=[self._side_bar], **kwargs) self.setup_connections() def setup_connections(self): self._tools.observe(self._set_slice_index, names='slice_index') def _set_slice_index(self, change): self.slice_index = change['new'] def update(self, change): spec_cube = change['new']['cube'] view = change['new']['view'] self._stats_view.data_points = spec_cube.size self._stats_view.data_shape = spec_cube.shape
def image(image, region=None, visualization=None, name=None, dimensions=(500, 500), do_async=None): """ Preview an Earth Engine Image """ if do_async is None: do_async = CONFIG.get('do_async') start = time() if name: label = '{} (Image)'.format(name) loading = 'Loading {} preview...'.format(name) else: label = 'Image Preview' loading = 'Loading preview...' formatdimension = "x".join([str(d) for d in dimensions]) wid = Accordion([Label(loading)]) wid.set_title(0, loading) def compute(image, region, visualization): if not region: region = tools.geometry.getRegion(image) else: region = tools.geometry.getRegion(region) params = dict(dimensions=formatdimension, region=region) if visualization: params.update(visualization) url = image.getThumbURL(params) req = requests.get(url) content = req.content rtype = req.headers['Content-type'] if rtype in ['image/jpeg', 'image/png']: img64 = base64.b64encode(content).decode('utf-8') src = '<img src="data:image/png;base64,{}"></img>'.format(img64) result = HTML(src) else: result = Label(content.decode('utf-8')) return result def setAccordion(acc): widget = compute(image, region, visualization) end = time() elapsed = end-start acc.children = [widget] elapsed = utils.format_elapsed(elapsed) acc.set_title(0, '{} [{}]'.format(label, elapsed)) if do_async: thread = threading.Thread(target=setAccordion, args=(wid,)) thread.start() else: setAccordion(wid) return wid
def collapsible(fn, *args, **kwargs): from ipywidgets import Output, Accordion out = Output() with out: fn(*args, **kwargs) acc = Accordion(children=[out]) acc.set_title(0, "View output") acc.selected_index = None return acc
def constructAccordion(param, itemList): param.element = {} for item, good in itemList.items(): param.element[item] = renderParam(param, param.setVar(item), item.title(), param.get(item), circle=good.color) accordion = Accordion( children=[HBox(list(param.element.values()))]) accordion.set_title(0, param.title) accordion.add_class('helipad_param_peritem') return accordion
def _widget(self): from ipywidgets import Accordion accordion = Accordion( children=[log._widget() for log in self.values()]) [accordion.set_title(i, title) for i, title in enumerate(self.keys())] return accordion
def get_model_widget(obj, **kwargs): """Creates interactive notebook widgets for all components and parameters, if available. """ children = [] wdict = {} for component in obj: idict = component.gui(display=False, toolkit="ipywidgets")["ipywidgets"] children.append(idict["widget"]) wdict["component_{}".format(component.name)] = idict["wdict"] accordion = Accordion(children=children) for i, comp in enumerate(obj): accordion.set_title(i, comp.name) return {"widget": accordion, "wdict": wdict}
def wrap(button): self.selected_tab().children = (HTML('Loading...'), ) try: tasklist = ee.data.getTaskList() # empty lists running_list = [] completed_list = [] failed_list = [] canceled_list = [] unknown_list = [] all_list = { 'RUNNING': running_list, 'COMPLETED': completed_list, 'FAILED': failed_list, 'CANCELED': canceled_list, 'UNKNOWN': unknown_list } for task in tasklist: state = task['state'] accordion = create_accordion(task) if task['state'] == 'COMPLETED': start = int(task['start_timestamp_ms']) end = int(task['creation_timestamp_ms']) seconds = float((start - end)) / 1000 name = '{} ({} sec)'.format( task['output_url'][0].split('/')[-1], seconds) else: name = '{}'.format(task['description']) # Accordion for CheckRow widget mainacc = Accordion(children=(accordion, )) mainacc.set_title(0, name) mainacc.selected_index = None # CheckRow wid = CheckRow(mainacc) # Append widget to the CORRECT list all_list[state].append(wid) # Assign Children self.runningVBox.children = tuple(running_list) self.completedVBox.children = tuple(completed_list) self.failedVBox.children = tuple(failed_list) self.canceledVBox.children = tuple(canceled_list) self.unknownVBox.children = tuple(unknown_list) except Exception as e: self.selected_tab().children = (HTML(str(e)), )
def get_parameter_widget(obj, **kwargs): """Creates interactive notebook widgets for the parameter, if available. """ if obj._number_of_elements == 1: return _get_value_widget(obj) else: wdict = {} par_widgets = [] for i in range(obj._number_of_elements): thiswd = _get_value_widget(obj=obj, index=i) par_widgets.append(thiswd["widget"]) wdict["element{}".format(i)] = thiswd["wdict"] update = Button( description="Update", tooltip="Unlike most other widgets, the multivalue parameter " "widgets do not update automatically when the value of the " "changes by other means. Use this button to update the values" "manually") def on_update_clicked(b): for value, container in zip(obj.value, par_widgets): minwidget = container.children[0] vwidget = container.children[1] maxwidget = container.children[2] if value < vwidget.min: minwidget.value = value elif value > vwidget.max: maxwidget.value = value vwidget.value = value update.on_click(on_update_clicked) wdict["update_button"] = update container = Accordion([VBox([update] + par_widgets)], descrition=obj.name) container.set_title(0, obj.name) return { "widget": container, "wdict": wdict, }
def create_object_output(object): ''' Create a output Widget for Images, Geometries and Features ''' ty = object.__class__.__name__ if ty == 'Image': info = object.getInfo() image_id = info['id'] if 'id' in info else 'No Image ID' prop = info['properties'] bands = info['bands'] bands_names = [band['id'] for band in bands] bands_types = [band['data_type']['precision'] for band in bands] bands_crs = [band['crs'] for band in bands] new_band_names = [ '<li>{} - {} - {}</li>'.format(name, ty, epsg) for name, ty, epsg in zip(bands_names, bands_types, bands_crs) ] new_properties = [ '<li><b>{}</b>: {}</li>'.format(key, val) for key, val in prop.items() ] header = HTML('<b>Image id:</b> {id} </br>'.format(id=image_id)) bands_wid = HTML('<ul>' + ''.join(new_band_names) + '</ul>') prop_wid = HTML('<ul>' + ''.join(new_properties) + '</ul>') acc = Accordion([bands_wid, prop_wid]) acc.set_title(0, 'Bands') acc.set_title(1, 'Properties') acc.selected_index = None # this will unselect all return VBox([header, acc]) elif ty == 'FeatureCollection': try: info = object.getInfo() except: print('FeatureCollection limited to 4000 features') info = object.limit(4000) return create_accordion(info) else: info = object.getInfo() return create_accordion(info)
def _build_highlights_accordion(self) -> DOMWidget: """ Creates a widget that displays highlights for this `Tier` in an `ipywidgets.Accordion` - which is nice! """ highlights = self.tier.get_highlights() if not highlights: return None widget = Accordion() for i, (name, highlight) in enumerate(highlights.items()): out = Output() widget.children = (*widget.children, out) with out: for item in highlight: publish_display_data(**item) widget.set_title(i, name) widget.selected_index = None return widget
def create_accordion(dictionary): """ Create an Accordion output from a dict object """ widlist = [] ini = 0 widget = Accordion() widget.selected_index = None # this will unselect all for key, val in dictionary.items(): if isinstance(val, dict): newwidget = create_accordion(val) widlist.append(newwidget) elif isinstance(val, list): # tranform list to a dictionary dictval = {k: v for k, v in enumerate(val)} newwidget = create_accordion(dictval) widlist.append(newwidget) else: value = HTML(str(val)) widlist.append(value) widget.set_title(ini, key) ini += 1 widget.children = widlist return widget
def show_items_feature_form(table, periods): tree = defaultdict(dict) for period in periods: section = period.section tree[section.group][section.name] = period.features def update(button, table, feature): clear_output() plot_items_feature(table, feature) group_pages = [] group_titles = [] for group_title, group in tree.iteritems(): section_pages = [] section_titles = [] for section_title, features in group.iteritems(): buttons = [] for feature in features: button = Button(description=feature.name) button.on_click(partial( update, table=table, feature=feature) ) buttons.append(button) section_pages.append(VBox(children=buttons)) section_titles.append(section_title) accordion = Accordion(children=section_pages) for index, title in enumerate(section_titles): accordion.set_title(index, title) group_pages.append(accordion) group_titles.append(group_title) accordion = Accordion(children=group_pages) for index, title in enumerate(group_titles): accordion.set_title(index, title) return accordion
def collapsible(function, *args, **kwargs): """ An IPython widget that can collapse its contents. Parameters ---------- function : callable A function that generates some kind of output (print, plots, etc). Args and kwargs will be forwarded here. Returns ------- ipywidgets.Accordion """ from ipywidgets import Output, Accordion out = Output() with out: function(*args, **kwargs) acc = Accordion(children=[out]) acc.set_title(0, "View output") acc.selected_index = None return acc
def box(self): layout = Accordion(children=[ VBox([ self.ui_th_std0, self.ui_qv0, self.ui_p0, self.ui_kappa, self.ui_w_max ]), VBox([*self.ui_processes]), VBox([ self.ui_nx, self.ui_nz, self.ui_sdpg, self.ui_dt, self.ui_n_steps, self.ui_condensation_rtol_lnv, self.ui_condensation_rtol_thd, *self.ui_mpdata_options ]), VBox([]) ]) layout.set_title(0, 'parameters') layout.set_title(1, 'processes') layout.set_title(2, 'discretisation') layout.set_title(3, 'parallelisation') return layout
def box(self): layout = Accordion(children=[ VBox([ self.ui_th_std0, self.ui_qv0, self.ui_p0, self.ui_kappa, self.ui_amplitude ]), VBox([ *self.ui_processes # , self.ui_ept # TODO ]), VBox([ self.ui_nx, self.ui_nz, self.ui_sdpg, self.ui_dt, self.ui_n_steps, self.ui_condensation_rtol_x, self.ui_condensation_rtol_thd, self.ui_adaptive, self.ui_condensation_coord, *self.ui_mpdata_options ]), # VBox([]) # TODO ]) layout.set_title(0, 'environment parameters') layout.set_title(1, 'processes') layout.set_title(2, 'discretisation') # layout.set_title(3, 'parallelisation') # TODO return layout
class Map(ipyleaflet.Map): def __init__(self, **kwargs): # Change defaults kwargs.setdefault('center', [0, 0]) kwargs.setdefault('zoom', 2) super(Map, self).__init__(**kwargs) # self.added_geometries = {} # self.added_images = {} self.is_shown = False self.EELayers = {} # CREATE TABS self.tabs = Tab() tab_names = ['Inspector', 'Assets', 'Tasks'] ## widgets self.inspectorWid = Accordion() # Inspector Widget self.assetsWid = Accordion() # Assets Widget self.tasksWid = HTML() # Tasks Widget childrenName = ['Inspector', 'Assets', 'Tasks'] childrenWid = [self.inspectorWid, self.assetsWid, self.tasksWid] # Dictonary to hold tab's widgets # (tab's name:widget) self.childrenDict = OrderedDict(zip(childrenName, childrenWid)) # Set tabs children self.tabs.children = self.childrenDict.values() # Set tabs names for i, name in enumerate(tab_names): self.tabs.set_title(i, name) # Handlers self.tabs.observe(self.handle_change_tab) self.handlers = {'Inspector': self.handle_inspector} # First handler: Inspector self.on_interaction(self.handlers['Inspector']) @property def added_images(self): return sum( [1 for val in self.EELayers.values() if val['type'] == 'Image']) @property def added_geometries(self): return sum( [1 for val in self.EELayers.values() if val['type'] == 'Geometry']) def create_assets_tab(self): # ASSETS TAB # Get assets root rootid = ee.data.getAssetRoots()[0]['id'] assets_list = ee.data.getList({'id': rootid}) widlist = [] namelist = [] for asset in assets_list: wid = HTML('') widlist.append(wid) name = asset['id'].split('/')[-1] ty = asset['type'] namelist.append('{} ({})'.format(name, ty)) self.assetsWid.children = widlist for i, name in enumerate(namelist): self.assetsWid.set_title(i, name) def show(self, inspector=True): """ Show the Map on the Notebook """ if not self.is_shown: # Layers Control lc = ipyleaflet.LayersControl() self.add_control(lc) self.is_shown = True if inspector: # Create Assets Tab self.create_assets_tab() # Display display(self, self.tabs) else: display(self) elif inspector: display(self, self.tabs) else: display(self) def addLayer(self, eeObject, visParams=None, name=None, show=True, opacity=None, inspect={ 'data': None, 'reducer': None, 'scale': None }): """ Adds a given EE object to the map as a layer. :param eeObject: Earth Engine object to add to map :type eeObject: ee.Image || ee.Geometry || ee.Feature :param visParams: visualization parameters. For Images can have the following arguments: bands, min, max. :type visParams: dict :param name: name for the layer :type name: str :param inspect: when adding a geometry or a feature you can pop up data from a desired layer. Params are: :data: the EEObject where to get the data from :reducer: the reducer to use :scale: the scale to reduce :return: the added layer :rtype: """ def addImage(image, name): # Check if layer exists if name in self.EELayers.keys(): print("Layer with name {} exists already, please choose" + "another name".format(name)) return params = get_image_tile(image, visParams, show, opacity) layer = ipyleaflet.TileLayer(url=params['url'], attribution=params['attribution'], name=name) self.add_layer(layer) self.EELayers[name] = { 'type': 'Image', 'object': image, 'visParams': visParams, 'layer': layer } return layer def addGeoJson(geometry, name): # Check if layer exists if name in self.EELayers.keys(): print("Layer with name {} exists already, please choose" + "another name".format(name)) return params = get_geojson_tile(geometry, inspect) layer = ipyleaflet.GeoJSON(data=params['geojson'], name=name, popup=HTML(params['pop'])) self.add_layer(layer) self.EELayers[name] = { 'type': 'Geometry', 'object': geometry, 'visParams': None, 'layer': layer } return layer # CASE: ee.Image if isinstance(eeObject, ee.Image): thename = name if name else 'Image {}'.format(self.added_images) addImage(eeObject, thename) elif isinstance(eeObject, ee.Geometry): thename = name if name else 'Geometry {}'.format( self.added_geometries) addGeoJson(eeObject, thename) elif isinstance(eeObject, ee.Feature): geom = eeObject.geometry() addGeoJson(geom) elif isinstance(eeObject, ee.ImageCollection): proxy = eeObject.sort('system:time_start') mosaic = ee.Image(proxy.mosaic()) thename = name if name else 'Mosaic {}'.format(self.added_images) addImage(mosaic, thename) else: print("`addLayer` doesn't support adding the specified object to" "the map") def removeLayer(self, name): """ Remove a layer by its name """ if name in self.EELayers.keys(): layer = self.EELayers[name]['layer'] self.remove_layer(layer) self.EELayers.pop(name) else: print('Layer {} is not present in the map'.format(name)) return def centerObject(self, eeObject, zoom=None, method=1): """ Center an eeObject :param eeObject: :param zoom: :param method: experimetal methods to estimate zoom for fitting bounds Currently: 1 or 2 :type: int """ bounds = get_bounds(eeObject) if isinstance(eeObject, ee.Geometry): centroid = eeObject.centroid().getInfo()['coordinates'] elif isinstance(eeObject, ee.Feature) or isinstance( eeObject, ee.Image): centroid = eeObject.geometry().centroid().getInfo()['coordinates'] elif isinstance(eeObject, list): pol = ee.Geometry.Polygon(inverse_coordinates(list)) centroid = pol.centroid().getInfo()['coordinates'] self.center = inverse_coordinates(centroid) if zoom: self.zoom = zoom else: self.zoom = get_zoom(bounds, method) def getCenter(self): """ Returns the coordinates at the center of the map. No arguments. Returns: Geometry.Point :return: """ center = self.center coords = inverse_coordinates(center) return ee.Geometry.Point(coords) def getBounds(self, asGeoJSON=True): """ Returns the bounds of the current map view, as a list in the format [west, south, east, north] in degrees. Arguments: asGeoJSON (Boolean, optional): If true, returns map bounds as GeoJSON. Returns: GeoJSONGeometry|List<Number>|String """ bounds = inverse_coordinates(self.bounds) if asGeoJSON: return ee.Geometry.Rectangle(bounds) else: return bounds def addTab(self, name, handler, widget=None): """ Add a Tab to the Panel. The handler is for the Map :param name: name for the new tab :type name: str :param handler: handle function for the new tab. Arguments of the function are: :type: the type of the event (click, mouseover, etc..) :coordinates: coordinates where the event occured [lon, lat] :widget: the widget inside the Tab :map: the Map instance :param widget: widget inside the Tab. Defaults to HTML('') :type widget: ipywidgets.Widget """ # Widget wid = widget if widget else HTML('') # Get tab's children as a list tab_children = list(self.tabs.children) # Get a list of tab's titles titles = [ self.tabs.get_title(i) for i, child in enumerate(tab_children) ] # Check if tab already exists if name not in titles: ntabs = len(tab_children) # Add widget as a new children self.childrenDict[name] = wid tab_children.append(wid) # Overwrite tab's children self.tabs.children = tab_children # Set name of the new tab self.tabs.set_title(ntabs, name) # Set the handler for the new tab def proxy_handler(f): def wrap(**kwargs): # Add widget to handler arguments kwargs['widget'] = self.childrenDict[name] coords = kwargs['coordinates'] kwargs['coordinates'] = inverse_coordinates(coords) kwargs['map'] = self return f(**kwargs) return wrap self.handlers[name] = proxy_handler(handler) else: print('Tab {} already exists, please choose another name'.format( name)) def handle_change_tab(self, change): """ Handle function to trigger when tab changes """ # Remove all handlers if change['name'] == 'selected_index': old = change['old'] new = change['new'] old_name = self.tabs.get_title(old) new_name = self.tabs.get_title(new) # Remove all handlers for handl in self.handlers.values(): self.on_interaction(handl, True) # Set new handler if new_name in self.handlers.keys(): self.on_interaction(self.handlers[new_name]) def handle_inspector(self, **change): """ Handle function for the Inspector Widget """ # Get click coordinates coords = inverse_coordinates(change['coordinates']) event = change['type'] # event type if event == 'click': # If the user clicked # Clear children // Loading self.inspectorWid.children = [HTML('wait a second please..')] self.inspectorWid.set_title(0, 'Loading...') # create a point where the user clicked point = ee.Geometry.Point(coords) # First Accordion row text (name) first = 'Point {} at {} zoom'.format(coords, self.zoom) namelist = [first] wids4acc = [HTML('')] # first row has no content for name, obj in self.EELayers.items(): # for every added layer # name = obj['name'] # IMAGES if obj['type'] == 'Image': # Get the image's values image = obj['object'] values = tools.get_value(image, point, 10, 'client') values = tools.sort_dict(values) # Create the content img_html = '' for band, value in values.items(): img_html += '<b>{}</b>: {}</br>'.format(band, value) wid = HTML(img_html) # append widget to list of widgets wids4acc.append(wid) namelist.append(name) # GEOMETRIES elif obj['type'] == 'Geometry': geom = obj['object'] data = str(geom.getInfo()) wid = HTML(data) wids4acc.append(wid) namelist.append(name) # Set children and children's name of inspector widget self.inspectorWid.children = wids4acc for i, n in enumerate(namelist): self.inspectorWid.set_title(i, n)
def _widget(self): """ Create IPython widget for display within a notebook """ try: return self._cached_widget except AttributeError: pass from ipywidgets import Layout, VBox, HBox, IntText, Button, HTML, Accordion layout = Layout(width='150px') if 'bokeh' in self.scheduler.services: link = self.dashboard_link link = '<p><b>Dashboard: </b><a href="%s" target="_blank">%s</a></p>\n' % (link, link) else: link = '' title = '<h2>%s</h2>' % type(self).__name__ title = HTML(title) dashboard = HTML(link) status = HTML(self._widget_status(), layout=Layout(min_width='150px')) request = IntText(0, description='Workers', layout=layout) scale = Button(description='Scale', layout=layout) minimum = IntText(0, description='Minimum', layout=layout) maximum = IntText(0, description='Maximum', layout=layout) adapt = Button(description='Adapt', layout=layout) accordion = Accordion([HBox([request, scale]), HBox([minimum, maximum, adapt])], layout=Layout(min_width='500px')) accordion.selected_index = None accordion.set_title(0, 'Manual Scaling') accordion.set_title(1, 'Adaptive Scaling') box = VBox([title, HBox([status, accordion]), dashboard]) self._cached_widget = box def adapt_cb(b): self.adapt(minimum=minimum.value, maximum=maximum.value) adapt.on_click(adapt_cb) def scale_cb(b): with log_errors(): n = request.value with ignoring(AttributeError): self._adaptive.stop() self.scale(n) scale.on_click(scale_cb) scheduler_ref = ref(self.scheduler) def update(): status.value = self._widget_status() pc = PeriodicCallback(update, 500, io_loop=self.scheduler.loop) self.scheduler.periodic_callbacks['cluster-repr'] = pc pc.start() return box
def _widget(self): """ Create IPython widget for display within a notebook """ try: return self._cached_widget except AttributeError: pass from ipywidgets import ( Layout, VBox, HBox, IntText, Button, HTML, Accordion, Text, ) layout = Layout(width="150px") if "dashboard" in self.scheduler.services: link = self.dashboard_link link = '<p><b>Dashboard: </b><a href="%s" target="_blank">%s</a></p>\n' % ( link, link, ) else: link = "" title = "<h2>%s</h2>" % type(self).__name__ title = HTML(title) dashboard = HTML(link) status = HTML(self._widget_status(), layout=Layout(min_width="150px")) request = IntText(0, description="Workers", layout=layout) scale = Button(description="Scale", layout=layout) request_cores = IntText(0, description="Cores", layout=layout) scale_cores = Button(description="Scale", layout=layout) request_memory = Text("O GB", description="Memory", layout=layout) scale_memory = Button(description="Scale", layout=layout) minimum = IntText(0, description="Minimum", layout=layout) maximum = IntText(0, description="Maximum", layout=layout) adapt = Button(description="Adapt", layout=layout) minimum_cores = IntText(0, description="Min cores", layout=layout) maximum_cores = IntText(0, description="Max cores", layout=layout) adapt_cores = Button(description="Adapt", layout=layout) minimum_mem = Text("0 GB", description="Min memory", layout=layout) maximum_mem = Text("0 GB", description="Max memory", layout=layout) adapt_mem = Button(description="Adapt", layout=layout) scale_hbox = [HBox([request, scale])] adapt_hbox = [HBox([minimum, maximum, adapt])] if hasattr(self, "jobqueue_worker_spec"): scale_hbox.append(HBox([request_cores, scale_cores])) scale_hbox.append(HBox([request_memory, scale_memory])) adapt_hbox.append(HBox([minimum_cores, maximum_cores, adapt_cores])) adapt_hbox.append(HBox([minimum_mem, maximum_mem, adapt_mem])) accordion = Accordion( [VBox(scale_hbox), VBox(adapt_hbox)], layout=Layout(min_width="500px")) accordion.selected_index = None accordion.set_title(0, "Manual Scaling") accordion.set_title(1, "Adaptive Scaling") box = VBox([title, HBox([status, accordion]), dashboard]) self._cached_widget = box def adapt_cb(b): self.adapt(minimum=minimum.value, maximum=maximum.value) def adapt_cores_cb(b): self.adapt(minimum_cores=minimum_cores.value, maximum_cores=maximum_cores.value) def adapt_mem_cb(b): self.adapt(minimum_memory=minimum_mem.value, maximum_memory=maximum_mem.value) adapt.on_click(adapt_cb) adapt_cores.on_click(adapt_cores_cb) adapt_mem.on_click(adapt_mem_cb) def scale_cb(request, kwarg): def request_cb(b): with log_errors(): arg = request.value with ignoring(AttributeError): self._adaptive.stop() local_kwargs = dict() local_kwargs[kwarg] = arg self.scale(**local_kwargs) return request_cb scale.on_click(scale_cb(request, "n")) scale_cores.on_click(scale_cb(request_cores, "cores")) scale_memory.on_click(scale_cb(request_memory, "memory")) def update(): status.value = self._widget_status() pc = PeriodicCallback(update, 500, io_loop=self.scheduler.loop) self.scheduler.periodic_callbacks["cluster-repr"] = pc pc.start() return box
def _widget(self): """ Create IPython widget for display within a notebook """ try: return self._cached_widget except AttributeError: pass from ipywidgets import Layout, VBox, HBox, IntText, Button, HTML, Accordion layout = Layout(width="150px") if "dashboard" in self.scheduler.services: link = self.dashboard_link link = '<p><b>Dashboard: </b><a href="%s" target="_blank">%s</a></p>\n' % ( link, link, ) else: link = "" title = "<h2>%s</h2>" % type(self).__name__ title = HTML(title) dashboard = HTML(link) status = HTML(self._widget_status(), layout=Layout(min_width="150px")) request = IntText(0, description="Workers", layout=layout) scale = Button(description="Scale", layout=layout) minimum = IntText(0, description="Minimum", layout=layout) maximum = IntText(0, description="Maximum", layout=layout) adapt = Button(description="Adapt", layout=layout) accordion = Accordion( [HBox([request, scale]), HBox([minimum, maximum, adapt])], layout=Layout(min_width="500px"), ) accordion.selected_index = None accordion.set_title(0, "Manual Scaling") accordion.set_title(1, "Adaptive Scaling") box = VBox([title, HBox([status, accordion]), dashboard]) self._cached_widget = box def adapt_cb(b): self.adapt(minimum=minimum.value, maximum=maximum.value) adapt.on_click(adapt_cb) def scale_cb(b): with log_errors(): n = request.value with ignoring(AttributeError): self._adaptive.stop() self.scale(n) scale.on_click(scale_cb) scheduler_ref = ref(self.scheduler) def update(): status.value = self._widget_status() pc = PeriodicCallback(update, 500, io_loop=self.scheduler.loop) self.scheduler.periodic_callbacks["cluster-repr"] = pc pc.start() return box
def _widget(self): """Create IPython widget for display within a notebook""" try: return self._cached_widget except AttributeError: pass try: from ipywidgets import ( HTML, Accordion, Button, HBox, IntText, Layout, Tab, VBox, ) except ImportError: self._cached_widget = None return None layout = Layout(width="150px") status = HTML(self._repr_html_()) if self._supports_scaling: request = IntText(0, description="Workers", layout=layout) scale = Button(description="Scale", layout=layout) minimum = IntText(0, description="Minimum", layout=layout) maximum = IntText(0, description="Maximum", layout=layout) adapt = Button(description="Adapt", layout=layout) accordion = Accordion( [HBox([request, scale]), HBox([minimum, maximum, adapt])], layout=Layout(min_width="500px"), ) accordion.selected_index = None accordion.set_title(0, "Manual Scaling") accordion.set_title(1, "Adaptive Scaling") def adapt_cb(b): self.adapt(minimum=minimum.value, maximum=maximum.value) update() adapt.on_click(adapt_cb) def scale_cb(b): with log_errors(): n = request.value with suppress(AttributeError): self._adaptive.stop() self.scale(n) update() scale.on_click(scale_cb) else: accordion = HTML("") scale_status = HTML(self._scaling_status()) tab = Tab() tab.children = [status, VBox([scale_status, accordion])] tab.set_title(0, "Status") tab.set_title(1, "Scaling") self._cached_widget = tab def update(): status.value = self._repr_html_() scale_status.value = self._scaling_status() cluster_repr_interval = parse_timedelta( dask.config.get("distributed.deploy.cluster-repr-interval", default="ms")) pc = PeriodicCallback(update, cluster_repr_interval * 1000) self.periodic_callbacks["cluster-repr"] = pc pc.start() return tab
class ExplorerFeatureSelect(ExplorerPane): def __init__(self, ed: ExplorerData, box_universe, box_name, parent_widget=None): super().__init__(parent_widget=parent_widget) self.data = ed self._box_universe = box_universe self._box_name = box_name self._suspend_updates = False try: clusterdef = ChainedBox(self._box_universe, self._box_name) clusterdef_relevant = clusterdef.relevant_and_demanded_features clusterdef_demanded = clusterdef.demanded_features self.checkers = [] for i in self.data.all_performance_measures: controller = Checkbox( value=i in clusterdef_relevant, description=i, disabled=i in clusterdef_demanded, style={'description_width': 'initial'}, tooltip=i if i not in clusterdef_demanded else _demanded_tooltip, ) self.checkers.append(controller) for i in self.data.all_strategy_names: controller = Checkbox( value=i in clusterdef_relevant, description=i, disabled=i in clusterdef_demanded, style={'description_width': 'initial'}, tooltip=i if i not in clusterdef_demanded else _demanded_tooltip, ) self.checkers.append(controller) for i in self.data.all_risk_factors: controller = Checkbox( value=i in clusterdef_relevant, description=i, disabled=i in clusterdef_demanded, style={'description_width': 'initial'}, tooltip=i if i not in clusterdef_demanded else _demanded_tooltip, ) self.checkers.append(controller) for s in range(len(self.checkers)): self.checkers[s].observe(self.toggle_check) self.ui_risks = VBox([ ch for ch in self.checkers if ch.description in self.data.all_risk_factors ], ) self.ui_strategies = VBox([ ch for ch in self.checkers if ch.description in self.data.all_strategy_names ], ) self.ui_perform = VBox([ ch for ch in self.checkers if ch.description in self.data.all_performance_measures ], ) self.accordion = Accordion( children=[self.ui_strategies, self.ui_risks, self.ui_perform], layout=Layout(width='600px', )) self.accordion.set_title(0, 'Policy Levers') self.accordion.set_title(1, 'Exogenous Uncertainties') self.accordion.set_title(2, 'Performance Measures') self.accordion_filter = Text(value='.*', placeholder='.*', description='Filter:', disabled=False) self.accordion_filter.observe(self.filter_checkboxes) self.current_relevant_attributes = Select( options=[], # value='OSX', rows=30, description='', disabled=False) # self.load_current_relevant_attributes_button = Button( # description=f"Next", # disabled=False, # button_style='', # 'success', 'info', 'warning', 'danger' or '' # tooltip='Load', # # icon='check' # ) # self.load_current_relevant_attributes_button.on_click( # self.load_selected_features_into_thresholds # ) self.next_button.on_click( self.load_selected_features_into_thresholds) # self.stack = VBox([ # self.header_area, # HBox([ # VBox([ # self.accordion_filter, # self.accordion, # ]), # VBox([ # HTML_widget(value=f"<b>{RELEVANT}</b>", placeholder=f"These things will be loaded"), # self.current_relevant_attributes, # self.load_current_relevant_attributes_button, # ]), # ]), # self.footer, # ]) self.make_stack( HBox([ VBox([ self.accordion_filter, self.accordion, ]), VBox([ HTML_widget( value=f"<b>{RELEVANT}</b>", placeholder=f"These things will be loaded"), self.current_relevant_attributes, # self.load_current_relevant_attributes_button, ]), ]), ) self.set_header(clusterdef.names) self.recompile_list_of_active_features( None) # initial interactive plots except: logger.exception("error in initialize_sliders") raise @property def joint_data(self): return self.data.joint_data def set_header(self, names): logger.info(f"{self.__class__.__name__}.set_header( {names} )") self.header.clear_output() with self.header: for name in names[:-1]: display(HTML(f"<h4>▽ {name}</h4>")) display(HTML(f"<h1>▷ {names[-1]}</h1>")) def toggle_check(self, action_info): try: logger.debug(f"toggle_check: {action_info}") owner = action_info.get('owner') if owner is None: return label = owner.description if action_info.get('name') == 'value': if label is not None: value = action_info.get('new') if value is not None: logger.info(f"toggle_check: set {label} to {value}") if value: self._box_universe[ self._box_name].relevant_features.add(label) self.recompile_list_of_active_features(None) else: self._box_universe[ self._box_name].relevant_features.discard( label) self.recompile_list_of_active_features(None) except: logger.exception("error in ExplorerFeatureSelect.toggle_check") raise def recompile_list_of_active_features(self, content): if content is not None and content['name'] == '_property_lock': return if self._suspend_updates: return try: owner = content['owner'] except: owner = None on_risks = [] on_strategies = [] on_perform = [] for ch in self.checkers: if ch.value: if ch.description in self.data.all_risk_factors_: on_risks.append(ch.description) if ch.description in self.data.all_strategy_names_: on_strategies.append(ch.description) if ch.description in self.data.all_performance_measures_: on_perform.append(ch.description) current = [] if on_strategies: current.append("-- STRATEGIES --") current.extend(on_strategies) if on_risks: current.append("-- RISK FACTORS --") current.extend(on_risks) if on_perform: current.append("-- PERFORMANCE MEASURES --") current.extend(on_perform) self.current_relevant_attributes.options = current def filter_checkboxes(self, event): if event.get('name') == 'value': logger.debug(f"filter_checkboxes:{event}") filter = event.get('new') if filter is not None: if filter == "" or filter == "*": for ch in self.checkers: # ch.layout.visibility = 'visible' ch.layout.display = 'flex' else: import re pattern = re.compile(filter) for ch in self.checkers: if pattern.search(ch.description): #ch.layout.visibility = 'visible' ch.layout.display = 'flex' else: #ch.layout.visibility = 'hidden' ch.layout.display = 'none' def load_selected_features_into_thresholds(self, event): execution_tracer(logger, event) try: self._parent_widget.load_cluster(self._box_name) [h.flush() for h in logger.handlers[0]] except: logger.exception() raise execution_tracer(logger, "END")
def make_config(self): layout = Layout() style = {"description_width": "initial"} checkbox1 = Checkbox(description="Show Targets", value=self.net.config["show_targets"], layout=layout, style=style) checkbox1.observe(lambda change: self.set_attr(self.net.config, "show_targets", change["new"]), names='value') checkbox2 = Checkbox(description="Errors", value=self.net.config["show_errors"], layout=layout, style=style) checkbox2.observe(lambda change: self.set_attr(self.net.config, "show_errors", change["new"]), names='value') hspace = IntText(value=self.net.config["hspace"], description="Horizontal space between banks:", style=style, layout=layout) hspace.observe(lambda change: self.set_attr(self.net.config, "hspace", change["new"]), names='value') vspace = IntText(value=self.net.config["vspace"], description="Vertical space between layers:", style=style, layout=layout) vspace.observe(lambda change: self.set_attr(self.net.config, "vspace", change["new"]), names='value') self.feature_bank = Select(description="Details:", value=self.net.config["dashboard.features.bank"], options=[""] + [layer.name for layer in self.net.layers], rows=1) self.feature_bank.observe(self.regenerate, names='value') self.control_select = Select( options=['Test', 'Train'], value=self.net.config["dashboard.dataset"], description='Dataset:', rows=1 ) self.control_select.observe(self.change_select, names='value') column1 = [self.control_select, self.zoom_slider, hspace, vspace, HBox([checkbox1, checkbox2]), self.feature_bank, self.feature_columns, self.feature_scale ] ## Make layer selectable, and update-able: column2 = [] layer = self.net.layers[-1] self.layer_select = Select(description="Layer:", value=layer.name, options=[layer.name for layer in self.net.layers], rows=1) self.layer_select.observe(self.update_layer_selection, names='value') column2.append(self.layer_select) self.layer_visible_checkbox = Checkbox(description="Visible", value=layer.visible, layout=layout) self.layer_visible_checkbox.observe(self.update_layer, names='value') column2.append(self.layer_visible_checkbox) self.layer_colormap = Select(description="Colormap:", options=[""] + AVAILABLE_COLORMAPS, value=layer.colormap if layer.colormap is not None else "", layout=layout, rows=1) self.layer_colormap_image = HTML(value="""<img src="%s"/>""" % self.net._image_to_uri(self.make_colormap_image(layer.colormap))) self.layer_colormap.observe(self.update_layer, names='value') column2.append(self.layer_colormap) column2.append(self.layer_colormap_image) ## get dynamic minmax; if you change it it will set it in layer as override: minmax = layer.get_act_minmax() self.layer_mindim = FloatText(description="Leftmost color maps to:", value=minmax[0], style=style) self.layer_maxdim = FloatText(description="Rightmost color maps to:", value=minmax[1], style=style) self.layer_mindim.observe(self.update_layer, names='value') self.layer_maxdim.observe(self.update_layer, names='value') column2.append(self.layer_mindim) column2.append(self.layer_maxdim) output_shape = layer.get_output_shape() self.layer_feature = IntText(value=layer.feature, description="Feature to show:", style=style) self.svg_rotate = Checkbox(description="Rotate", value=layer.visible, layout=layout) self.layer_feature.observe(self.update_layer, names='value') column2.append(self.layer_feature) self.svg_rotate = Checkbox(description="Rotate network", value=self.net.config["svg_rotate"], style={"description_width": 'initial'}, layout=Layout(width="52%")) self.svg_rotate.observe(lambda change: self.set_attr(self.net.config, "svg_rotate", change["new"]), names='value') self.save_config_button = Button(icon="save", layout=Layout(width="10%")) self.save_config_button.on_click(self.save_config) column2.append(HBox([self.svg_rotate, self.save_config_button])) config_children = HBox([VBox(column1, layout=Layout(width="100%")), VBox(column2, layout=Layout(width="100%"))]) accordion = Accordion(children=[config_children]) accordion.set_title(0, self.net.name) accordion.selected_index = None return accordion
def create_object_output(object): ''' Create a output Widget for Images, Geometries and Features ''' ty = object.__class__.__name__ if ty == 'Image': info = object.getInfo() image_id = info['id'] if 'id' in info else 'No Image ID' prop = info.get('properties') bands = info.get('bands') bands_names = [band.get('id') for band in bands] # BAND PRECISION bands_precision = [] for band in bands: data = band.get('data_type') if data: precision = data.get('precision') bands_precision.append(precision) # BAND CRS bands_crs = [] for band in bands: crs = band.get('crs') bands_crs.append(crs) # BAND MIN AND MAX bands_min = [] for band in bands: data = band.get('data_type') if data: bmin = data.get('min') bands_min.append(bmin) bands_max = [] for band in bands: data = band.get('data_type') if data: bmax = data.get('max') bands_max.append(bmax) # BANDS new_band_names = [] zipped_data = zip(bands_names, bands_precision, bands_min, bands_max, bands_crs) for name, ty, mn, mx, epsg in zipped_data: value = '<li><b>{}</b> ({}) {} to {} - {}</li>'.format( name, ty, mn, mx, epsg) new_band_names.append(value) bands_wid = HTML('<ul>' + ''.join(new_band_names) + '</ul>') # PROPERTIES if prop: new_properties = [] for key, val in prop.items(): value = '<li><b>{}</b>: {}</li>'.format(key, val) new_properties.append(value) prop_wid = HTML('<ul>' + ''.join(new_properties) + '</ul>') else: prop_wid = HTML('Image has no properties') # ID header = HTML('<b>Image id:</b> {id} </br>'.format(id=image_id)) acc = Accordion([bands_wid, prop_wid]) acc.set_title(0, 'Bands') acc.set_title(1, 'Properties') acc.selected_index = None # thisp will unselect all return VBox([header, acc]) elif ty == 'FeatureCollection': try: info = object.getInfo() except: print('FeatureCollection limited to 4000 features') info = object.limit(4000) return create_accordion(info) else: info = object.getInfo() return create_accordion(info)
def _widget(self): """ Create IPython widget for display within a notebook """ try: return self._cached_widget except AttributeError: pass try: from ipywidgets import Layout, VBox, HBox, IntText, Button, HTML, Accordion except ImportError: self._cached_widget = None return None layout = Layout(width="150px") if self.dashboard_link: dashboard_link = ( '<p><b>Dashboard: </b><a href="%s" target="_blank">%s</a></p>\n' % (self.dashboard_link, self.dashboard_link) ) else: dashboard_link = "" if self.jupyter_link: jupyter_link = ( '<p><b>Jupyter: </b><a href="%s" target="_blank">%s</a></p>\n' % (self.jupyter_link, self.jupyter_link) ) else: jupyter_link = "" title = "<h2>%s</h2>" % self._cluster_class_name title = HTML(title) dashboard = HTML(dashboard_link) jupyter = HTML(jupyter_link) status = HTML(self._widget_status(), layout=Layout(min_width="150px")) if self._supports_scaling: request = IntText( self.initial_node_count, description="Nodes", layout=layout ) scale = Button(description="Scale", layout=layout) minimum = IntText(0, description="Minimum", layout=layout) maximum = IntText(0, description="Maximum", layout=layout) adapt = Button(description="Adapt", layout=layout) accordion = Accordion( [HBox([request, scale]), HBox([minimum, maximum, adapt])], layout=Layout(min_width="500px"), ) accordion.selected_index = None accordion.set_title(0, "Manual Scaling") accordion.set_title(1, "Adaptive Scaling") def adapt_cb(b): self.adapt(minimum=minimum.value, maximum=maximum.value) update() adapt.on_click(adapt_cb) def scale_cb(b): with log_errors(): n = request.value with suppress(AttributeError): self._adaptive.stop() self.scale(n) update() scale.on_click(scale_cb) else: accordion = HTML("") box = VBox([title, HBox([status, accordion]), jupyter, dashboard]) self._cached_widget = box def update(): self.close_when_disconnect() status.value = self._widget_status() pc = PeriodicCallback(update, 500) # , io_loop=self.loop) self.periodic_callbacks["cluster-repr"] = pc pc.start() return box
def make_config(self): layout = Layout() style = {"description_width": "initial"} checkbox1 = Checkbox(description="Show Targets", value=self.net.config["show_targets"], layout=layout, style=style) checkbox1.observe(lambda change: self.set_attr(self.net.config, "show_targets", change["new"]), names='value') checkbox2 = Checkbox(description="Errors", value=self.net.config["show_errors"], layout=layout, style=style) checkbox2.observe(lambda change: self.set_attr(self.net.config, "show_errors", change["new"]), names='value') hspace = IntText(value=self.net.config["hspace"], description="Horizontal space between banks:", style=style, layout=layout) hspace.observe(lambda change: self.set_attr(self.net.config, "hspace", change["new"]), names='value') vspace = IntText(value=self.net.config["vspace"], description="Vertical space between layers:", style=style, layout=layout) vspace.observe(lambda change: self.set_attr(self.net.config, "vspace", change["new"]), names='value') self.feature_bank = Select(description="Features:", value=self.net.config["dashboard.features.bank"], options=[""] + [layer.name for layer in self.net.layers if self.net._layer_has_features(layer.name)], rows=1) self.feature_bank.observe(self.regenerate, names='value') self.control_select = Select( options=['Test', 'Train'], value=self.net.config["dashboard.dataset"], description='Dataset:', rows=1 ) self.control_select.observe(self.change_select, names='value') column1 = [self.control_select, self.zoom_slider, hspace, vspace, HBox([checkbox1, checkbox2]), self.feature_bank, self.feature_columns, self.feature_scale ] ## Make layer selectable, and update-able: column2 = [] layer = self.net.layers[-1] self.layer_select = Select(description="Layer:", value=layer.name, options=[layer.name for layer in self.net.layers], rows=1) self.layer_select.observe(self.update_layer_selection, names='value') column2.append(self.layer_select) self.layer_visible_checkbox = Checkbox(description="Visible", value=layer.visible, layout=layout) self.layer_visible_checkbox.observe(self.update_layer, names='value') column2.append(self.layer_visible_checkbox) self.layer_colormap = Select(description="Colormap:", options=[""] + AVAILABLE_COLORMAPS, value=layer.colormap if layer.colormap is not None else "", layout=layout, rows=1) self.layer_colormap_image = HTML(value="""<img src="%s"/>""" % self.net._image_to_uri(self.make_colormap_image(layer.colormap))) self.layer_colormap.observe(self.update_layer, names='value') column2.append(self.layer_colormap) column2.append(self.layer_colormap_image) ## get dynamic minmax; if you change it it will set it in layer as override: minmax = layer.get_act_minmax() self.layer_mindim = FloatText(description="Leftmost color maps to:", value=minmax[0], style=style) self.layer_maxdim = FloatText(description="Rightmost color maps to:", value=minmax[1], style=style) self.layer_mindim.observe(self.update_layer, names='value') self.layer_maxdim.observe(self.update_layer, names='value') column2.append(self.layer_mindim) column2.append(self.layer_maxdim) output_shape = layer.get_output_shape() self.layer_feature = IntText(value=layer.feature, description="Feature to show:", style=style) self.svg_rotate = Checkbox(description="Rotate", value=layer.visible, layout=layout) self.layer_feature.observe(self.update_layer, names='value') column2.append(self.layer_feature) self.svg_rotate = Checkbox(description="Rotate network", value=self.net.config["svg_rotate"], style={"description_width": 'initial'}, layout=Layout(width="52%")) self.svg_rotate.observe(lambda change: self.set_attr(self.net.config, "svg_rotate", change["new"]), names='value') self.save_config_button = Button(icon="save", layout=Layout(width="10%")) self.save_config_button.on_click(self.save_config) column2.append(HBox([self.svg_rotate, self.save_config_button])) config_children = HBox([VBox(column1, layout=Layout(width="100%")), VBox(column2, layout=Layout(width="100%"))]) accordion = Accordion(children=[config_children]) accordion.set_title(0, self.net.name) accordion.selected_index = None return accordion
def _widget(self): """ Create IPython widget for display within a notebook """ try: return self._cached_widget except AttributeError: pass from ipywidgets import Layout, VBox, HBox, IntText, Button, HTML, Accordion layout = Layout(width='150px') if 'bokeh' in self.scheduler.services: template = config.get('diagnostics-link', 'http://{host}:{port}/status') host = self.scheduler.address.split('://')[1].split(':')[0] port = self.scheduler.services['bokeh'].port link = template.format(host=host, port=port, **os.environ) link = '<p><b>Dashboard: </b><a href="%s" target="_blank">%s</a></p>\n' % (link, link) else: link = '' title = '<h2>%s</h2>' % type(self).__name__ title = HTML(title) dashboard = HTML(link) status = HTML(self._widget_status(), layout=Layout(min_width='150px')) request = IntText(0, description='Workers', layout=layout) scale = Button(description='Scale', layout=layout) minimum = IntText(0, description='Minimum', layout=layout) maximum = IntText(0, description='Maximum', layout=layout) adapt = Button(description='Adapt', layout=layout) accordion = Accordion([HBox([request, scale]), HBox([minimum, maximum, adapt])], layout=Layout(min_width='500px')) accordion.selected_index = None accordion.set_title(0, 'Manual Scaling') accordion.set_title(1, 'Adaptive Scaling') box = VBox([title, HBox([status, accordion]), dashboard]) self._cached_widget = box def adapt_cb(b): self.adapt(minimum=minimum.value, maximum=maximum.value) adapt.on_click(adapt_cb) def scale_cb(b): with log_errors(): n = request.value with ignoring(AttributeError): self._adaptive.stop() self.scale(n) scale.on_click(scale_cb) scheduler_ref = ref(self.scheduler) def update(): status.value = self._widget_status() pc = PeriodicCallback(update, 500, io_loop=self.scheduler.loop) self.scheduler.periodic_callbacks['cluster-repr'] = pc pc.start() return box
def _widget(self): """ Create IPython widget for display within a notebook """ try: return self._cached_widget except AttributeError: pass if self.asynchronous: return None try: from ipywidgets import Layout, VBox, HBox, IntText, Button, HTML, Accordion except ImportError: self._cached_widget = None return None layout = Layout(width="150px") title = HTML("<h2>YarnCluster</h2>") status = HTML(self._widget_status(), layout=Layout(min_width="150px")) request = IntText(0, description="Workers", layout=layout) scale = Button(description="Scale", layout=layout) minimum = IntText(0, description="Minimum", layout=layout) maximum = IntText(0, description="Maximum", layout=layout) adapt = Button(description="Adapt", layout=layout) accordion = Accordion( [HBox([request, scale]), HBox([minimum, maximum, adapt])], layout=Layout(min_width="500px"), ) accordion.selected_index = None accordion.set_title(0, "Manual Scaling") accordion.set_title(1, "Adaptive Scaling") @adapt.on_click def adapt_cb(b): self.adapt(minimum=minimum.value, maximum=maximum.value) @scale.on_click def scale_cb(b): with log_errors(): self.scale(request.value) app_id = HTML("<p><b>Application ID: </b>{0}</p>".format(self.app_id)) elements = [title, HBox([status, accordion]), app_id] if self.dashboard_link is not None: link = HTML( '<p><b>Dashboard: </b><a href="{0}" target="_blank">{0}' "</a></p>\n".format(self.dashboard_link)) elements.append(link) self._cached_widget = box = VBox(elements) self._status_widget = status return box
def qa(): # path_plug = "cbm/foi/foi_db_func/" path_data = normpath(join(config.get_value(['paths', 'temp']), 'qa')) progress = Output() def outlog(*text): with progress: print(*text) qa_info = HTML( value="""The CbM Quality Assurance (QA) is a framework intended to enable the Member State (MS) to report to the Commission about the state of one of the components inside the control and management system.<br> To run the Quality Assessment (QA) procedures direct access to the database and the object storage with CARD data is required.<br> """, placeholder='QA Information', ) # Connect db_select = Dropdown(options=[db for db in config.get_value(['db'])], description='Configure:', disabled=True, layout=Layout(width='140px')) db_config = Button(value=False, disabled=False, button_style='info', tooltip='Configure db connection.', icon='cogs', layout=Layout(width='40px')) db_box = HBox([db_select, db_config]) db_conf_box = HBox([]) @db_config.on_click def db_config_on_click(b): if db_conf_box.children == (): db_conf_box.children = [settings_ds.direct_conn()] else: db_conf_box.children = () accor = Accordion(children=[ VBox([db_box, db_conf_box]), ext_func.upload_shp(path_data), ext_func.create_tables(), ext_card2db.dias_cat(), settings_ds.direct(), ext_func.extraction(), VBox([get_panel.get()], layout=Layout(border='1px solid black')), view_panel.view() ]) def on_tb_name(change): # Get to the widget that is inside the accordion widget # Any change of the order of the widgets within the widgets # box will break this connection. new_value = accor.children[1].children[3].children[0].value accor.children[2].children[1].value = new_value accor.children[1].children[3].children[0].observe(on_tb_name, 'value') def on_start(change): new_value = accor.children[3].children[2].children[0].value accor.children[5].children[0].children[0].value = new_value accor.children[3].children[2].children[0].observe(on_start, 'value') def on_end(change): new_value = accor.children[3].children[2].children[1].value accor.children[5].children[0].children[1].value = new_value accor.children[3].children[2].children[1].observe(on_end, 'value') accor.set_title(0, "1. Connect to database and object storage.") accor.set_title( 1, "2. Upload .shp, with all the required files (cpg, dbf, prj, shx).") accor.set_title(2, "3. Create the essential CbM tables.") accor.set_title( 3, "4. Add CARD metadata to database table 'xx_dias_catalogue'.") accor.set_title(4, "5. Configure database tables.") accor.set_title(5, "6. Run parcel extraction routines.") accor.set_title(6, "7. Get parcel information, time series and images.") accor.set_title(7, "8. View parcel data.") wbox = VBox([qa_info, accor]) return wbox
class __Databases(object): """Database browser implementation Args: spark (SparkSession): Spark Session object """ def __init__(self, spark): self.spark = spark def create(self): """Create the sidecar view""" self.sc = Sidecar( title="Databases-%s" % os.environ["DBJL_CLUSTER"].split("-")[-1], layout=Layout(width="300px"), ) self.refresh = Button(description="refresh") self.refresh.on_click(self.on_refresh) self.output = Output( layout=Layout( height="600px", width="320px", overflow_x="scroll", overflow_y="scroll" ) ) self.output.add_class("db-detail") self.selects = [] self.accordion = Accordion(children=[]) with self.sc: display(VBox([self.refresh, self.accordion, self.output])) self.update() self.set_css() def on_refresh(self, b): """Refresh handler Args: b (ipywidgets.Button): clicked button """ self.selects = [] self.update() def update(self): """Update the view when an element was selected""" tables = {} for obj in self.spark.sql("show tables").rdd.collect(): db = obj[0] table = obj[1] temp = obj[2] if temp and db == "": db = "temp" if tables.get(db, None) is None: tables[db] = [] if temp: tables[db].append("%s (temp)" % table) else: tables[db].append(table) for db in sorted(tables.keys()): select = Select(options=[""] + sorted(tables[db]), disabled=False) select.observe(self.on_click(db, self), names="value") self.selects.append(select) self.accordion.children = self.selects for i, db in enumerate(sorted(tables.keys())): self.accordion.set_title(i, db) def on_click(self, db, parent): """Click handler providing db and parent as context Args: db (str): database name parent (object): parent object """ def f(change): if change["old"] is not None: parent.output.clear_output() with parent.output: if db == "temp": table = change["new"] else: table = "%s.%s" % (db, change["new"]) if table.endswith(" (temp)"): table = table[:-7] try: schema = parent.spark.sql("describe extended %s" % table) rows = int(parent.spark.conf.get("spark.sql.repl.eagerEval.maxNumRows")) parent.spark.conf.set("spark.sql.repl.eagerEval.maxNumRows", 1000) display(schema) parent.spark.conf.set("spark.sql.repl.eagerEval.maxNumRows", rows) except: print("schema cannot be accessed, table most probably broken") return f def close(self): """Close view""" self.selects = [] self.sc.close() def set_css(self): """Set CSS""" display( HTML( """ <style> .db-detail .p-Widget { overflow: visible; } </style> """ ) )
def _build_instance_creation_widgets(self) -> Tuple[list, VBox]: """ Build widgets for creating instance and put them into their respective accordions :return: list of instance creation widgets (for easier manipulation and getting values from them), VBox (layout) to be displayed in a tab """ instance_widgets = [] style = {'description_width': '60%', 'width': 'auto'} # create widget for each variable for e, column in enumerate(self.feature_names): if e in self.categorical_names: # for categoricals make dropdown with name, but value as a number (from 0) options = { key: val for val, key in enumerate(self.categorical_names[e]) } instance_widgets.append( Dropdown(description=f"{column}:", options=options, style=style)) else: instance_widgets.append( FloatText(description=f"{column}: ", style=style)) # fill grid with widgets widgets_grid = GridspecLayout(ceil(self.X_train.shape[1] / 2), 2, layout=Layout(overflow='scroll', max_height='25em')) for e, item in enumerate(instance_widgets): widgets_grid[e // 2, e % 2] = item # add the selection of instance in train data fill_from_df = widgets.BoundedIntText( value=0, min=0, max=len(self.X_train) - 1, step=1, description='Instance id:', style=style, description_tooltip= 'When changed all values in "Build your own instance" will be set accordingly' ) # connect instance selection to builder widgets def on_value_change(change): instance = self.X_train[change['new']] for e, val in enumerate(instance): instance_widgets[e].value = val fill_from_df.observe(on_value_change, names='value') # importing instance from variable instance_variable = UpdatingCombobox( options_keys=self.kernel_globals.keys(), value='', description='Variable with instance:', description_tooltip= 'Variable with your instance as numpy array, pandas dataframe or series.\n' 'After clicking on "import instance", the values in "Build your own instance"' ' will be set accordingly.', style=style) def fill_from_variable(orig_btn): # param ignored instance = self._get_variable_from_kernel(instance_variable.value) try: for e, val in enumerate(instance): instance_widgets[e].value = val # reset to without error import_box.children = [instance_variable, import_btn] except: # add error to box import_box.children = [ instance_variable, import_btn, import_error ] import_error = HTML("Import from variable failed\n") import_btn = Button(description='Import') import_btn.on_click(fill_from_variable) import_box = VBox(children=(instance_variable, import_btn)) # put both into separate accordions to allow both opened at the same time accordion1 = Accordion(children=[fill_from_df, import_box]) accordion1.set_title(0, 'Select instance from dataset') accordion1.set_title(1, 'Import instance from variable') accordion2 = Accordion(children=[widgets_grid]) accordion2.set_title(0, 'Build your own instance') accordions = VBox([accordion1, accordion2]) # set everything to first instance on_value_change({'new': 0}) return instance_widgets, accordions