def expect_fig(fig: go.Figure, filename: str, check: bool) -> None: """Check for pixel-for-pixel equivalence to stored image.""" ext = "png" if check: found = fig.to_image(format=ext) try: file = open(filename + "." + ext, "rb") expected = file.read() if expected != found: print(f"{filename}: differs from reference image.") raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), filename + ".new." + ext) print(f"{filename}: image identical.") # fig.show(config=default_config()) except FileNotFoundError as e: file_new = open(e.filename, "wb") file_new.write(found) print(f"{filename}: creating new reference image.") fig.show(config=default_config()) assert False else: print(f"{filename}: image not compared.")
def ploty_fig_to_image(fig: go.Figure, width: int = 360, height: int = 360): fig_data = fig.to_image(format='svg', width=width, height=height).decode() return img(src=f'data:image/svg+xml;base64,{fig_data}', className='img-fluid', style=f'width: 100%; height: auto')