def new_display(self, attached=True, wid=None, height_px=600, width_px=580): if wid is not None: self.wid = str(wid) all_d = set() for r in self.get_instances(): for j in r.displays.keys(): all_d.add(j) if self.wid in all_d: print('{} exists. Enter a different id or remove current display'. format(self.wid)) return html_code = "<iframe src='{}/{}' width={} height={}></iframe>".format( self.frame_url, self.wid, width_px, height_px) self.displays[self.wid] = { 'attached': attached, 'obj': ipw.widgets.HTML(value=html_code) } if attached: display(self.displays[self.wid]['obj']) else: self.sc = Sidecar(title='{}'.format(self.wid), layout=ipw.Layout(width='580px', height='600px')) self.displays[self.wid]['obj'].on_displayed(self.handler_displayed) with self.sc: display(self.displays[self.wid]['obj']) return
def display_map(self, map_output, hemisphere=None, extra_layers=True): """ Will render the UI using ipyleaflet and jupyter widgets """ self.map_output = map_output self.dc = DrawControl( circlemarker={}, polyline={}, rectangle={ "shapeOptions": { "fillColor": "#cc00cc", "color": "#cc00cc", "fillOpacity": 0.5 } }) if hemisphere is None: projection = widget_projections[self.projection.value] else: projection = widget_projections[hemisphere] # TODO: maybe there is a way to create the map in the constructor # and just update its properties to see if this is faster. self.map = Map(center=projection['center'], zoom=projection['zoom'], max_zoom=projection['max_zoom'], basemap=projection['base_map'], crs=projection['projection']) self.map.add_control(self.dc) self.map.add_control(self.layers_control) self.map.add_control(self.search_control) for ib_layer in flight_layers[self.projection.value]: self.map.add_layer(ib_layer(self.start_date, self.end_date)) if extra_layers: for layer in custom_layers[self.projection.value]: self.map.add_layer(layer) # if self.dc.last_draw['geometry'] is not None: # self.map.add_layer(GeoJSON(name='selected geometry', data=self.dc.last_draw)) self.map.layout.height = '560px' self.out.clear_output() if map_output == 'vertical': if hasattr(self, 'sc'): self.sc.clear_output() else: self.sc = Sidecar(title='Map Widget') with self.sc: display(self.out) with self.out: display(self.map) for component in self.controls: display(component) else: with self.out: display(self.map) display(self.out)
def display_controls(self, where): if where == 'vertical': if not hasattr(self, 'sc'): self.sc = Sidecar(title='Map Widget') with self.sc: for component in self.controls: display(component) else: for component in self.controls: display(component)
def display_credentials(self, where='horizontal'): """ renders the input controls to get the user's credentials note that theey are not enccrypted here. """ if where == 'vertical': if not hasattr(self, 'sc'): self.sc = Sidecar(title='Map Widget') with self.sc: display(self.username, self.password, self.email, self.credentials_button) else: display(self.username, self.password, self.email, self.credentials_button)
def set_sidecar(title): global SIDECAR try: from sidecar import Sidecar SIDECAR = Sidecar(title=title) except: print("Warning: module sidecar not installed")
def __init__(self, width=400, height=400): self.multicanvas = MultiCanvas(4, width=width, height=height) self.turtleCanvas = self.multicanvas[3] self.sc = Sidecar(title="Turtle Screen") with self.sc: display(self.multicanvas) self.turtles = []
def show(self, viewer, title): if title not in self.sidecars: self.sidecars[title] = Sidecar(title=title, anchor=self.anchor) else: # TODO: find a better way to do this self.sidecars[title].close() self.sidecars[title].open() with self.sidecars[title]: viewer.show()
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 create(self): """Create the sidecar view""" self.sc = Sidecar(title="DBFS-%s" % os.environ["DBJL_CLUSTER"].split("-")[-1]) self.path = "/" self.flist = Select(options=[], rows=40, disabled=False) self.flist.observe(self.on_click, names="value") self.refresh = Button(description="refresh") self.refresh.on_click(self.on_refresh) self.output = Output() self.up = Button(description="up") self.up.on_click(self.on_up) with self.sc: display( VBox([ HBox([self.up, self.refresh]), self.flist, self.output ])) self.update()
def __init__(self): self._sc = Sidecar(title='Variables') get_ipython().user_ns_hidden['widgets'] = widgets get_ipython().user_ns_hidden['NamespaceMagics'] = NamespaceMagics self.closed = False self.namespace = NamespaceMagics() self.namespace.shell = get_ipython().kernel.shell self._box = widgets.Box() self._box.layout.overflow_y = 'scroll' self._table = widgets.HTML(value='Not hooked') self._box.children = [self._table] self._ipython = get_ipython() self._ipython.events.register('post_run_cell', self._fill)
def set_sidecar(title, init=False): global SIDECAR try: from sidecar import Sidecar _has_sidecar = True except: print("Warning: module sidecar not installed") _has_sidecar = False if _has_sidecar: close_sidecar() SIDECAR = Sidecar(title=title) set_defaults(display="sidecar") if init: d = get_or_create_display(init=True)
def show_in_sidecar(self, **kwargs): """ Display the Jdaviz application in a "sidecar", which by default is a tab on the right side of the JupyterLab interface. Additional keywords not listed here are passed into the ``sidecar.Sidecar`` constructor. See `jupyterlab-sidecar <https://github.com/jupyter-widgets/jupyterlab-sidecar>`_ for the most up-to-date options. Parameters ---------- title : str, optional The title of the sidecar tab. Defaults to the name of the application; e.g., "specviz". anchor : str Where the tab should appear, by default on the right. Options are: {sidecar_anchor_values}. Returns ------- sidecar The ``sidecar.Sidecar`` object used to create the tab. Notes ----- If this method is called in the "classic" Jupyter notebook, the app will appear inline, as only lab has a mechanism to have multiple tabs. See Also -------- show_in_new_tab show_inline """ if 'title' not in kwargs: kwargs['title'] = self.app.config scar = Sidecar(**kwargs) with scar: display(self.app) return scar
#%% # Example import requests import json from sidecar import Sidecar url = 'http://127.0.0.1:8080' sidecar = Sidecar(url) artifacts = sidecar.artifacts() print('\nBlock Number: {}'.format(hex(int(artifacts['at']['height'])))) print('\nBlock Hash: {}'.format(artifacts['at']['hash'])) print('\nSpec Version: {}'.format(artifacts['specVersion'])) print('\nTx Version: {}'.format(artifacts['txVersion'])) print('\nGenesis Hash: {}'.format(artifacts['genesisHash']))
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> """ ) )
class __Dbfs(object): """Database browser implementation Args: dbutils (DBUtils): DBUtils object (for fs only) """ def __init__(self, dbutils): self.dbutils = dbutils def create(self): """Create the sidecar view""" self.sc = Sidecar(title="DBFS-%s" % os.environ["DBJL_CLUSTER"].split("-")[-1]) self.path = "/" self.flist = Select(options=[], rows=40, disabled=False) self.flist.observe(self.on_click, names="value") self.refresh = Button(description="refresh") self.refresh.on_click(self.on_refresh) self.output = Output() self.up = Button(description="up") self.up.on_click(self.on_up) with self.sc: display( VBox([ HBox([self.up, self.refresh]), self.flist, self.output ])) self.update() def convertBytes(self, fsize): """Convert bytes to largest unit Args: fsize (int): Size in bytes Returns: tuple: size of largest unit, largest unit """ size = fsize unit = "B" if size > 1024 * 1024 * 1024 * 10: size = int(size / 1024.0 / 1024.0 / 1024.0) unit = "GB" elif size > 1024 * 1024 * 10: size = int(size / 1024.0 / 1024.0) unit = "MB" elif size > 1024 * 10: size = int(size / 1024.0) unit = "KB" return (size, unit) def update(self): """Update the view when an element was selected""" with self.output: print("updating ...") fobjs = self.dbutils.fs.ls(self.path) self.show_path(self.path) dirs = sorted([fobj.name for fobj in fobjs if fobj.isDir()], key=lambda x: x.lower()) files = sorted( [ "%s (%d %s)" % ((fobj.name, ) + self.convertBytes(fobj.size)) for fobj in fobjs if not fobj.isDir() ], key=lambda x: x[0].lower(), ) self.flist.options = [""] + dirs + files def show_path(self, path): """Show path in output widget Args: path (str): Currently selected path """ self.output.clear_output() with self.output: print("dbfs:" + re.sub(r"\s\(.*?\)$", "", path)) def on_refresh(self, b): """Refresh handler Args: b (ipywidgets.Button): clicked button """ self.update() def on_up(self, b): """Up handler Args: b (ipywidgets.Button): clicked button """ new_path = os.path.dirname(self.path.rstrip("/")) if new_path != self.path: self.path = new_path self.update() def on_click(self, change): """Click handler providing db and parent as context Args: db (str): database name parent (object): parent object """ new_path = os.path.join(self.path, change["new"]) if change["old"] is not None: if change["new"][-1] == "/": self.path = new_path self.update() else: self.show_path(new_path) def close(self): """Close view""" self.sc.close()
def sidebar(stuff, title='Sidebar'): sc = Sidecar(title=title) with sc: display(stuff)
class JS9Server(pyjs9.JS9, KeepRefs): def __init__(self, root='http://localhost', path='/jjs9', port_html=8888, port_io=2718, transport='socketio', wid=None): KeepRefs.__init__(self) pyjs9.js9Globals['transport'] = transport if wid is None: self.wid = str(uuid.uuid4().hex) else: self.wid = wid self.node_url = '{}:{}'.format(root, port_io) self.frame_url = '{}:{}{}'.format(root, port_html, path) self.displays = OrderedDict() self.connected = False self.msg = '' self.id = None #super(JS9Server, self).__init__(host=self.node_url, id=wid+'JS9') def connect(self, wid=None, external=False): temp = self.wid if wid is not None: self.wid = wid if external: super(JS9Server, self).__init__(host=self.node_url, id=self.wid + 'JS9') self.connected = True return if self.wid in self.displays: super(JS9Server, self).__init__(host=self.node_url, id=self.wid + 'JS9') self.connected = True else: print('{} display does not exist'.format(self.wid)) self.wid = temp def handler_displayed(self, widget): #self._connect() self.msg = 'connected' def new_display(self, attached=True, wid=None, height_px=600, width_px=580): if wid is not None: self.wid = str(wid) all_d = set() for r in self.get_instances(): for j in r.displays.keys(): all_d.add(j) if self.wid in all_d: print('{} exists. Enter a different id or remove current display'. format(self.wid)) return html_code = "<iframe src='{}/{}' width={} height={}></iframe>".format( self.frame_url, self.wid, width_px, height_px) self.displays[self.wid] = { 'attached': attached, 'obj': ipw.widgets.HTML(value=html_code) } if attached: display(self.displays[self.wid]['obj']) else: self.sc = Sidecar(title='{}'.format(self.wid), layout=ipw.Layout(width='580px', height='600px')) self.displays[self.wid]['obj'].on_displayed(self.handler_displayed) with self.sc: display(self.displays[self.wid]['obj']) return def close_display(self, wid=None): if wid is not None: closeid = wid else: closeid = self.wid temp = self.displays[closeid] if temp['attached']: temp['obj'].close() else: self.sc.close() temp['obj'].close() #self.displays.pop(closeid) del (self.displays[closeid]) self.connected = False return def close_all_displays(self, force=False): tkeys = list(self.displays.keys()) for kid in tkeys: self.close_display(wid=kid) try: self.sc.close_all() except AttributeError: pass if force: for r in self.get_instances(): for jid in r.displays.keys(): temp = r.displays[jid] if temp['attached']: temp['obj'].close() else: r.sc.close() temp['obj'].close() del (r.displays[jid]) r.connected = False return
def lab_display(view): from sidecar import Sidecar with Sidecar(title='nglview'): display(view)
class IceFlowUI: """ UI for the IceFlow API """ def __init__(self): self.out = widgets.Output(layout={'border': '1px solid black'}) self.iceflow = IceflowClient() self.last_orders = None self.current_projection = 'north' self.clear = True self.controls = [] self.credentials = None self.start_date = datetime(1993, 1, 1) self.end_date = datetime.now() slider_dates = [(date.strftime(' %Y-%m-%d '), date) for date in pd.date_range(datetime(1993, 1, 1), datetime.now(), freq='D')] slider_index = (0, len(slider_dates) - 1) self.username = widgets.Text( value='', description='User:'******'Your NASA EarthData username ', disabled=False ) self.password = widgets.Password( value='', placeholder='Enter password', description='Password:'******'', description='Email:', placeholder='Your EarthData e-mail', disabled=False ) self.credentials_button = widgets.Button(description='Set Credentials', ) self.projection = widgets.Dropdown( options=['global', 'south', 'north'], description='Hemisphere:', disabled=False, value='north' ) self.dataset = widgets.SelectMultiple( options=['ATM1B', 'GLAH06', 'ILVIS2'], value=['ATM1B'], rows=4, description='Datasets', disabled=False ) self.itrf = widgets.Dropdown( options=[None, 'ITRF2000', 'ITRF2008', 'ITRF2014'], disabled=False, description='ITRF:', layout={'width': 'max-content', 'display': 'flex', 'description_width': 'initial'} ) self.epoch = widgets.Text( value='', description='Epoch:', placeholder='i.e. 2008.1', disabled=False ) self.is2 = widgets.Dropdown( options=['None', 'ATL03', 'ATL06', 'ATL07', 'ATL08'], description='ICESat 2:', disabled=False, ) self.dates_range = widgets.SelectionRangeSlider( options=slider_dates, index=slider_index, continuous_update=False, description='Date Range', orientation='horizontal', layout={'width': '90%'}) self.granule_count = widgets.Button(description="Get Raw Granule Count", display='flex', flex_flow='column', align_items='stretch', ) self.granule_count.style.button_color = 'lightgreen' self.granule_count.layout.width = 'auto' self.print_parameters = widgets.Button(description="Print Current Parameters", display='flex', flex_flow='column', align_items='stretch', ) self.print_parameters.style.button_color = 'lightgreen' self.print_parameters.layout.width = 'auto' self.post_order = widgets.Button(description="Place Data Order", display='flex', flex_flow='column', align_items='stretch', ) self.post_order.style.button_color = 'lightblue' self.post_order.layout.width = 'auto' self.check_order_status = widgets.Button(description="Order status", display='flex', flex_flow='column', align_items='stretch', ) self.check_order_status.style.button_color = 'lightblue' self.check_order_status.layout.width = 'auto' self.download_button = widgets.Button(description="Download completed orders", display='flex', flex_flow='column', align_items='stretch', ) self.download_button.style.button_color = 'lightblue' self.download_button.layout.width = 'auto' self.selection_buttons = widgets.HBox([self.granule_count, self.print_parameters, self.post_order, self.check_order_status, self.download_button]) self.selection_controls = widgets.VBox([self.projection, self.dataset, self.itrf, self.epoch, self.is2, self.dates_range, self.selection_buttons]) self.controls.append(self.selection_controls) self.layers_control = LayersControl(position='topright') # Map Components place_marker = Marker(icon=AwesomeIcon(name="check", marker_color='green', icon_color='darkgreen')) self.search_control = SearchControl( position="topleft", url='https://nominatim.openstreetmap.org/search?format=json&q={s}', zoom=5, marker=place_marker ) self.dc = DrawControl( circlemarker={}, polyline={}, rectangle={ "shapeOptions": { "fillColor": "#cc00cc", "color": "#cc00cc", "fillOpacity": 0.5 } }) self.file_upload = widgets.FileUpload( accept='.json,.geojson,.shp', multiple=False # True to accept multiple files upload else False ) # Now we are going to link the controls to the relevant events self.dates_range.observe(self.dates_changed, 'value') self.projection.observe(self.hemisphere_change) self.credentials_button.on_click(self.set_credentials) self.granule_count.on_click(self.query_cmr) self.print_parameters.on_click(self.get_parameters) self.post_order.on_click(self.place_data_orders) self.check_order_status.on_click(self.order_statuses) self.download_button.on_click(self.download_orders) def get_parameters(self, change): print(self.build_parameters()) def hemisphere_change(self, change): if change['type'] == 'change' and change['name'] == 'value': self.display_map(self.map_output, hemisphere=self.projection.value) def dates_changed(self, change): if change['type'] == 'change' and change['name'] == 'value': start, end = change['new'] self.start_date = start self.end_date = end # we filter the geopandas datafram to only display flights within the user range self.display_map(self.map_output) def set_credentials(self, event): if (self.username.value != '' and self.password.value != ''): self.credentials = { 'username': self.username.value, 'password': self.password.value, 'email': self.email.value } self.authenticate() else: print('enter your NASA Earth login credentials') self.credentials = None return None def display_credentials(self, where='horizontal'): """ renders the input controls to get the user's credentials note that theey are not enccrypted here. """ if where == 'vertical': if not hasattr(self, 'sc'): self.sc = Sidecar(title='Map Widget') with self.sc: display(self.username, self.password, self.email, self.credentials_button) else: display(self.username, self.password, self.email, self.credentials_button) def display_controls(self, where): if where == 'vertical': if not hasattr(self, 'sc'): self.sc = Sidecar(title='Map Widget') with self.sc: for component in self.controls: display(component) else: for component in self.controls: display(component) def display_map(self, map_output, hemisphere=None, extra_layers=True): """ Will render the UI using ipyleaflet and jupyter widgets """ self.map_output = map_output self.dc = DrawControl( circlemarker={}, polyline={}, rectangle={ "shapeOptions": { "fillColor": "#cc00cc", "color": "#cc00cc", "fillOpacity": 0.5 } }) if hemisphere is None: projection = widget_projections[self.projection.value] else: projection = widget_projections[hemisphere] # TODO: maybe there is a way to create the map in the constructor # and just update its properties to see if this is faster. self.map = Map(center=projection['center'], zoom=projection['zoom'], max_zoom=projection['max_zoom'], basemap=projection['base_map'], crs=projection['projection']) self.map.add_control(self.dc) self.map.add_control(self.layers_control) self.map.add_control(self.search_control) for ib_layer in flight_layers[self.projection.value]: self.map.add_layer(ib_layer(self.start_date, self.end_date)) if extra_layers: for layer in custom_layers[self.projection.value]: self.map.add_layer(layer) # if self.dc.last_draw['geometry'] is not None: # self.map.add_layer(GeoJSON(name='selected geometry', data=self.dc.last_draw)) self.map.layout.height = '560px' self.out.clear_output() if map_output == 'vertical': if hasattr(self, 'sc'): self.sc.clear_output() else: self.sc = Sidecar(title='Map Widget') with self.sc: display(self.out) with self.out: display(self.map) for component in self.controls: display(component) else: with self.out: display(self.map) display(self.out) def bounding_box(self, points): """ returns a bbox array for a given polygon """ x_coordinates, y_coordinates = zip(*points) return [(min(x_coordinates), min(y_coordinates)), (max(x_coordinates), max(y_coordinates))] def build_parameters(self): """ returns the current selection parameters based on the widgets and map state """ self.datasets_iceflow = [] if self.dc.last_draw['geometry'] is None: print('You need to select an area using the bbox or polygon tools') return None coords = [list(coord) for coord in self.bounding_box(self.dc.last_draw['geometry']['coordinates'][0])] bbox = f'{coords[0][0]},{coords[0][1]},{coords[1][0]},{coords[1][1]}' start = self.dates_range.value[0].date().strftime('%Y-%m-%d') end = self.dates_range.value[1].date().strftime('%Y-%m-%d') ITRF = self.itrf.value epoch = self.epoch.value selected_datasets = self.dataset.value for d in selected_datasets: self.datasets_iceflow.append(d) if self.is2.value != 'None': self.datasets_iceflow.append(self.is2.value) params = { 'start': start, 'end': end, 'bbox': bbox, 'datasets': self.datasets_iceflow } if ITRF != 'None' and ITRF is not None: params['itrf'] = ITRF if epoch != 'None' and epoch != '': params['epoch'] = epoch return params def query_cmr(self, event=None, params=None): if params is None: params = self.build_parameters() granules = self.iceflow.query_cmr(params) return granules def place_data_orders(self, event=None, params=None): if params is None: params = self.build_parameters() self.authenticate() if not self.clear: print("There is an active order being processed," + " check the status and wait until that's completed to order again") return None self.last_orders = self.iceflow.place_data_orders(params) self.clear = False return self.last_orders def authenticate(self): if self.credentials is None: print('You need to enter valid EarthData credentials') return None user = self.credentials['username'] password = self.credentials['password'] email = self.credentials['email'] session = self.iceflow.authenticate(user, password, email) return session def order_status(self, order): status = self.iceflow.check_order_status(order) return status def order_statuses(self, envent=None): if self.last_orders is None: print('No active orders') return None self.clear = True for order in self.last_orders: status = self.iceflow.check_order_status(order)['status'] if status == 'INPROGRESS': self.clear = False if order['provider'] == 'icepyx': print(f"Order for {order['dataset']} ICESat 2 data is ready to be downloaded") else: print(f"Order {order['response']['order']['order_id']} for {order['dataset']} is {status}") def download_order(self, order): return self.iceflow.download_order(order) def download_orders(self, event=None, orders=None): if self.last_orders is None: print('No active orders') return None if orders is not None: self.iceflow.download_orders(orders) else: self.iceflow.download_orders(self.last_orders)
#%% # Example import requests import json from sidecar import Sidecar url = 'http://127.0.0.1:8080' # url = 'https://cb-runtime-wk8yx7pds0ag.paritytech.net/block/540' sidecar = Sidecar(url) block = sidecar.block(1184728)