def magic_table()

in repl/src/main/resources/fake_shell.py [0:0]


def magic_table(name):
    try:
        value = global_dict[name]
    except KeyError:
        exc_type, exc_value, tb = sys.exc_info()
        return execute_reply_error(exc_type, exc_value, None)

    if not isinstance(value, (list, tuple)):
        value = [value]

    headers = {}
    data = []

    for row in value:
        cols = []
        data.append(cols)
        
        if 'Row' == row.__class__.__name__:
            row = row.asDict()

        if not isinstance(row, (list, tuple, dict)):
            row = [row]

        if isinstance(row, (list, tuple)):
            iterator = enumerate(row)
        else:
            iterator = sorted(row.items())

        for name, col in iterator:
            col_type, col = magic_table_convert(col)

            try:
                header = headers[name]
            except KeyError:
                header = {
                    'name': str(name),
                    'type': col_type,
                }
                headers[name] = header
            else:
                # Reject columns that have a different type. (allow none value)
                if col_type != "NULL_TYPE" and header['type'] != col_type:
                    if header['type'] == "NULL_TYPE":
                        header['type'] = col_type
                    else:
                        exc_type = Exception
                        exc_value = 'table rows have different types'
                        return execute_reply_error(exc_type, exc_value, None)

            cols.append(col)

    headers = [v for k, v in sorted(headers.items())]

    return {
        'application/vnd.livy.table.v1+json': {
            'headers': headers,
            'data': data,
        }
    }