plotly / plotly/plotly.py

change context manager for temporary auto-reverted `figure.layout`

Open
#4,334 2 comments 1 reaction 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.