Skip to content

Reference (Utility Functions)

Plot

shirotsubaki.utils.figure_to_html(fig, fmt='svg', embed=True, html_dir=None, img_rel_path=None, dpi=100, dpi_html=90, callback=None)

Convert a matplotlib Figure into HTML.
See example: examples/#example-4-plot

Parameters:

  • fig (Figure) –

    A matplotlib Figure object

  • fmt (str, default: 'svg' ) –

    Image format

  • embed (bool, default: True ) –

    Whether to embed the image as a data URI in the HTML or save it as a separate file

  • html_dir (str, default: None ) –

    Output directory of the HTML report (used when embed=False)

  • img_rel_path (str, default: None ) –

    Relative path from the HTML file to the image file (used when embed=False)

  • dpi (int, default: 100 ) –

    Resolution used only when fmt='png'

  • dpi_html (int, default: 90 ) –

    Used to calculate the display size in the HTML file

  • callback (Callable[[], None], default: None ) –

    A function to call after converting (e.g., matplotlib.pyplot.close)

Returns:

  • str

    An HTML string that embeds or links the image

DataFrame

shirotsubaki.utils.style_float_cols(styled, fmt='{:.3f}')

Format float columns and right-align them.
See example: examples/#example-5-data-frame

Parameters:

  • styled (Styler) –

    A pandas Styler object

  • fmt (str, default: '{:.3f}' ) –

    A format string to apply to float columns

shirotsubaki.utils.style_top_ranks_per_row(styled, targets, ascending=True, styles={1: 'background: #8dce6f;', 2: 'background: #ccf188;'})

Apply styles to the top 1st and 2nd values in each row.
See example: examples/#example-5-data-frame

Parameters:

  • styled (Styler) –

    A pandas Styler object

  • targets (list[str]) –

    List of column names to apply highlighting to

  • ascending (bool, default: True ) –

    Whether to rank in ascending order (default: True)

  • styles (dict[int, str], default: {1: 'background: #8dce6f;', 2: 'background: #ccf188;'} ) –

    A mapping from rank (1, 2, ...) to CSS style string

Others

shirotsubaki.utils.lighten_color(hex_color, ratio=0.5)

Lightens a color given in hex string and returns it as a hex string.

Parameters:

  • hex_color (str) –

    A color in hex format

  • ratio (float, default: 0.5 ) –

    The proportion of white to mix with the original color
    Should be between 0.0 (no change) and 1.0 (full white)

Returns:

  • str

    A lightened color in hex format

Example
from shirotsubaki import utils as stutils
color = stutils.lighten_color('#336699', ratio=0.5)  # -> '#99B2CC'