Render list of dicts as table

This commit is contained in:
Bridget
2020-04-14 01:22:31 +02:00
parent 53bae0bd5c
commit ae71285beb

View File

@@ -9,6 +9,7 @@ from typing import Dict, List
import requests
from colorama import Back, Fore, Style, deinit, init # type: ignore
from tabulate import tabulate
from urllib3.connectionpool import InsecureRequestWarning # type: ignore
from lib import ErrorReceived, connect, disconnect, get_available_commands, send_cmd
@@ -52,6 +53,13 @@ class Shell(cmd.Cmd):
except Exception as e:
return f"{Fore.RED}Error{Fore.RESET}: {e}"
# Render as table if list of dicts
if isinstance(result, list) and isinstance(result[0], dict):
rows = []
for item in result:
rows.append(list(item.keys()) + list(item.values()))
return tabulate(rows, tablefmt="plain")
return result
def do_connect(self, url):