def get_table_renderable(self, name, interactive=False): """ Returns a Rich table renderable """ # Get interface interface = self.tables.get(name) # Return if interface is null if not interface: return # Get field map field_map = interface.field_map # Get column count col_count = len(field_map) # Initialize Rich Table as renderable renderable = Table(title=f"Table: {name} ({col_count} " f"{self.inflector.plural('col', col_count)})") # Check if interactive if interactive: # Set caption renderable.caption = "c\[ommands]: Show available commands" # noqa # Add columns [ renderable.add_column(col_name) for col_name in ("#", "field", "display", "cast", "type") ] # Iterate over field map for i, (field, info) in enumerate(field_map.items()): # Get cast cast = info[_c.CAST] # Add row renderable.add_row( str(i), field, info[interface.DISPLAY], str(cast), str(interface.TYPE_MAP[cast]), ) # Pad the renderable renderable = Padding(renderable, (1, 1)) # Return renderable return renderable
def get_tables_renderable(self, tables, interactive=False): """ Displays a list of tables using a Rich Table """ # Get table count table_count = len(tables) # Initialize Rich Table as renderable renderable = Table(title=( f"Tables: {self.db_name} " f"({table_count} {self.inflector.plural('table', table_count)})")) # Check if interactive if interactive: # Set caption renderable.caption = "c\[ommands]: Show available commands" # noqa # Add columns [ renderable.add_column(col_name) for col_name in ("#", "name", "interface", "cols", "rows") ] # Iterate over tables for i, (table_name, interface) in enumerate(tables): # Get column count col_count = str(len(interface.Item.__table__.columns)) # Get row count row_count = str(interface.count()) # Add row renderable.add_row(str(i), interface.db_table_name, interface.name, col_count, row_count) # Pad the renderable renderable = Padding(renderable, (1, 1)) # Return renderable return renderable
def output( self, outdata: Union[List[str], Dict[str, Any]], tablefmt: str = "rich", title: str = None, caption: str = None, account: str = None, config=None, ok_status: Union[int, List[int], Tuple[int, str], List[Tuple[int, str]]] = None, ) -> str: # log.debugv(f"data passed to output():\n{pprint(outdata, indent=4)}") def _do_subtables(data: list, tablefmt: str = "rich"): out = [] for inner_dict in data: # the object: switch/vlan etc dict for key, val in inner_dict.items(): if not isinstance(val, (list, dict, tuple)): if val is None: inner_dict[key] = '' elif isinstance(val, str) and val.lower() in ['up', 'down']: color = 'red' if val.lower() == 'down' else 'green' if tablefmt == 'rich': inner_dict[ key] = f'[b {color}]{val.title()}[/b {color}]' else: inner_dict[key] = typer.style(val.title(), fg=color) else: if tablefmt == 'rich': inner_dict[key] = Text(str(val), style=None) else: inner_dict[key] = str(val) else: val = self.listify(val) if val and tablefmt == "rich" and hasattr( val[0], 'keys'): inner_table = Table( *(k for k in val[0].keys()), show_header=True, # padding=(0, 0), pad_edge=False, collapse_padding=True, show_edge=False, header_style="bold cyan", box=SIMPLE) _ = [ inner_table.add_row(*[ self.do_pretty(kk, str(vv)) for kk, vv in v.items() ]) for v in val ] with console.capture(): console.print(inner_table) inner_dict[key] = console.export_text() elif val and tablefmt == "tabulate" and hasattr( val[0], 'keys'): inner_table = tabulate(val, headers="keys", tablefmt=tablefmt) inner_dict[key] = inner_table else: if all(isinstance(v, str) for v in val): inner_dict[key] = ", ".join(val) out.append(inner_dict) return out raw_data = outdata _lexer = table_data = None if config and config.sanitize and raw_data and all( isinstance(x, dict) for x in raw_data): redact = [ "mac", "serial", "neighborMac", "neighborSerial", "neighborPortMac", "longitude", "latitude" ] outdata = [{ k: d[k] if k not in redact else "--redacted--" for k in d } for d in raw_data] # -- // List[str, ...] \\ -- Bypass all formatters, (config file output, etc...) if outdata and all(isinstance(x, str) for x in outdata): tablefmt = "strings" # -- convert List[dict] --> Dict[dev_name: dict] for yaml/json outputs if tablefmt in ['json', 'yaml', 'yml']: outdata = self.listify(outdata) if outdata and 'name' in outdata[0]: outdata: Dict[str, Dict[str, Any]] = { item['name']: {k: v for k, v in item.items() if k != 'name'} for item in outdata } if tablefmt == "json": raw_data = json.dumps(outdata, indent=4) _lexer = lexers.JsonLexer elif tablefmt in ["yml", "yaml"]: raw_data = yaml.dump(outdata, sort_keys=False) _lexer = lexers.YamlLexer elif tablefmt == "csv": raw_data = table_data = "\n".join([ ",".join([ k if outdata.index(d) == 0 else str(v) for k, v in d.items() if k not in CUST_KEYS ]) for d in outdata ]) elif tablefmt == "rich": from rich.console import Console from rich.table import Table from rich.box import HORIZONTALS, SIMPLE from rich.text import Text from centralcli import constants console = Console(record=True) customer_id, customer_name = "", "" # outdata = self.listify(outdata) # -- // List[dict, ...] \\ -- if outdata and all(isinstance(x, dict) for x in outdata): customer_id = outdata[0].get("customer_id", "") customer_name = outdata[0].get("customer_name", "") outdata = [{k: v for k, v in d.items() if k not in CUST_KEYS} for d in outdata] table = Table( # show_edge=False, show_header=True, title=title, header_style='magenta', show_lines=False, box=HORIZONTALS, row_styles=['none', 'dark_sea_green']) fold_cols = ['description'] _min_max = {'min': 10, 'max': 30} set_width_cols = {'name': _min_max, 'model': _min_max} full_cols = [ 'mac', 'serial', 'ip', 'public ip', 'version', 'radio', 'id' ] for k in outdata[0].keys(): if k in fold_cols: table.add_column(k, overflow='fold', max_width=115, justify='left') elif k in set_width_cols: table.add_column(k, min_width=set_width_cols[k]['min'], max_width=set_width_cols[k]['max'], justify='left') elif k in full_cols: table.add_column(k, no_wrap=True, justify='left') else: table.add_column(k, justify='left') formatted = _do_subtables(outdata) [ table.add_row(*list(in_dict.values())) for in_dict in formatted ] if title: table.title = f'[italic cornflower_blue]{constants.what_to_pretty(title)}' if account or caption: table.caption_justify = 'left' table.caption = '' if not account else f'[italic dark_olive_green2] Account: {account}' if caption: table.caption = f"[italic dark_olive_green2]{table.caption} {caption}" data_header = f"--\n{'Customer ID:':15}{customer_id}\n{'Customer Name:':15} {customer_name}\n--\n" with console.capture(): console.print(table) raw_data = console.export_text(clear=False) table_data = console.export_text(styles=True) raw_data = f"{data_header}{raw_data}" if customer_id else f"{raw_data}" table_data = f"{data_header}{table_data}" if customer_id else f"{table_data}" elif tablefmt == "tabulate": customer_id = customer_name = "" outdata = self.listify(outdata) # -- // List[dict, ...] \\ -- if outdata and all(isinstance(x, dict) for x in outdata): customer_id = outdata[0].get("customer_id", "") customer_name = outdata[0].get("customer_name", "") outdata = [{k: v for k, v in d.items() if k not in CUST_KEYS} for d in outdata] raw_data = outdata outdata = _do_subtables(outdata, tablefmt=tablefmt) # outdata = [dict((k, v) for k, v in zip(outdata[0].keys(), val)) for val in outdata] table_data = tabulate(outdata, headers="keys", tablefmt=tablefmt) td = table_data.splitlines(keepends=True) table_data = f"{typer.style(td[0], fg='cyan')}{''.join(td[1:])}" data_header = f"--\n{'Customer ID:':15}{customer_id}\n" \ f"{'Customer Name:':15} {customer_name}\n--\n" table_data = f"{data_header}{table_data}" if customer_id else f"{table_data}" raw_data = f"{data_header}{raw_data}" if customer_id else f"{raw_data}" else: # strings output No formatting # -- // List[str, ...] \\ -- if len(outdata) == 1: if "\n" not in outdata[0]: # we can format green as only success output is sent through formatter. table_data = typer.style(f" {outdata[0]}", fg="green") raw_data = outdata[0] else: # template / config file output # get rid of double nl @ EoF (configs) raw_data = table_data = "{}\n".format( '\n'.join(outdata).rstrip('\n')) else: raw_data = table_data = '\n'.join(outdata) # Not sure what hit's this, but it was created so something must log.debug("List[str] else hit") if _lexer and raw_data: table_data = highlight( bytes(raw_data, 'UTF-8'), _lexer(), formatters.Terminal256Formatter(style='solarized-dark')) return self.Output(rawdata=raw_data, prettydata=table_data, config=config)
table.add_column("Box Office", "[u]$4,331,212,357", no_wrap=True) with beat(10): console.print(table, justify="center") table.title = "Star Wars Box Office" with beat(10): console.print(table, justify="center") table.title = ( "[not italic]:popcorn:[/] Star Wars Box Office [not italic]:popcorn:[/]" ) with beat(10): console.print(table, justify="center") table.caption = "Made with Rich" with beat(10): console.print(table, justify="center") table.caption = "Made with [b]Rich[/b]" with beat(10): console.print(table, justify="center") table.caption = "Made with [b magenta]Rich[/]" with beat(10): console.print(table, justify="center") for row in TABLE_DATA: table.add_row(*row) with beat(10): console.print(table, justify="center")
def get_list_renderable( self, interactive=False, limit=None, offset=0, sort_by=None, filter_by=None, highlight_id=None, ): """ Returns a Rich table renderable for the interface list view """ # Define max limit limit_max = 1000 # Initialize limie limit = limit or limit_max # Get limit where max is 1000 limit = min(limit, limit_max) # Get display map display_map = self.list_display_map # Get items items = self.db_session.query(self.Item) # Check if filter by is not null if filter_by: # Apply filters items = self.filter(queryset=items, display_map=display_map, tuples=filter_by) # Check if sort by is not null if sort_by: # Apply sort fields items = self.sort(*sort_by, queryset=items, display_map=display_map) # Get row count row_count = items.count() # Get page count page_count = math.ceil(row_count / limit) # Define title title = (f"{self.name}: {self.db_table_name}\n" f"Page {int(offset / limit) + 1} of {page_count} " f"({row_count} {self.inflector.plural('row', row_count)})") # Limit items items = items.offset(offset).limit(limit) # Initialize Rich Table as renderable renderable = Table(title=title) # Get display fields display_fields = self.display_list_by # Initialize fields fields = [] # Iterate over display fields for field, display in display_fields.items(): # Create column renderable.add_column(display) # Add field to fields fields.append(field) # Iterate over items for item in items: # Initialize style style = "" # Get item ID item_id = item.id # Check if item is highlighted if item_id == highlight_id: # Set style style = "white on blue" # Add row renderable.add_row( *[str(getattr(item, field)) for field in fields], style=style) # Check if interactive if interactive: # Initialize caption caption = "c\[ommands]: Show available commands" # noqa # Initialize sub-caption sub_caption = "" # Check if sort by is not null if sort_by: # Add sort sub-caption sub_caption += f"sort {(' ' + OPERATOR_AND + ' ').join(sort_by)}\n" # Check if filter by is not null if filter_by: # Get filter strings filter_strings = [f"{f} = {v}" for f, v in filter_by] # Add filter sub-caption sub_caption += ( f"filter {(' ' + OPERATOR_AND + ' ').join(filter_strings)}\n" ) # Check if sub-caption if sub_caption: # Combine caption and sub-caption caption = sub_caption + "\n" + caption # Set caption renderable.caption = caption.strip("\n") # Pad renderable renderable = Padding(renderable, (1, 1)) # Return renderable and row count return renderable, row_count
def summary( self, x: tp.Optional[tp.Any] = None, depth: int = 2, tablefmt: str = "fancy_grid", return_repr: bool = False, initialize: bool = False, eval_shape: bool = True, **tablulate_kwargs, ) -> tp.Optional[str]: """ Prints a summary of the network. Arguments: x: A sample of inputs to the network. depth: The level number of nested level which will be showed. Information about summaries from modules deeper than `depth` will be aggregated together. tablefmt: A string representing the style of the table generated by `tabulate`. See [python-tabulate](https://github.com/astanin/python-tabulate) for more options. tablulate_kwargs: Additional keyword arguments passed to `tabulate`. See [python-tabulate](https://github.com/astanin/python-tabulate) for more options. """ if x is None: x = {} entries: tp.List[types.SummaryTableEntry] states = self.states.copy() if self.run_eagerly else self.states method = (self.call_summary_step if self.run_eagerly else self.call_summary_step_jit) if eval_shape: entries = jax.eval_shape(self.call_summary_step, x, states) else: entries = method(x, states) total_entry = entries[-1] entries = entries[:-1] depth_groups: tp.Dict[ str, tp.List[types.SummaryTableEntry]] = toolz.groupby( lambda entry: "/".join(entry.path.split("/")[:depth]), entries) entries = [ utils.get_grouped_entry(entry, depth_groups) for entry in entries if entry.path in depth_groups ] main_table = Table( show_header=True, show_lines=True, show_footer=True, # box=rich.box.HORIZONTALS, ) main_table.add_column("Layer") main_table.add_column("Outputs Shape") main_table.add_column("Trainable\nParameters") main_table.add_column("Non-trainable\nParameters") rows: tp.List[tp.List[str]] = [] rows.append(["Inputs", utils.format_output(x), "", ""]) for entry in entries: rows.append([ f"{entry.path}{{pad}} " + (f"[dim]{entry.module_type_name}[/]" if entry.module_type_name else ""), utils.format_output(entry.output_value), f"[green]{entry.trainable_params_count:,}[/]{{pad}} {utils.format_size(entry.trainable_params_size)}" if entry.trainable_params_count > 0 else "", f"[green]{entry.non_trainable_params_count:,}[/]{{pad}} {utils.format_size(entry.non_trainable_params_size)}" if entry.non_trainable_params_count > 0 else "", ]) # global summaries params_count = total_entry.trainable_params_count params_size = total_entry.trainable_params_size states_count = total_entry.non_trainable_params_count states_size = total_entry.non_trainable_params_size total_count = params_count + states_count total_size = params_size + states_size rows.append([ "", "Total", (f"[green]{params_count:,}[/]{{pad}} {utils.format_size(params_size)}" if params_count > 0 else ""), (f"[green]{states_count:,}[/]{{pad}} {utils.format_size(states_size)}" if states_count > 0 else ""), ]) # add padding for col in range(4): max_length = max( len(line.split("{pad}")[0]) for row in rows for line in row[col].split("\n")) for row in rows: row[col] = "\n".join( line.format( pad=" " * (max_length - len(line.rstrip().split("{pad}")[0]))) for line in row[col].rstrip().split("\n")) for row in rows[:-1]: main_table.add_row(*row) main_table.columns[1].footer = Text.from_markup(rows[-1][1], justify="right") main_table.columns[2].footer = rows[-1][2] main_table.columns[3].footer = rows[-1][3] main_table.caption_style = "bold" main_table.caption = ( "\nTotal Parameters: " + f"[green]{total_count:,}[/] {utils.format_size(total_size)}" if total_count > 0 else "") summary = "\n" + utils.get_table_repr(main_table) print(summary) if return_repr: return summary
def overview(self): r""" Prints an overview for the wallet's colkey. """ console = bittensor.__console__ wallet = bittensor.wallet( config = self.config ) subtensor = bittensor.subtensor( config = self.config ) all_hotkeys = CLI._get_hotkey_wallets_for_wallet( wallet ) neurons = [] block = subtensor.block with console.status(":satellite: Syncing with chain: [white]{}[/white] ...".format(self.config.subtensor.network)): for wallet in tqdm(all_hotkeys): nn = subtensor.neuron_for_pubkey( wallet.hotkey.ss58_address ) if not nn.is_null: neurons.append( nn ) balance = subtensor.get_balance( wallet.coldkeypub.ss58_address ) TABLE_DATA = [] total_stake = 0.0 total_rank = 0.0 total_trust = 0.0 total_consensus = 0.0 total_incentive = 0.0 total_dividends = 0.0 total_emission = 0 for nn, hotwallet in tqdm(list(zip(neurons,all_hotkeys))): uid = nn.uid active = nn.active stake = nn.stake rank = nn.rank trust = nn.trust consensus = nn.consensus incentive = nn.incentive dividends = nn.dividends emission = int(nn.emission * 1000000000) last_update = int(block - nn.last_update) row = [ hotwallet.hotkey_str, str(uid), str(active), '{:.5f}'.format(stake), '{:.5f}'.format(rank), '{:.5f}'.format(trust), '{:.5f}'.format(consensus), '{:.5f}'.format(incentive), '{:.5f}'.format(dividends), '{}'.format(emission), str(last_update), bittensor.utils.networking.int_to_ip( nn.ip) + ':' + str(nn.port) if nn.port != 0 else '[yellow]none[/yellow]', nn.hotkey ] total_stake += stake total_rank += rank total_trust += trust total_consensus += consensus total_incentive += incentive total_dividends += dividends total_emission += emission TABLE_DATA.append(row) total_neurons = len(neurons) table = Table(show_footer=False) table.title = ( "[white]Wallet - {}:{}".format(self.config.wallet.name, wallet.coldkeypub.ss58_address) ) table.add_column("[overline white]HOTKEY", str(total_neurons), footer_style = "overline white", style='bold white') table.add_column("[overline white]UID", str(total_neurons), footer_style = "overline white", style='yellow') table.add_column("[overline white]ACTIVE", justify='right', style='green', no_wrap=True) table.add_column("[overline white]STAKE(\u03C4)", '\u03C4{:.5f}'.format(total_stake), footer_style = "overline white", justify='right', style='green', no_wrap=True) table.add_column("[overline white]RANK", '{:.5f}'.format(total_rank), footer_style = "overline white", justify='right', style='green', no_wrap=True) table.add_column("[overline white]TRUST", '{:.5f}'.format(total_trust), footer_style = "overline white", justify='right', style='green', no_wrap=True) table.add_column("[overline white]CONSENSUS", '{:.5f}'.format(total_consensus), footer_style = "overline white", justify='right', style='green', no_wrap=True) table.add_column("[overline white]INCENTIVE", '{:.5f}'.format(total_incentive), footer_style = "overline white", justify='right', style='green', no_wrap=True) table.add_column("[overline white]DIVIDENDS", '{:.5f}'.format(total_dividends), footer_style = "overline white", justify='right', style='green', no_wrap=True) table.add_column("[overline white]EMISSION(\u03C1)", '\u03C1{}'.format(int(total_emission)), footer_style = "overline white", justify='right', style='green', no_wrap=True) table.add_column("[overline white]UPDATED", justify='right', no_wrap=True) table.add_column("[overline white]AXON", justify='left', style='dim blue', no_wrap=True) table.add_column("[overline white]HOTKEY", style='dim blue', no_wrap=False) table.show_footer = True table.caption = "[white]Wallet balance: [green]\u03C4" + str(balance.tao) console.clear() for row in TABLE_DATA: table.add_row(*row) table.box = None table.pad_edge = False table.width = None console.print(table)