plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

Feature Request: Reproducible Plot HTMLs

Open achimgaedke opened this issue 4 years ago • 1 comments

The HTML/js contains UUIDs generated by python's built-in uuid.uuid4. By nature, those are unique and totally random.

But if you generate documents as part of a pipeline (e.g. via https://dvc.org/ ) then it would be great to have them reproducible down to the byte.

import plotly.express as px

fig =px.scatter(x=range(10), y=range(10))
fig.write_html("file1.html")
fig.write_html("file2.html")

The files will be identical except from the UUIDs.

I "solved" this problem by monkey-patching the relevant function:

# monkey patch
import uuid
import random

uuid4_rnd = random.Random(0)
def uuid4_seeded():
    """Generate a random UUID using random.Random"""
    return uuid.UUID(bytes=uuid4_rnd.randbytes(16), version=4)

uuid.uuid4 = uuid4_seeded

fig =px.scatter(x=range(10), y=range(10))
fig.write_html("file.html")

re-executing this snippet will produce identical files.

It would be great to exchange one function (or set a seed for an internal version of uuid) in plotly instead of hijacking the python built-in.

achimgaedke avatar Sep 22 '21 05:09 achimgaedke

A minimal implementation would exchange the two uuid.uuid4 calls in the plotly.py code base with

import random
uuid.UUID(bytes=random.randbytes(16), version=4)

This will allow random.seed(42) to do the trick.

achimgaedke avatar Sep 22 '21 06:09 achimgaedke

see #1968

gvwilson avatar Aug 12 '24 16:08 gvwilson