Skip to content

change context manager for temporary auto-reverted figure.layout #4334

Open
@janosh

Description

@janosh

Instead of

fig.layout.showlegend = False
fig.write_image('plot.pdf')
fig.layout.showlegend = True

it would be nice to write

from plotly.express import plotly_patch  # or similar

with plotly_patch(fig.layout, showlegend=False):
    fig.write_image('plot.pdf')

I tried

from unittest.mock import patch

with patch.dict(fig.layout, showlegend=False):
    fig.write_image('plot.pdf')

which unfortunately doesn't correctly restore the previous value but tries to delete the patched keys which is undesired behavior and raises AttributeError to boot:

   1906 """Unpatch the dict."""
   1907 if self._original is not None:
-> 1908     self._unpatch_dict()
...
   1934 keys = list(in_dict)
   1935 for key in keys:
-> 1936     del in_dict[key]

AttributeError: __delitem__

Related

Found this forum post where such a context manager was welcomed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3backlogfeaturesomething new

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions