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 loading_bar_ipython(loading, data=None): from ipywidgets import IntProgress, HTML, VBox from IPython.display import display progress = IntProgress(min=0, max=100, value=0) progress.bar_style = 'info' label = HTML() box = VBox(children=[label, progress]) display(box) page_cnt = 0 shown_categories = set() while True: if data and 'pages' in data: page_cnt = len(data['pages']) if data and 'categories' in data: new_cats = set(data['categories'].keys()) for new in new_cats.difference(shown_categories): print(f"\rCategory title: {new}") shown_categories = new_cats label.value = f'Pages: {page_cnt} / ?' progress.value = page_cnt % 95 if not loading['status']: label.value = f'Pages: {page_cnt} / {page_cnt}' progress.value = 100 print(f"Scraping Done! {page_cnt} pages read") break time.sleep(0.5)
def log_progress(sequence, every=None, size=None, name='Items'): """Ipywidget for displaying a progress bar. Developed by Elisa Heim Args: sequence ([type]): [description] every ([type], optional): [description]. Defaults to None. size ([type], optional): [description]. Defaults to None. name (str, optional): [description]. Defaults to 'Items'. Yields: Ipywidget: a progress bar showing current / max number of models. """ 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: 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, desc='Items', total=None, miniters=None): """ Make and display a progress bar. Parameters ---------- sequence : list. Represents a elements sequence. desc : String, optional, default 'Items'. Represents the description of the operation. total : int, optional, default None. Represents the total/number elements in sequence. miniters : int, optional, default None. Represents the steps in which the bar will be updated """ is_iterator = False if total is None: try: total = len(sequence) except TypeError: is_iterator = True if total is not None: if miniters is None: if total <= 200: miniters = 1 else: miniters = int(total / 200) else: if miniters is None: miniters = 1 if is_iterator: progress = IntProgress(min=0, max=1, value=1) progress.bar_style = 'info' else: progress = IntProgress(min=0, max=total, 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 % miniters == 0: if is_iterator: label.value = '%s: %s / ?' % (desc, index) else: progress.value = index label.value = u'%s: %s / %s' % (desc, index, total) yield record except Exception: progress.bar_style = 'danger' raise else: progress.bar_style = 'success' progress.value = index label.value = '%s: %s' % (desc, str(index or '?'))
def log_progress(sequence, every=None, size=None, desc='Items'): """ Make and display a progress bar. Parameters ---------- sequence : list. Represents a elements sequence. every : int, optional, default None. Represents the steps in which the bar will be updated size : int, optional, default None. Represents the size/number elements in sequence. desc : String, optional, default 'Items'. Represents the description of the operation. """ 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) else: if every is None: raise AssertionError('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 = '%s: %s / ?' % (desc, index) else: progress.value = index label.value = u'%s: %s / %s' % (desc, index, size) yield record except Exception: progress.bar_style = 'danger' raise else: progress.bar_style = 'success' progress.value = index label.value = '%s: %s' % (desc, str(index or '?'))
def progressBar(sequence, width=None, every=None, size=None, name='Items', text_color='white'): 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=0, width=width) progress.bar_style = 'info' else: progress = IntProgress(min=0, max=size, value=0, width=width) label = HTML() box = VBox(children=[label, progress]) display(box) index = 0 try: for index, record in enumerate(sequence, 0): if index == 0 or index % every == 0: if is_iterator: label.value = '<font color="{color}">{name}: {index} / ?</font>'.format( color=text_color, name=name, index=index) else: progress.value = index label.value = u'<font color="{color}">{name}: {index} / {size}</font>'.format( color=text_color, name=name, index=index, size=size) yield record except: progress.bar_style = 'danger' raise else: progress.bar_style = 'success' progress.value = index + 1 label.value = u'<font color="{color}">{name}: {index} / {size}</font>'.format( color=text_color, name=name, index=str(index + 1 or '?'), size=size)
def log_progress(sequence, every=None, size=None, name='Items'): """https://github.com/alexanderkuk/log-progress Arguments: sequence {[type]} -- [description] Keyword Arguments: every {[type]} -- [description] (default: {None}) size {[type]} -- [description] (default: {None}) name {str} -- [description] (default: {'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: 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'): """ Displays a progress indicator Example def dothing(v): time.sleep(1) s = [1, 2, 3,4] for r in log_progress(s, every=1): dothing(r) From 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 = 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=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): 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(sequence, every=None, size=None, name='Items'): from ipywidgets import IntProgress, HTML, VBox from IPython.display import display import time import datetime 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) start = time.time() 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 elapsed = time.time() - start avg_iter = elapsed / index remaining = int(avg_iter * (size - index)) label.value = u'{name}: {index} / {size} [ETA: {seconds}]'.format( name=name, index=index, size=size, seconds=datetime.timedelta(seconds=remaining)) yield record except: progress.bar_style = 'danger' raise else: progress.bar_style = 'success' progress.value = index
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') -> object: """ Shows a progress bar with labels in a Jupyter notebook (see https://github.com/kuk/log-progress). Args: sequence: The list to iterate over. Each element in the list can cause a progress bar update but the frequency depends on the every parameter. every: The frequency of the progress bar update. E.g. update progress bar after every two items. size: The number of items in the list. name: The description to show. Returns: """ 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 = f'{name}: {record} / ?' else: progress.value = index label.value = f'{name}: {record} / {sequence[-1]}' yield record except: progress.bar_style = 'danger' raise else: progress.bar_style = 'success' progress.value = index label.value = f'{name}: {record} / {sequence[-1]}'
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): """ Returns a log progress bar for computations """ 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 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 log_progress(sequence, every=None, size=None, name='Items'): 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'): """ Widget based progress bar for Jupyter, source: https://github.com/kuk/log-progress (requires the jupyter lab extension to be installed: $jupyter labextension install @jupyter-widgets/jupyterlab-manager) """ 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 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 labeled_progress(it, n, labels, fillvalue="...", final=""): "Iterator and set of labels. Reports progress with bar and label." detail = HTML(value='<i>initializing</i>', disabled=True) f = FloatProgress(min=0, max=n) display(HBox([f, detail])) for x, label in zip_longest(it, labels, fillvalue=fillvalue): detail.value = label yield x f.value += 1 f.description = f'{int(100*f.value/n)}%' detail.value = final
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 build_map(self, click_handler): mean_lat = self.x_data[self.lat_key].values.mean() mean_lng = self.x_data[self.lon_key].values.mean() # create the map m = Map(center=(mean_lat, mean_lng), zoom=4, basemap=basemaps.Stamen.Terrain) m.layout.height = '600px' # show trace markers = [] for k in self.marker_info: lat, lon = k message = HTML() message.description = "Station ID" message.value = str(self.marker_info[k]) message.placeholder = "" marker = Marker(location=(lat, lon)) marker.on_click(click_handler) marker.popup = message markers.append(marker) marker_cluster = MarkerCluster( markers=markers ) # not sure whether we could register once instead of each marker: # marker_cluster.on_click(click_handler) m.add_layer(marker_cluster) # m.add_control(FullScreenControl()) return m
def drawPOIS(POIS, zoom=12): centerLat, centerLog = 0, 0 # taking the average of all latitude and longitude of all the POIs # to get the center of the map for poi in POIS: centerLat += poi.coordinates[0] centerLog += poi.coordinates[1] centerLat /= len(POIS) centerLog /= len(POIS) center = (centerLog, centerLat) m = Map(center=center, zoom=zoom, close_popup_on_click=False) # creating the popup messages on the markers # with the name of the POI for poi in POIS: name = poi.address.split(",")[0] marker = Marker(location=poi.coordinates[::-1]) text = HTML() text.value = f"{name}" marker.popup = text m.add_layer(marker) return m
def show(self, data, header=None, footer=None): # We accepting list and dict only! if data != None: if type(data) is dict: self.data = [data] elif type(data) is list: self.data = data else: display( HTML("<b>You should pass either list or dictionary!</b>")) return # Draw a border. Need to be moved to properties. html = '<div style="border: 1px solid #DDDDDD">' if header != None: html += f'<div style="width:100%;background-color:#DDDDDD;padding-left:8px;line-height:1.5">{header}</div>' html += self._generateHTML(self.data) html += '</div>' if footer != None: html += f'<div style="width:100%;background-color:#DDDDDD;padding-left:8px;line-height:1.5">{footer}</div>' # Generates and draws an HTML page. page = HTML() page.value = html display(page)
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 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): 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 progress_bar(sequence, every=None, size=None, name='Parameters'): from ipywidgets import IntProgress, HTML, VBox # c&p 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]) IPython.display.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 = f"{name}: {str(index or '?')}"
def log_message(msg): from ipywidgets import HTML, VBox from IPython.display import display label = HTML() box = VBox(children=[label]) display(box) label.value = msg
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 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 make_link(fig, 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) fig.savefig(tempfile_path, type='pdf') link = HTML() filename = str( os.path.join('../utils/output', os.path.basename(tempfile_path))) link.value = FileLink(filename)._format_path() return link
def _generateMarkers(self): popupTemplate = """ <head> <style> table { border: 1px solid black; border-radius: 25px; } </style> </head> <body> <table style="width:100%"> <tr> <th>Key</th> <th>Value</th> </tr> ROWS </table> </body> """ # tableTemplate = """<table style="width:100%"><tr><th>key</th><th>value</th></tr>ROWS</table>""" rowTemplate = """<tr><td>KEY</td><td>VALUE</td></tr>""" markers = [] for _, row in self.df.iterrows(): if isinstance(row[self.lon], numbers.Number) and isinstance( row[self.lat], numbers.Number): markerTemp = ipyleaflet.Marker( location=[row[self.lat], row[self.lon]], draggable=False) # popup information message = HTML() rowList = [] for x, y in row.iteritems(): if x in self.properties: str_x = re.escape(str(x)) str_y = re.escape(str(y)) rowTemp = re.sub('VALUE', str_y, re.sub('KEY', str_x, rowTemplate)) rowTemp = re.sub(r'\\(.)', r'\1', rowTemp) rowList.append(rowTemp) message.value = re.sub( r'\\(.)', r'\1', re.sub('ROWS', ''.join(rowList), popupTemplate)) message.placeholder = '' message.description = '' markerTemp.popup = message # style of marker markerTemp.layout = {'padding': '1px'} markers.append(markerTemp) return markers
def show_stations_on_map(self,list_inc,list_process,list_filters, opt_params): """ Returns an interactive map of source and stations, use only in Jupyter Notebooks. Inputs: | list_inc: list of incidents | list_process: list of processes, one list per incident | list filters: list of filters defined for stations | opt_params: optional parameters (dictionary) Optional parameters: """ records = self._extract_records(list_inc, list_process, list_filters) if not records: LOGGER.warning("There are no records to satisfy the provided" " filters.") return m = Map( basemap=basemap_to_tiles(basemaps.Esri.WorldImagery, "2020-04-08"), center = (Station.pr_source_loc[0],Station.pr_source_loc[1]), zoom = 8, close_popup_on_click=True ) for i in records: if not i[0]: continue lat = i[0].station.lat lon = i[0].station.lon marker = Marker(location=(lat, lon), draggable=False, icon=AwesomeIcon(name="map-pin", marker_color='green', icon_color='darkgreen'), opacity=0.8) m.add_layer(marker) message = HTML() message.value = i[0].station.inc_st_name[list_inc[0]] marker.popup = message m.add_layer(Marker(icon=AwesomeIcon(name="star", marker_color='red', icon_color='darkred'), location=(Station.pr_source_loc[0],Station.pr_source_loc[1]), draggable=False)) return m
def observe_slider_update_label( self, slider: w.IntSlider, label: w.HTML, text: str, decimals: int = 2 ) -> Callable[[Any], None]: is_range: bool = isinstance(slider, (w.IntRangeSlider, w.FloatRangeSlider)) is_float: bool = isinstance(slider, (w.FloatSlider, w.FloatRangeSlider)) number_fmt: str = f"{{:.{decimals}f}}" if is_float else "{}" value_fmt: str = f"{number_fmt}-{number_fmt}" if is_range else number_fmt def get_label(value) -> str: tag: str = "" if slider.value is None else value_fmt.format(*value) if is_range else value_fmt.format(value) return f"<b>{text}</b> {tag}" def handler(*_): label.value = get_label(slider.value) label.value = get_label(slider.value) slider.observe(handler, names='value')
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 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)