def add_route_to_map(route, some_map, color='blue'): """ Add a route from the HERE REST API to the given map. This includes markers for all points where a maneuver is needed, like 'turn left'. And it includes a path with lat/lons from start to end and little circle markers around them. """ path_positions = list(chunks(route[0]['shape'], 2)) maneuvers = { (man['position']['latitude'], man['position']['longitude']): man['instruction'] for man in route[0]['leg'][0]['maneuver']} polyline = Polyline( locations=path_positions, color=color, fill=False ) some_map += polyline for lat, lon in path_positions: if (lat, lon) in maneuvers: some_map += CircleMarker(location=(lat, lon), radius=2) marker = Marker(location=(lat, lon), draggable=False) message1 = HTML() message1.value = maneuvers[(lat, lon)] marker.popup = message1 some_map += marker else: some_map += CircleMarker(location=(lat, lon), radius=3)
def log_progress(sequence, every=None, size=None): is_iterator = False start_tic = time.time() if size is None: try: size = len(sequence) except TypeError: is_iterator = True if size is not None: if every is None: if size <= 200: every = 1 else: every = size / 200 # every 0.5% else: assert every is not None, 'sequence is iterator, set every' if is_iterator: progress = IntProgress(min=0, max=1, value=1) progress.bar_style = 'info' else: progress = IntProgress(min=0, max=size, value=0) label = HTML() box = VBox(children=[label, progress]) display(box) index = 0 try: for index, record in enumerate(sequence, 1): if index == 1 or index % every == 0: if is_iterator: label.value = '{index} / ?'.format(index=index) else: progress.value = index label.value = u'{index} / {size}'.format( index=index, size=size ) yield record except: progress.bar_style = 'danger' raise else: progress.bar_style = 'success' progress.value = index # pretty human readable time diff if False: import dateutil.relativedelta attrs = ['years', 'months', 'days', 'hours', 'minutes', 'seconds'] delta = dateutil.relativedelta.relativedelta(seconds=time.time()-start_tic) elapsed = " ".join(['%d %s' % (getattr(delta, attr), getattr(delta, attr) > 1 and attr or attr[:-1]) for attr in attrs if getattr(delta, attr)])+" %d usec"%((tdiff-int(tdiff))*1000000) label.value = u'{index} : {elapsed}'.format(index=index or '?', elapsed=elapsed) else: # simple time in sec label.value = u'{index} : {elapsed:0.2f}s'.format(index=index or '?', elapsed=time.time()-start_tic)
def progress_bar(sequence, every=None, size=None, name='Progress'): '''Source: https://github.com/kuk/log-progress. Makes progress bar during iterating through sequence. Example: from time import sleep for i in progress_bar([1,2,3,4],every=1): sleep(1)''' is_iterator = False if size is None: try: size = len(sequence) except TypeError: is_iterator = True if size is not None: if every is None: if size <= 200: every = 1 else: every = int(size / 200) # every 0.5% else: assert every is not None, 'sequence is iterator, set every' if is_iterator: progress = IntProgress(min=0, max=1, value=1) progress.bar_style = 'info' else: progress = IntProgress(min=0, max=size, value=0) label = HTML() box = VBox(children=[label, progress]) display(box) index = 0 try: for index, record in enumerate(sequence, 1): if index == 1 or index % every == 0: if is_iterator: label.value = '{name}: {index} / ?'.format( name=name, index=index ) else: progress.value = index label.value = u'{name}: {index} / {size}'.format( name=name, index=index, size=size ) yield record except: progress.bar_style = 'danger' raise else: progress.bar_style = 'success' progress.value = index label.value = "{name}: {index}".format( name=name, index=str(index or '?') )
def log_progress(sequence, every=None, size=None, name='Items'): from ipywidgets import IntProgress, HTML, VBox from IPython.display import display is_iterator = False if size is None: try: size = len(sequence) except TypeError: is_iterator = True if size is not None: if every is None: if size <= 200: every = 1 else: every = int(size / 200) # every 0.5% else: assert every is not None, 'sequence is iterator, set every' if is_iterator: progress = IntProgress(min=0, max=1, value=1) progress.bar_style = 'info' else: progress = IntProgress(min=0, max=size, value=0) label = HTML() box = VBox(children=[label, progress]) display(box) index = 0 try: for index, record in enumerate(sequence, 1): if index == 1 or index % every == 0: if is_iterator: label.value = '{name}: {index} / ?'.format( name=name, index=index ) else: progress.value = index label.value = u'{name}: {index} / {size}'.format( name=name, index=index, size=size ) yield record except Exception: progress.bar_style = 'danger' raise else: progress.bar_style = 'success' progress.value = index label.value = "{name}: {index}".format( name=name, index=str(index or '?') )
def log_progress(sequence, every=None, size=None): """Widget based progress bar for Jupyter (IPython Notebook) Author: Kukushkin Alexander Source: https://github.com/alexanderkuk/log-progress """ from ipywidgets import IntProgress, HTML, VBox from IPython.display import display is_iterator = False if size is None: try: size = len(sequence) except TypeError: is_iterator = True if size is not None: if every is None: if size <= 200: every = 1 else: every = size / 200 # every 0.5% else: assert every is not None, 'sequence is iterator, set every' if is_iterator: progress = IntProgress(min=0, max=1, value=1) progress.bar_style = 'info' else: progress = IntProgress(min=0, max=size, value=0) label = HTML() box = VBox(children=[label, progress]) display(box) index = 0 try: for index, record in enumerate(sequence, 1): if index == 1 or index % every == 0: if is_iterator: label.value = '{index} / ?'.format(index=index) else: progress.value = index label.value = u'{index} / {size}'.format( index=index, size=size ) yield record except: progress.bar_style = 'danger' raise else: progress.bar_style = 'success' progress.value = index label.value = str(index or '?')
def get_ui_elements(self): """ Obtiene los componentes gráficos del juego Devuelve un contenedor con los botones y el visor del juego Parámetros: Ninguno """ # visor HTML donde se representará el juego visor = HTML() # Crea un desplegable con los niveles desplegable = Dropdown(description='Elija nivel:') # Botones para las direcciones up = Button(description="^") down = Button(description="v") right = Button(description=">") left = Button(description="<") empty = Button(description=" ") empty.margin = 2 direcciones = VBox( [HBox([empty, up, empty]), HBox([left, down, right])]) acciones = self.get_actions() control = VBox([direcciones, acciones]) ui = VBox(children=[desplegable, visor, control]) return ui
def label_widget(dataset, indices, done_callback): from IPython.display import display, clear_output from ipywidgets import Text, HTML, Button labels = [] i = 0 transcript = HTML(dataset[indices[0]]['segment']) box = Text(placeholder='y/n') def on_submit(text): nonlocal i label = 1 if text.value == 'y' else 0 labels.append((indices[i], label)) i += 1 transcript.value = dataset[indices[i]]['segment'] box.value = '' box.on_submit(on_submit) finished = False btn_finished = Button(description='Finished') def on_click(b): done_callback(labels) btn_finished.on_click(on_click) display(transcript) display(box) display(btn_finished)
def _log_progress(sequence, every=1, size=None, name='Position', timeit=True): from ipywidgets import IntProgress, HTML, VBox, HBox from IPython.display import display progress_logger = logging.getLogger(__name__ + ".progress") is_iterator = False if size is None: try: size = len(sequence) except TypeError: is_iterator = True timeit = False if size is not None: if every is None: if size <= 200: every = 1 else: every = int(size / 200) # every 0.5% else: assert every is not None, 'sequence is iterator, set every' if is_iterator: progress = IntProgress(min=0, max=1, value=1) progress.bar_style = 'info' else: progress = IntProgress(min=0, max=size, value=0) position_label = HTML() time_label = HTML() label = HBox(children=[position_label, time_label]) box = VBox(children=[label, progress]) display(box) index = 0 if timeit: t_remaining = "?" t_start = timer() try: for index, record in enumerate(sequence, 1): if index == 1 or index % every == 0: if is_iterator: position_label.value = u'{}: {} / ?'.format(name, index) else: progress.value = index position_label.value = u'{}: {} / {}'.format(name, index, size) if timeit: time_label.value = u' | Remaining: {}'.format(_format_remaining(t_remaining)) progress_logger.info(position_label.value + time_label.value) yield record if timeit: t_remaining = (size - index - 1) * (timer() - t_start) / (index) except: progress.bar_style = 'danger' raise else: progress.bar_style = 'success' progress.value = index position_label.value = "{}: {}".format(name, str(index or '?'))
def log_progress(sequence, every=None, size=None, name='Items'): """From https://github.com/kuk/log-progress""" is_iterator = False if size is None: try: size = len(sequence) except TypeError: is_iterator = True if size is not None: if every is None: if size <= 200: every = 1 else: every = int(size / 200) # every 0.5% else: assert every is not None, 'sequence is iterator, set every' if is_iterator: progress = IntProgress(min=0, max=1, value=1) progress.bar_style = 'info' else: progress = IntProgress(min=0, max=size, value=0) label = HTML() box = VBox(children=[label, progress]) display(box) index = 0 try: for index, record in enumerate(sequence, 1): if index == 1 or index % every == 0: if is_iterator: label.value = '{name}: {index} / ?'.format(name=name, index=index) else: progress.value = index label.value = u'{name}: {index} / {size}'.format( name=name, index=index, size=size) yield record except: progress.bar_style = 'danger' raise else: progress.bar_style = 'success' progress.value = index label.value = "{name}: {index}".format(name=name, index=str(index or '?'))
def log_progress(sequence, every=None, size=None): from ipywidgets import IntProgress, HTML, VBox from IPython.display import display is_iterator = False if size is None: try: size = len(sequence) except TypeError: is_iterator = True if size is not None: if every is None: if size <= 200: every = 1 else: every = int(size / 200) # every 0.5% else: assert every is not None, 'sequence is iterator, set every' if is_iterator: progress = IntProgress(min=0, max=1, value=1) progress.bar_style = 'info' else: progress = IntProgress(min=0, max=size, value=0) label = HTML() box = VBox(children=[label, progress]) display(box) index = 0 try: for index, msg in enumerate(sequence, 1): if index == 1 or index % every == 0: if is_iterator: label.value = 'Running: {index} / ?: {msg}...'.format( index=index, msg=msg) else: progress.value = index label.value = u'Running: {index} / {size}: {msg}...'.format( index=index, size=size, msg=msg) except: progress.bar_style = 'danger' raise else: progress.bar_style = 'success' progress.value = size # label.value = str(index or '?') label.value = 'Done.'
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 delete_selected(self, button=None): ''' function to delete selected assets ''' selected = self.get_selected() # Output widget output = HTML('') def handle_yes(button): self.children = [self.header, output] # pool = pp.ProcessPool(self.POOL_SIZE) if selected: assets = [ass for ass in selected.keys()] ''' OLD for asset, ty in selected.items(): recrusive_delete_asset_to_widget(asset, output) args = [] for asset, ty in selected.items(): args.append((asset, output)) # pool.map(recrusive_delete_asset_to_widget, args) # pool.map(test2, args) # pool.close() # pool.join() ''' ''' Pool way (not good) pool = Pool(self.POOL_SIZE) pool.map(batch.recrusive_delete_asset, assets) # TODO: cant map recrusive_delete_asset_to_widget because the passed widget is not pickable pool.close() pool.join() ''' for assetid in assets: thread = Thread(target=batch.recrusive_delete_asset, args=(assetid, )) thread.start() # when deleting end, reload self.reload() def handle_no(button): self.reload() def handle_cancel(button): self.reload() assets_str = [ '{} ({})'.format(ass, ty) for ass, ty in selected.items() ] assets_str = '</br>'.join(assets_str) confirm = ConfirmationWidget( '<h2>Delete {} assets</h2>'.format(len(selected.keys())), 'The following assets are going to be deleted: </br> {} </br> Are you sure?' .format(assets_str), handle_yes=handle_yes, handle_no=handle_no, handle_cancel=handle_cancel) self.children = [self.header, confirm, output]
def _make_table(self): # Global styles styles = [ dict(selector="tr", props=[ ("height", self.row_height), ("vertical-align", "middle"), ("text-align", "center"), ("width", self.column_width), ]), dict(selector="td", props=[ ("padding-bottom", self.table_cell_paddings), ("padding-top", self.table_cell_paddings), ("vertical-align", "middle"), ("text-align", "center"), ("width", self.column_width), ]), dict(selector="th", props=[ ("padding-bottom", self.table_cell_paddings), ("padding-top", self.table_cell_paddings), ("vertical-align", "middle"), ("text-align", "center"), ("width", self.column_width), ]), ] # Make styler for DataFrame styler = self.df.style.set_table_attributes('class="table"') \ .set_table_styles(styles) \ .apply(self.selection_colorize, axis=None) # Render HTML table from DataFrame html = styler.render() # Build visual components v_row_buttons = VBox( [Label(description='', layout=dict(height=self.row_height, width=self.row_button_width, padding=self.button_padding)), *self.row_button_list], ) v_bottom = HBox([ v_row_buttons, HTML(html) ]) v_col_buttons = HBox( [Label(description='', layout=dict(height=self.row_height, width=self.row_button_width, padding=self.button_padding)), Label(description='', layout=dict(height=self.row_height, width=self.column_width, padding=self.button_padding)), *self.column_button_list], ) # Make dashboard self._table = VBox( (v_col_buttons, v_bottom,), layout=dict(width="{}pt".format(self.table_width)) )
def show_plot(filename=None): outdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'output') if filename is None: _, tempfile_path = tempfile.mkstemp(dir=outdir, suffix='.pdf') else: tempfile_path = os.path.join(outdir, filename) pyplot.savefig(tempfile_path, type='pdf', bbox_inches='tight') output = Output() with output: pyplot.show() link = HTML() filename = str( os.path.join('../utils/output', os.path.basename(tempfile_path))) link.value = FileLink(filename)._format_path() display(VBox([output, link]))
def __init__(self, persent=0.0, description=None): self.description = description self.persent = persent # Init self.prefix = HTML() self.bar = FloatProgress() self.update() super().__init__(children=[self.prefix, self.bar])
def get_widget(eeobject): """ Create a widget with the eeobject information """ info = dispatch(eeobject) # DISPATCH if isinstance(info, (dict, )): return ipytools.create_accordion(info) else: return HTML(str(info) + '<br/>')
def __init__(self, width=230, height=300): self.html = HTML( value="", layout=Layout(width=("%dpx" % width), height=("%dpx" % height), border="solid 1px #ddd", overflow="scroll")) self.width = width self.height = height self.number = 0 self.chunks = []
def get_html_text(text, color): html = HTML( """<style> body { background-color:""" + color + """ } </style>""" + text ) html = HTML("<b><font size=4 color='" + color + "'>" + text + "</font></b>") return html
def __init__(self, keys, scheduler=None, interval="100ms", complete=False, loop=None): super(ProgressWidget, self).__init__(keys, scheduler, interval, complete) from ipywidgets import FloatProgress, HBox, VBox, HTML self.elapsed_time = HTML("") self.bar = FloatProgress(min=0, max=1, description="") self.bar_text = HTML("") self.bar_widget = HBox([self.bar_text, self.bar]) self.widget = VBox([self.elapsed_time, self.bar_widget])
def __init__(self, signal_components: dict, md_type="", parent=None): self.type = md_type self.parent = parent if self.parent is not None: try: self.parent.set_title_from_widget(self) except: print("Unable to set the Tab title.") # Retain only the signals (at this stage a single component should be present) self.signals = list(signal_components.keys()) self.wsm_signals = SelectMultiple(options=["", *self.signals], value=[""], description="Signals:", placeholder="Signals", disabled=False) # Components self.components = signal_components # start date wdp_start_date = DatePicker(description='Start date:', disabled=False) # stop date wdp_stop_date = DatePicker(description='Stop date:', disabled=False) self.whb_dates = HBox([wdp_start_date, wdp_stop_date]) # Max number of markers to detect self.wit_max_num = BoundedIntText(value=0, min=0, step=1, description="Max Number of Markers:") # Initialize the super().__init__([ HTML(value=f"<B>Processor type: {self.type}</B>"), self.wsm_signals, self.whb_dates, HTML(value="<B>Options :</B>") ], layout=Layout(border='1px solid black')) self.options = []
def __init__(self, entries, log_report='LogReport'): self._entries = entries self._log_report = log_report self._default_row = dict((e, None) for e in entries) self._widget = HTML() self.count = 0 self.update([])
def area(): """ Provide map and options to choose area of interest """ center = [65.73, -50.71] zoom = 4 m = Map(center=center, zoom=zoom) global dc, start, end, file, lon_l, lat_l, lon_r, lat_r # Pick date start = widgets.DatePicker(disabled=False) end = widgets.DatePicker(disabled=False) # Select from map dc = DrawControl(rectangle={'shapeOptions': { 'color': '#0000FF' }}, polyline={}, polygon={}, circlemarker={}) m.add_control(dc) # Shapefile file = widgets.FileUpload(accept='.shp', multiple=False) # Bounding box lon_l = widgets.FloatText(description="lon") lat_l = widgets.FloatText(description="lat") lon_r = widgets.FloatText(description="lon") lat_r = widgets.FloatText(description="lat") return (AppLayout(header=VBox([ HTML("<h1>Select area (time and space)</h1>"), HBox([Label("Start Date:"), start, Label("End Date:"), end]) ]), center=m, right_sidebar=VBox([ HTML("<h3>or upload shapefile<h3>"), file, HTML("<h3> <h3>"), HTML("<h3>or bounding box<h3>"), Label("Bottom-left corner"), lon_l, lat_l, Label("Upper-right corner"), lon_r, lat_r ])))
def send_clicked(self, btn: Button) -> None: "Callback to be called when the Send button is clicked." btn.button_style = 'info' btn.disabled = True self.logger.logger.info('clicked') url = self.url_txt.value method = self.method_ddn.value headers_text = self.req_pane.get_child_named('Headers').value headers = json.loads(headers_text) if headers_text.strip() else {} data_text = self.req_pane.get_child_named('Data').value data = json.loads(data_text) if data_text.strip() else {} args = [url, method] kwargs = dict(headers=headers, cassette_path=self.cassette_path, logger=self.logger) if data: kwargs['json'] = data self.logger.logger.info('vcr request {} {}'.format(args, kwargs)) if 1: timeout_execute_request = timeout_decorator.timeout( self.timeout)(self.execute_request) try: self.logger.logger.info('callign timeout_execute_request') self.resp, is_cached = timeout_execute_request(*args, **kwargs) self.logger.logger.info('result request {}'.format(self.resp)) except timeout_decorator.TimeoutError: self.logger.logger.info('timed out') self.resp_htm = HBox([ HTML('Response'), HTML('Status: Timed out after {:.3f} secs.'.format( self.timeout)) ], layout=Layout(width='100%', justify_content='space-between')) raise else: self.resp, is_cached = self.execute_request(*args, **kwargs) self.logger.logger.info(self.resp.content) if self.post_process_resp: self.post_process_resp(self.resp) btn.button_style = 'primary' self.show_response(self.resp, is_cached) btn.disabled = False
def __init__(self, gen, total=None, hide_graph=False, order=None): super().__init__(gen, NBProgressBar, total) self.report = [] self.text = HTML() self.vbox = VBox([self.first_bar.box, self.text]) if order is None: order = ['pb1', 'text', 'pb2', 'graph'] self.inner_dict = {'pb1':self.first_bar.box, 'text':self.text} self.hide_graph,self.order = hide_graph,order
def initialize(self, options): """ Summary: Initialize the processor using a dictionary, which needs to have the same format has that produced by the dump function. Arguments: options - dictionary with the options to initialize the processor Returns: Nothing. """ # Add the signal if "signals" in options: self.wsm_signals.value = options["signals"] else: self.wsm_signals.value = tuple([""]) # Add the components if "components" in options: self.wsm_components.value = list(set(options["components"]) & \ set(self.wsm_components.options)) else: self.wsm_components.value = tuple([""]) # Now add specific options, if present for wd in self.options: key = wd.description # Eventually remove the last colon if key[-1] == ":": key = key[:-1] if key in options: if isinstance(wd, Text) and isinstance(options[key], list): wd.value = ", ".join([str(a) for a in options[key]]) else: wd.value = options[key] # Set the children of the widget self.children = [ HTML(value=f"<B>Processor type: {self.type}</B>"), HBox([self.wsm_signals, self.wsm_components]), HTML(value="<B>Options :</B>"), *self.options ]
def __init__(self, path): self.db_view = SimpleDatabaseView() toolbar = ToolBar(['eye-slash', 'list', 'image'], exclusive=True, current=0) self.toolbar = toolbar self.hierarchy_bar = HierarchyBar(path, layout=Layout(width='70%')) self.length_label = RichLabel('[0]') self.header = HBox([ toolbar, HTML(layout=Layout(width='25px')), self.hierarchy_bar, HTML(layout=Layout(width='40px')), self.length_label ], layout=Layout(align_content='flex-end')) self.offset_edit = BoundedIntText(description='offset: ', value=self.db_view.offset, min=0, layout=Layout(width='150px')) self.limit_edit = BoundedIntText(description='limit: ', value=self.db_view.limit, min=0, max=self.db_view.limit, layout=Layout(width='150px')) self.progress = TinyLoading(layout=Layout(width='150px')) self.foot = HBox( [self.offset_edit, self.limit_edit, HSpace(), self.progress], layout=Layout(align_items='center')) jsdlink((self.offset_edit, 'value'), (self.db_view, 'offset')) jsdlink((self.limit_edit, 'value'), (self.db_view, 'limit')) jsdlink((self.db_view, 'cache_progress'), (self.progress, 'value')) def toolbar_handling(button_id): self.db_view.visible = button_id != 0 self.foot.layout.display = 'none' if button_id == 0 else 'flex' self.toolbar.on_button_clicked(toolbar_handling) toolbar_handling(0) super(DatabaseView, self).__init__([self.header, VSpace(), self.db_view, self.foot])
def log_progress(sequence, name, every=None, size=None, who="reads"): from ipywidgets import IntProgress, HTML, VBox from IPython.display import display is_iterator = False if size is None: try: size = len(sequence) except TypeError: is_iterator = True if size is not None: if every is None: if size <= 200: every = 1 else: every = size / 200 # every 0.5% else: assert every is not None, "sequence is iterator, set every" if is_iterator: progress = IntProgress(min=0, max=1, value=1) progress.bar_style = "info" else: progress = IntProgress(min=0, max=size, value=0) label = HTML() box = VBox(children=[label, progress]) display(box) index = 0 try: for index, record in enumerate(sequence, 1): if index == 1 or index % every == 0: if is_iterator: label.value = name + ": " + "{index} {who} / ?".format(index=index, who=who) else: progress.value = index label.value = u"{name}: {index} / {size}".format(name=name, index=index, size=size) yield record except: progress.bar_style = "danger" raise else: progress.bar_style = "success" progress.value = index label.value = str(name + ": " + str(index) + " " + str(who) or "?")
def __init__(self, *args, **kwargs): self.status_label = Label(value="Status: unknown", layout=Layout(margin="18px")) self.debug = HTML(layout={ "width": "590px", "height": "800px", "border": "none" }) super().__init__(*args, **kwargs)
def create_download_link(df, title="Download CSV file", filename="selected_mtg.csv"): csv = df.to_csv() b64 = base64.b64encode(csv.encode()) payload = b64.decode() html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>' html = html.format(payload=payload, title=title, filename=filename) return HTML(html)
def setup_notebook_container(self): """Display the display widgets if the Swarm is running in an IPython kernel.""" if running_in_ipython() and self._use_notebook_widget: from ipywidgets import HTML from IPython.core.display import display, HTML as cell_html # Set font weight of tqdm progressbar display(cell_html("<style> .widget-label {font-weight: bold !important;} </style>")) self._notebook_container = HTML()
def add_legend(self): items = ['<ul style="list-style-type: none">'] for label in self.labels: items.append( f'<li style="background: {label[1]}; display: inline; padding: 5px"><b>{label[0]}</b></li>' ) items.append("</ul>") html_str = ''.join(items) self.legend = HTML(value=html_str)
def show_func(self, idx, row) -> None: self.out.clear_output() with self.out: display( VBox([ self.get_progress_bar(idx), HTML(self.show_callback(idx, row)), self.button_box(idx, row[self.options_col]) ]))
def display_stats(player: S) -> Widget: return VBox([ HTML(f'<h3>Forward Looking</h3>'), player .pipe(get_player_fwd_stats, ctx) .pipe(display_df), HTML(f'<h3>Recent {ctx.player_fixtures_look_back} Fixtures</h3>'), player .pipe(DF).T[['Total Points Recent Fixtures', 'Fixtures Played Recent Fixtures', 'Avg Total Points Recent Fixtures', 'Avg ICT Index Recent Fixtures', 'Avg Influence Recent Fixtures', 'Avg Creativity Recent Fixtures', 'Avg Threat Recent Fixtures']] .pipe(ctx.dd.format) .pipe(lambda df: df.rename(columns={x: x.replace(' Recent Fixtures', '') for x in df.columns})) .pipe(display_df), HTML(f'<h3>From Start of Season</h3>'), player .pipe(DF).T[['Total Points', 'Minutes Played', 'Minutes Percent', 'ICT Index', 'Influence', 'Creativity', 'Threat']] .pipe(ctx.dd.display, footer=False, descriptions=False, index=False)])
def show(parameters, name="name"): html = "<table class='table' style='margin-top: 10px'>" html += "<tr><th scope='col' style='width: 20%'>{}</th><th scope='col' style='width: 80%'>Value</th></tr>\n".format( name.capitalize()) for name in parameters: html += "<tr><td>{}</td><td>{}</td></tr>\n".format( name, parameters[name]) html += "</table>" display(HTML(html))
def make_widget(self, all): from ipywidgets import FloatProgress, HBox, VBox, HTML import cgi self.elapsed_time = HTML('') self.bars = {key: FloatProgress(min=0, max=1, description='', height = '10px') for key in all} self.bar_texts = {key: HTML('', width = "140px") for key in all} self.bar_labels = {key: HTML('<div style=\"padding: 0px 10px 0px 10px; text-align:left; word-wrap: break-word;\">' + cgi.escape(key) + '</div>') for key in all} def key(kv): """ Order keys by most numerous, then by string name """ return kv[::-1] key_order = [k for k, v in sorted(all.items(), key=key, reverse=True)] self.bar_widgets = VBox([ HBox([ self.bar_texts[key], self.bars[key], self.bar_labels[key] ]) for key in key_order ]) self.widget.children = (self.elapsed_time, self.bar_widgets)
def get_progress_bar(self, idx) -> HBox: """ get a progress bar based on the row index """ i = self.df.index.get_loc(idx) return HBox([ IntProgress(value=i, min=0, max=len(self.df)), HTML(f"{i+1}/{len(self.df)}") ])
def run(self, duration, obs=None): """Run the simulation. Parameters ---------- duration : Real a duration for running a simulation. A simulation is expected to be stopped at t() + duration. obs : list of Obeservers, optional observers """ from ecell4_base.core import TimeoutObserver timeout = TimeoutObserver(self.__timeout) if obs is None: obs = (timeout, ) elif isinstance(obs, collections.Iterable): obs = tuple(obs) + (timeout, ) else: obs = (obs, timeout) from ipywidgets import FloatProgress, HBox, HTML from IPython.display import display from time import sleep fp = FloatProgress(min=0, max=100) ptext = HTML() display(HBox(children=[fp, ptext])) tstart = self.__sim.t() upto = tstart + duration while self.__sim.t() < upto: self.__sim.run(upto - self.__sim.t(), obs) value = (self.__sim.t() - tstart) / duration fp.value = value * 100 ptext.value = self.get_text(value, timeout.accumulation()) sleep(self.__wait) fp.value = 100 ptext.value = self.get_text(1, timeout.accumulation())
def __init__(self, model, opt, maxiters, verbose=False, current_iteration=0, ipython_notebook=True, clear_after_finish=False): self.verbose = verbose if self.verbose: self.model = model self.iteration = current_iteration self.p_iter = self.iteration self.maxiters = maxiters self.len_maxiters = len(str(maxiters)) self.opt_name = opt.opt_name self.model.add_observer(self, self.print_status) self.status = 'running' self.clear = clear_after_finish self.update() try: from IPython.display import display from ipywidgets import IntProgress, HTML, Box, VBox, HBox, FlexBox self.text = HTML(width='100%') self.progress = IntProgress(min=0, max=maxiters) #self.progresstext = Text(width='100%', disabled=True, value='0/{}'.format(maxiters)) self.model_show = HTML() self.ipython_notebook = ipython_notebook except: # Not in Ipython notebook self.ipython_notebook = False if self.ipython_notebook: left_col = VBox(children=[self.progress, self.text], padding=2, width='40%') right_col = Box(children=[self.model_show], padding=2, width='60%') self.hor_align = FlexBox(children = [left_col, right_col], width='100%', orientation='horizontal') display(self.hor_align) try: self.text.set_css('width', '100%') left_col.set_css({ 'padding': '2px', 'width': "100%", }) right_col.set_css({ 'padding': '2px', }) self.hor_align.set_css({ 'width': "100%", }) self.hor_align.remove_class('vbox') self.hor_align.add_class('hbox') left_col.add_class("box-flex1") right_col.add_class('box-flex0') except: pass #self.text.add_class('box-flex2') #self.progress.add_class('box-flex1') else: self.exps = exponents(self.fnow, self.current_gradient) print('Running {} Code:'.format(self.opt_name)) print(' {3:7s} {0:{mi}s} {1:11s} {2:11s}'.format("i", "f", "|g|", "runtime", mi=self.len_maxiters))
class VerboseOptimization(object): def __init__(self, model, opt, maxiters, verbose=False, current_iteration=0, ipython_notebook=True, clear_after_finish=False): self.verbose = verbose if self.verbose: self.model = model self.iteration = current_iteration self.p_iter = self.iteration self.maxiters = maxiters self.len_maxiters = len(str(maxiters)) self.opt_name = opt.opt_name self.model.add_observer(self, self.print_status) self.status = 'running' self.clear = clear_after_finish self.update() try: from IPython.display import display from ipywidgets import IntProgress, HTML, Box, VBox, HBox, FlexBox self.text = HTML(width='100%') self.progress = IntProgress(min=0, max=maxiters) #self.progresstext = Text(width='100%', disabled=True, value='0/{}'.format(maxiters)) self.model_show = HTML() self.ipython_notebook = ipython_notebook except: # Not in Ipython notebook self.ipython_notebook = False if self.ipython_notebook: left_col = VBox(children=[self.progress, self.text], padding=2, width='40%') right_col = Box(children=[self.model_show], padding=2, width='60%') self.hor_align = FlexBox(children = [left_col, right_col], width='100%', orientation='horizontal') display(self.hor_align) try: self.text.set_css('width', '100%') left_col.set_css({ 'padding': '2px', 'width': "100%", }) right_col.set_css({ 'padding': '2px', }) self.hor_align.set_css({ 'width': "100%", }) self.hor_align.remove_class('vbox') self.hor_align.add_class('hbox') left_col.add_class("box-flex1") right_col.add_class('box-flex0') except: pass #self.text.add_class('box-flex2') #self.progress.add_class('box-flex1') else: self.exps = exponents(self.fnow, self.current_gradient) print('Running {} Code:'.format(self.opt_name)) print(' {3:7s} {0:{mi}s} {1:11s} {2:11s}'.format("i", "f", "|g|", "runtime", mi=self.len_maxiters)) def __enter__(self): self.start = time.time() self._time = self.start return self def print_out(self, seconds): if seconds<60: ms = (seconds%1)*100 self.timestring = "{s:0>2d}s{ms:0>2d}".format(s=int(seconds), ms=int(ms)) else: m, s = divmod(seconds, 60) if m>59: h, m = divmod(m, 60) if h>23: d, h = divmod(h, 24) self.timestring = '{d:0>2d}d{h:0>2d}h{m:0>2d}'.format(m=int(m), h=int(h), d=int(d)) else: self.timestring = '{h:0>2d}h{m:0>2d}m{s:0>2d}'.format(m=int(m), s=int(s), h=int(h)) else: ms = (seconds%1)*100 self.timestring = '{m:0>2d}m{s:0>2d}s{ms:0>2d}'.format(m=int(m), s=int(s), ms=int(ms)) if self.ipython_notebook: names_vals = [['optimizer', "{:s}".format(self.opt_name)], ['runtime', "{:>s}".format(self.timestring)], ['evaluation', "{:>0{l}}".format(self.iteration, l=self.len_maxiters)], ['objective', "{: > 12.3E}".format(self.fnow)], ['||gradient||', "{: >+12.3E}".format(float(self.current_gradient))], ['status', "{:s}".format(self.status)], ] #message = "Lik:{:5.3E} Grad:{:5.3E} Lik:{:5.3E} Len:{!s}".format(float(m.log_likelihood()), np.einsum('i,i->', grads, grads), float(m.likelihood.variance), " ".join(["{:3.2E}".format(l) for l in m.kern.lengthscale.values])) html_begin = """<style type="text/css"> .tg-opt {font-family:"Courier New", Courier, monospace !important;padding:2px 3px;word-break:normal;border-collapse:collapse;border-spacing:0;border-color:#DCDCDC;margin:0px auto;width:100%;} .tg-opt td{font-family:"Courier New", Courier, monospace !important;font-weight:bold;color:#444;background-color:#F7FDFA;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;} .tg-opt th{font-family:"Courier New", Courier, monospace !important;font-weight:normal;color:#fff;background-color:#26ADE4;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;} .tg-opt .tg-left{font-family:"Courier New", Courier, monospace !important;font-weight:normal;text-align:left;} .tg-opt .tg-right{font-family:"Courier New", Courier, monospace !important;font-weight:normal;text-align:right;} </style> <table class="tg-opt">""" html_end = "</table>" html_body = "" for name, val in names_vals: html_body += "<tr>" html_body += "<td class='tg-left'>{}</td>".format(name) html_body += "<td class='tg-right'>{}</td>".format(val) html_body += "</tr>" self.text.value = html_begin + html_body + html_end self.progress.value = (self.iteration+1) #self.progresstext.value = '0/{}'.format((self.iteration+1)) self.model_show.value = self.model._repr_html_() else: n_exps = exponents(self.fnow, self.current_gradient) if self.iteration - self.p_iter >= 20 * np.random.rand(): a = self.iteration >= self.p_iter * 2.78 b = np.any(n_exps < self.exps) if a or b: self.p_iter = self.iteration print('') if b: self.exps = n_exps print('\r', end=' ') print('{3:} {0:>0{mi}g} {1:> 12e} {2:> 12e}'.format(self.iteration, float(self.fnow), float(self.current_gradient), "{:>8s}".format(self.timestring), mi=self.len_maxiters), end=' ') # print 'Iteration:', iteration, ' Objective:', fnow, ' Scale:', beta, '\r', sys.stdout.flush() def print_status(self, me, which=None): self.update() t = time.time() seconds = t-self.start #sys.stdout.write(" "*len(self.message)) if t-self._time > .3 or seconds < .3: self.print_out(seconds) self._time = t self.iteration += 1 def update(self): self.fnow = self.model.objective_function() if self.model.obj_grads is not None: grad = self.model.obj_grads self.current_gradient = np.dot(grad, grad) else: self.current_gradient = np.nan def finish(self, opt): self.status = opt.status if self.verbose and self.ipython_notebook: if 'conv' in self.status.lower(): self.progress.bar_style = 'success' elif self.iteration >= self.maxiters: self.progress.bar_style = 'warning' else: self.progress.bar_style = 'danger' def __exit__(self, type, value, traceback): if self.verbose: self.stop = time.time() self.model.remove_observer(self) self.print_out(self.stop - self.start) if not self.ipython_notebook: print() print('Runtime: {}'.format("{:>9s}".format(self.timestring))) print('Optimization status: {0}'.format(self.status)) print() elif self.clear: self.hor_align.close()
def animator(progbar, count, tot_string=False, linenum=False, terminal=False, init=False, length=False, **kwargs ): """ Animates progress bar in unique position in terminal Multiple progress bars not supported in jupyter yet. """ # if ipython? welcome_message = kwargs.pop('welcome_message', '') if welcome_message: welcome_message = welcome_message.replace('Interrogating corpus ... \n', '') welcome_message = welcome_message.replace('Concordancing corpus ... \n', '') welcome_message = welcome_message.replace('\n', '<br>').replace(' ' * 17, ' ' * 17) else: welcome_message = '' if init: from traitlets import TraitError try: from ipywidgets import IntProgress, HTML, VBox from IPython.display import display progress = IntProgress(min=0, max=length, value=1) using_notebook = True progress.bar_style = 'info' label = HTML() label.font_family = 'monospace' gblabel = HTML() gblabel.font_family = 'monospace' box = VBox(children=[label, progress, gblabel]) display(box) return box except TraitError: pass except ImportError: pass if not init: try: from ipywidgets.widgets.widget_box import FlexBox if isinstance(progbar, FlexBox): label, progress, goodbye = progbar.children progress.value = count if count == length: progress.bar_style = 'success' else: label.value = '%s\nInterrogating: %s ...' % (welcome_message, tot_string) return except: pass # add startnum start_at = kwargs.get('startnum', 0) if start_at is None: start_at = 0.0 denominator = kwargs.get('denom', 1) if kwargs.get('note'): if count is None: perc_done = 0.0 else: perc_done = (count * 100.0 / float(length)) / float(denominator) kwargs['note'].progvar.set(start_at + perc_done) kwargs['root'].update() return if init: from corpkit.textprogressbar import TextProgressBar return TextProgressBar(length, dirname=tot_string) # this try is for sublime text nosetests, which don't take terminal object try: with terminal.location(0, terminal.height - (linenum + 1)): if tot_string: progbar.animate(count, tot_string) else: progbar.animate(count) # typeerror for nose except: if tot_string: progbar.animate(count, tot_string) else: progbar.animate(count)