def draw_console(self, series): if isinstance(series, Series): series = [series] table = rich.table.Table( expand=False, box=rich.box.MINIMAL, border_style="rgb(100,100,100)", ) table.add_column(self.x_axis.label) for _ in series: table.add_column(self.y_axis.label) for n in range(len(series[0].data)): row = [series[0].data[n][0]] for s in series: row.append(s.data[n][1]) for n, v in enumerate(row): if n == 0: row[n] = rich.text.Text(f"{v:0.3f}", style="bold italic") elif isinstance(v, float): color = _color_for_value(v, self.y_axis.min, self.y_axis.max) row[n] = rich.text.Text(f"{v:0.3f}", style=color) else: row[n] = f"{v!r}" table.add_row(*row) return rich.align.Align.center(table)
def get_rich_table(list: typing.List[dict]): import rich.table table = rich.table.Table() columns: set = functools.reduce(set.union, map(set, list)) for column in columns: table.add_column(column) for item in list: table.add_row(*map(get_rich, map(item.get, columns))) return table
def get_rich_pandas(df, table=None, **k): if table is None: table = rich.table.Table() indexes = df.index.names columns = indexes + list(df.columns) for column in columns: table.add_column(column) for index, row in df.iterrows(): if not isinstance(index, tuple): index = (index, ) data = dict(zip(list(indexes), map("[b]{}".format, index)), **row) table.add_row(*map(get_rich, map(data.get, columns))) return table
def _rich_table(name, count_str, rows, offset, has_more, colors=True, show_footer=False): header = 'table ' if name: header += name if offset: header += f'[{offset}..]' header += f" {count_str}" if not rows: return header table = rich.table.Table(title=rich.text.Text(header), show_footer=show_footer, min_width=len(header)) # TODO enable/disable styling for k, v in rows[0].items(): kw = {} if isinstance(v, (int, float)): kw['justify'] = 'right' if colors: if isinstance(v, int): kw['style'] = 'cyan' elif isinstance(v, float): kw['style'] = 'yellow' elif isinstance(v, str): kw['style'] = 'green' table.add_column(k, footer=k, **kw) for r in rows: table.add_row(*[ rich.markup.escape(str(x) if x is not None else '-') for x in r.values() ]) if has_more: table.add_row(*['...' for x in rows[0]]) return table
def __create_failed_table(self, resp_json, level="File"): """Output a response after deletion.""" # Check that enough info if not all(x in resp_json for x in ["not_exists", "not_removed"]): raise dds_cli.exceptions.APIError( f"Malformatted response detected when attempting remove action on {self.project}." ) # Get info not_exists = resp_json["not_exists"] delete_failed = resp_json["not_removed"] # Create table if any files failed if not_exists or delete_failed: if self.no_prompt: self.failed_files = {"Errors": []} for x in not_exists: self.failed_files["Errors"].append( {x: f"No such {level.lower()}"}) for x, y in delete_failed.items(): self.failed_files["Errors"].append({x: y}) else: # Create table and add columns table = rich.table.Table( title=f"{level}s not deleted", title_justify="left", show_header=True, header_style="bold", ) columns = [level, "Error"] for x in columns: table.add_column(x) # Add rows for x in not_exists: table.add_row(rich.markup.escape(x), f"No such {level.lower()}") for x, y in delete_failed.items(): table.add_row( f"[light_salmon3]{rich.markup.escape(x)}[/light_salmon3]", f"[light_salmon3]{rich.markup.escape(y)}[/light_salmon3]", ) # Print out table self.failed_table = rich.padding.Padding(table, 1)
def _print_error_table(self, data_results=None, label_results=None): results = {} if isinstance(data_results, dict): for key in data_results: results[key] = {} results[key]['data'] = data_results[key] results[key]['label'] = None if isinstance(label_results, dict): for key in label_results: if key in results: results[key]['label'] = label_results[key] else: results[key] = {'label': label_results[key], 'data': None} if not next(iter(results), None): return console.print('\n[b red]** Error Table **[/b red]') page = 1 page_length = math.ceil(len(results) / 10) while True: table = rich.table.Table(show_header=True, header_style="bold magenta") table.add_column("FILE NAME") if isinstance(data_results, dict): table.add_column("DATA UPLOAD") if isinstance(label_results, dict): table.add_column("LABEL UPLOAD") for _ in range(10): key = next(iter(results), None) if not key: break if isinstance(data_results, dict) and isinstance( label_results, dict): data = results[key]['data'] label = results[key]['label'] table.add_row(key, f"{data if data else '-'}", f"{label if label else '-'}") elif isinstance(data_results, dict): data = results[key]['data'] table.add_row(key, f"{data if data else '-'}") else: label = results[key]['label'] table.add_row(key, f"{label if label else '-'}") del results[key] console.print(table) if not next(iter(results), None): break else: click.echo( f'Press any button to continue to the next page ({page}/{page_length}). Otherwise press ‘Q’ to quit.', nl=False) key = click.getchar() click.echo() if key == 'q' or key == 'Q': break console.log(f'[b]Check the log file for more details[/b]') console.log(f'- {simple_logger.handlers[0].baseFilename}') console.log(f'- {logger.handlers[0].baseFilename}')
def describe_projects(self): page = 1 while True: projects, project_count = self._get_projects( page=page, page_size=self.CURRENT_PAGE_COUNT) table = rich.table.Table(show_header=True, header_style="bold magenta") table.add_column("NAME", width=50) table.add_column("LABELS", justify="right") table.add_column("PROGRESS", justify="right") for item in projects: table.add_row(item.name, f"{item.label_count}", f"{item.progress}%") console.print(table) total_page = math.ceil(project_count / self.CURRENT_PAGE_COUNT) if total_page > page: click.echo( f'Press any button to continue to the next page ({page}/{total_page}). Otherwise press ‘Q’ to quit.', nl=False) key = click.getchar() click.echo() page = page + 1 if key == 'q' or key == 'Q': return elif total_page <= page: return
def cli_list(cfg): engine = migrate.migrate(cfg) indexes = index.Index.list_indexes(engine, False) table = rich.table.Table(title='Indexes') table.add_column('Last Built') table.add_column('Index') table.add_column('S3 Prefix') table.add_column('Schema') for i in sorted(indexes, key=lambda i: i.name): built = f'[green]{i.built}[/]' if i.built else '[red]Not built[/]' table.add_row(built, i.name, i.s3_prefix, str(i.schema)) console.print(table)
def print_summary(self): """Prints a summary of all pipelines.""" filtered_workflows = self.filtered_workflows() # Sort by released / dev / archived, then alphabetical if not self.sort_workflows_by or self.sort_workflows_by == "release": filtered_workflows.sort(key=lambda wf: ( (wf.releases[-1].get("published_at_timestamp", 0) if len(wf.releases) > 0 else 0) * -1, wf.archived, wf.full_name.lower(), )) # Sort by date pulled elif self.sort_workflows_by == "pulled": def sort_pulled_date(wf): try: return wf.local_wf.last_pull * -1 except: return 0 filtered_workflows.sort(key=sort_pulled_date) # Sort by name elif self.sort_workflows_by == "name": filtered_workflows.sort(key=lambda wf: wf.full_name.lower()) # Sort by stars, then name elif self.sort_workflows_by == "stars": filtered_workflows.sort(key=lambda wf: (wf.stargazers_count * -1, wf.full_name.lower())) # Build summary list to print table = rich.table.Table() table.add_column("Pipeline Name") table.add_column("Stars", justify="right") table.add_column("Latest Release", justify="right") table.add_column("Released", justify="right") table.add_column("Last Pulled", justify="right") table.add_column("Have latest release?") for wf in filtered_workflows: wf_name = "[bold][link=https://nf-co.re/{0}]{0}[/link]".format( wf.name, wf.full_name) version = "[yellow]dev" if len(wf.releases) > 0: version = "[blue]{}".format(wf.releases[-1]["tag_name"]) published = wf.releases[-1]["published_at_pretty"] if len( wf.releases) > 0 else "[dim]-" pulled = wf.local_wf.last_pull_pretty if wf.local_wf is not None else "[dim]-" if wf.local_wf is not None: revision = "" if wf.local_wf.active_tag is not None: revision = "v{}".format(wf.local_wf.active_tag) elif wf.local_wf.branch is not None: revision = "{} - {}".format(wf.local_wf.branch, wf.local_wf.commit_sha[:7]) else: revision = wf.local_wf.commit_sha if wf.local_is_latest: is_latest = "[green]Yes ({})".format(revision) else: is_latest = "[red]No ({})".format(revision) else: is_latest = "[dim]-" rowdata = [ wf_name, str(wf.stargazers_count), version, published, pulled, is_latest ] # Handle archived pipelines if wf.archived: rowdata[1] = "archived" rowdata = [re.sub("\[\w+\]", "", k) for k in rowdata] table.add_row(*rowdata, style="dim") else: table.add_row(*rowdata) if len(filtered_workflows) > 0: # Print summary table return table else: return_str = f"No pipelines found using filter keywords: '{', '.join(self.keyword_filters)}'" if self.keyword_filters == ("modules", ): return_str += "\n\n:bulb: Did you mean 'nf-core modules list' instead?" return return_str
def _blob_to_table(cls, rows, cols): table = rich.table.Table(show_header=True) [table.add_column(col_name) for col_name in cols] [table.add_row(*r) for r in rows] return table
def print_help() -> None: console = rich.console.Console() console.print("[bold]HTTPX :butterfly:", justify="center") console.print() console.print("A next generation HTTP client.", justify="center") console.print() console.print( "Usage: [bold]httpx[/bold] [cyan]<URL> [OPTIONS][/cyan] ", justify="left" ) console.print() table = rich.table.Table.grid(padding=1, pad_edge=True) table.add_column("Parameter", no_wrap=True, justify="left", style="bold") table.add_column("Description") table.add_row( "-m, --method [cyan]METHOD", "Request method, such as GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD.\n" "[Default: GET, or POST if a request body is included]", ) table.add_row( "-p, --params [cyan]<NAME VALUE> ...", "Query parameters to include in the request URL.", ) table.add_row( "-c, --content [cyan]TEXT", "Byte content to include in the request body." ) table.add_row( "-d, --data [cyan]<NAME VALUE> ...", "Form data to include in the request body." ) table.add_row( "-f, --files [cyan]<NAME FILENAME> ...", "Form files to include in the request body.", ) table.add_row("-j, --json [cyan]TEXT", "JSON data to include in the request body.") table.add_row( "-h, --headers [cyan]<NAME VALUE> ...", "Include additional HTTP headers in the request.", ) table.add_row( "--cookies [cyan]<NAME VALUE> ...", "Cookies to include in the request." ) table.add_row( "--auth [cyan]<USER PASS>", "Username and password to include in the request. Specify '-' for the password to use " "a password prompt. Note that using --verbose/-v will expose the Authorization " "header, including the password encoding in a trivially reversible format.", ) table.add_row( "--proxy [cyan]URL", "Send the request via a proxy. Should be the URL giving the proxy address.", ) table.add_row( "--timeout [cyan]FLOAT", "Timeout value to use for network operations, such as establishing the connection, " "reading some data, etc... [Default: 5.0]", ) table.add_row("--follow-redirects", "Automatically follow redirects.") table.add_row("--no-verify", "Disable SSL verification.") table.add_row( "--http2", "Send the request using HTTP/2, if the remote server supports it." ) table.add_row( "--download [cyan]FILE", "Save the response content as a file, rather than displaying it.", ) table.add_row("-v, --verbose", "Verbose output. Show request as well as response.") table.add_row("--help", "Show this message and exit.") console.print(table)
def print_entries( *, console: rich.console.Console, title: str, entries: Iterable[client.Entry], debug: bool, highlight_ids: AbstractSet[str] = frozenset(), center: bool = False, only_totals: bool = False, add_date: bool = False, ) -> None: table = rich.table.Table( title=title, box=rich.box.ROUNDED, ) table.add_column("Description", style="yellow") table.add_column("Start", style="cyan") table.add_column("End", style="cyan") table.add_column("Duration", style="dim cyan") table.add_column("Project") table.add_column("Tags", style="blue") table.add_column(":gear:") # icons total = datetime.timedelta() project_totals = collections.defaultdict(datetime.timedelta) time_format = "[b]%Y-%m-%d[/b] %H:%M" if add_date else "%H:%M" for entry in reversed(list(entries)): if debug: console.print(entry, highlight=True) data = [] data.append(entry.description) assert entry.start is not None, entry data.append(entry.start.strftime(time_format)) if entry.end is None: data.append(":clock3:") now = datetime.datetime.now(dateutil.tz.tzlocal()) duration = now - entry.start else: data.append(entry.end.strftime(time_format)) duration = entry.end - entry.start total += duration data.append(timedelta_str(duration)) proj_key = (entry.project or "Other", entry.project_color or "default") project_totals[proj_key] += duration if entry.project is None: data.append("") else: data.append( f"[{entry.project_color}]{entry.project}[/{entry.project_color}]" ) data.append(", ".join(entry.tags)) icon = "" if entry.eid in highlight_ids: icon += ":sparkles:" if entry.billable: icon += ":heavy_dollar_sign:" data.append(icon) style = None if highlight_ids and entry.eid not in highlight_ids: style = rich.style.Style(dim=True) table.add_row(*data, style=style) if not only_totals: renderable = rich.align.Align(table, "center") if center else table console.print(renderable) justify = "center" if center else None grid = rich.table.Table.grid() grid.add_column() grid.add_column() grid.add_row("Total: ", timedelta_str(total), style="bold") for (proj, color), tag_total in sorted(project_totals.items()): grid.add_row(f"[{color}]{proj}[/{color}]: ", timedelta_str(tag_total)) console.print(grid, justify=justify)
def print_entries( console: rich.console.Console, date: datetime.date, entries: Iterable[client.Entry], *, debug: bool, workspace_name: str, highlight_ids: AbstractSet[str] = frozenset(), center: bool = False, ) -> None: console.print(f"[yellow]Workspace:[/yellow] {workspace_name}\n") date_str = date.strftime("%a, %Y-%m-%d") table = rich.table.Table( title=date_str, box=rich.box.ROUNDED, ) table.add_column("Description", style="yellow") table.add_column("Start", style="cyan") table.add_column("End", style="cyan") table.add_column("Project") table.add_column("Tags", style="blue") table.add_column(":gear:") # icons total = datetime.timedelta() project_totals = collections.defaultdict(datetime.timedelta) for entry in reversed(list(entries)): if debug: console.print(entry, highlight=True) data = [] data.append(entry.description) assert entry.start is not None, entry data.append(entry.start.strftime("%H:%M")) if entry.end is None: data.append(":clock3:") now = datetime.datetime.now(dateutil.tz.tzlocal()) duration = now - entry.start else: data.append(entry.end.strftime("%H:%M")) duration = entry.end - entry.start total += duration proj_key = (entry.project or "Other", entry.project_color or "default") project_totals[proj_key] += duration if entry.project is None: data.append("") else: data.append( f"[{entry.project_color}]{entry.project}[/{entry.project_color}]" ) data.append(", ".join(entry.tags)) icon = "" if entry.eid in highlight_ids: icon += ":sparkles:" if entry.billable: icon += ":heavy_dollar_sign:" data.append(icon) style = None if highlight_ids and entry.eid not in highlight_ids: style = rich.style.Style(dim=True) table.add_row(*data, style=style) renderable = rich.align.Align(table, "center") if center else table console.print(renderable) justify = "center" if center else None console.print(f"[b]Total: {timedelta_str(total)}[/b]", justify=justify) for (proj, color), tag_total in sorted(project_totals.items()): console.print( f"[{color}]{proj}[/{color}]: {timedelta_str(tag_total)}", justify=justify, )
def print_help(self): create_table = lambda name: rich.table.Table \ ( box = None, show_header = False, title = name.upper(), padding = (0, 0, 0, self.padding), title_justify = 'left', title_style = 'bold', ) add_row = lambda table, name, type, description, default: table.add_row \ ( '[{style}]{data}[/{style}]'.format \ ( style = self.colour, data = name, ), '[{style}]<{data}>[/{style}]'.format \ ( style = utils.get_colour(type), data = type.__name__, ) if type is not Ellipsis else '', description, '[dim i]Default: [{style}]{data!r}[/{style}][/dim i]'.format \ ( style = utils.get_colour(builtins.type(default)), data = default, ) if default is not Ellipsis else '', ) create_option = lambda name, help: click.core.Option \ ( param_decls = [f'--{name}'], is_flag = True, help = help, hidden = True, ) global_options = \ ( create_option('help', 'Display the program help manual'), create_option('version', 'Display the program version'), ) table_names = \ ( 'arguments', 'options', 'global options', ) column_names = \ ( 'name', 'type', 'description', 'default', ) command_info = self.registered_commands[0] command = typer.main.get_command_from_info(command_info) command_examples = command_info.examples command_examples.append(((), {global_options[0].name: True})) tables = {} for table_name in table_names: table = create_table(table_name) for column_name in column_names: table.add_column(header = column_name) tables[table_name] = table for param in command.params: if param.hidden \ or param.name in [option.name for option in global_options]: continue if param.param_type_name == 'option': param_name = f'--{param.name}' add_row \ ( tables[f'{param.param_type_name}s'], f'--{param.name}', command.callback.__annotations__[param.name], param.help, param.default, ) for global_option in global_options: add_row \ ( tables['global options'], f'--{global_option.name}', ..., global_option.help, ..., ) typer.secho(self.info.name.title(), bold = True, nl = False) if self.info.name is not None: typer.secho(' version ', nl = False) typer.secho(self.info.version, fg = self.colour, nl = False) print('\n') if command.help: print(' ' * self.padding + command.help, end='\n\n') tables = \ ( tables['arguments'], tables['options'], tables['global options'], ) min_widths = [0 for _ in range(len(column_names))] for table in tables: col_widths = table._calculate_column_widths(self.console, self.console.width) for index, min_width in enumerate(min_widths): min_widths[index] = max(min_width, col_widths[index] - self.padding) for table in tables: for index, min_width in enumerate(min_widths): table.columns[index].min_width = min_width self.console.print(table) print() table = create_table('examples') table.add_column('example') def example(*arguments, **options): data = '' for argument in arguments: argument_colour = utils.get_colour(type(argument)) if isinstance(argument, str) and ' ' in argument: argument = repr(argument) data += f'[{argument_colour}]{argument!s}[/{argument_colour}] ' for option_key, option_val in options.items(): option_colour = utils.get_colour(type(option_val)) if isinstance(option_val, str) and ' ' in option_val: option_val = repr(option_val) data += f'[{self.colour}]--{option_key}[/{self.colour}]' if not isinstance(option_val, bool): data += f' [{option_colour}]{option_val!s}[/{option_colour}]' data += ' ' table.add_row \ ( f'[blue]$[/blue] [i]{self.info.name} {data}[/i]', ) for arguments, options in command_examples: example(*arguments, **options) self.console.print(table) print()