import io


def export_html(palette):
    """
    Return a HTML string for a palette preview

    Arguments:
        palette (dict): Dictionnary of named colors (as dumped in JSON from
            ``colors`` command)

    Returns:
        string: HTML preview.
    """
    # HTML color palette
    html_palette = io.StringIO()
    html_palette.write('<div class="palette" style="display: flex; flex-direction: row; flex-wrap: wrap;">\n')

    for original_code, values in sorted(palette.items(), key=lambda x:x[1]):
        name, from_color = values
        html_palette.write('  <div class="item" style="flex: 1 0 20%; max-width: 20%; padding: 0.5rem;">\n')
        html_palette.write('    <div class="color" style="background-color: {}; width: 100%; height: 3rem;"></div>\n'.format(original_code))
        html_palette.write('    <p class="code" style="">{}</p>\n'.format(original_code))
        html_palette.write('    <p class="name" style="">{}</p>\n'.format(name))
        html_palette.write('  </div>\n')

    html_palette.write('</div>\n')

    output = html_palette.getvalue()

    html_palette.close()

    return output