plotly / plotly/plotly.py

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

未關閉
#4,334 2 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

feature P3
主要語言
Python
星號
18.8k
分支
2.8k
平均合併
16 小時 26 分鐘
30 天內合併 PR
21

描述

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.

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

issue 中沒有指定原始檔案或測試。先找出 figure layout 的變更與還原行為,接著檢查 context manager 如何保留現有值,並在 fig.write_image('plot.pdf'); 之後還原這些值;done 應包含對暫時性 showlegend 變更的覆蓋,同時不刪除 layout 鍵。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
data-visualization
Issue 類型
功能
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。