change context manager for temporary auto-reverted `figure.layout`
Open
Nobody has claimed this yet.
feature
P3
- Dominant language
- Python
- Stars
- 18.8k
- Forks
- 2.8k
- Avg merge
- 16h 26m
- Merged PRs (30d)
- 21
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No source file or test is named in the issue. Start by locating the figure layout mutation and restoration behavior, then inspect how a context manager could preserve existing values and restore them after fig.write_image('plot.pdf'); done should include coverage for temporary showlegend changes without deleting layout keys.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data-visualization
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100