class_docstr: Auxiliary class to decouple display handling logic from other functionality.
class_docstr_tok: ["Auxiliary","class","to","decouple","display","handling","logic","from","other","functionality","."]
docstr: Handle plotly figure item.
docstr_tok: ["Handle","plotly","figure","item","."]
code: def handle_figure(
      cls,
      item: BaseFigure,
      index: int,
      plotly_to_image: bool = False,
      **kwargs,
  ) -> str:
      """Handle plotly figure item."""
      if plotly_to_image is True:
          img = item.to_image(format="jpeg", engine="auto")
          return imagetag(img)
  
      post_script = textwrap.dedent(
          """
              var gd = document.getElementById('{plot_id}');
              var x = new MutationObserver(function (mutations, observer) {{
                      var display = window.getComputedStyle(gd).display;
                      if (!display || display === 'none') {{
                          console.log([gd, 'removed!']);
                          Plotly.purge(gd);
                          observer.disconnect();
                      }}
              }});
  
              // Listen for the removal of the full notebook cells
              var notebookContainer = gd.closest('#notebook-container');
              if (notebookContainer) {{
                  x.observe(notebookContainer, {childList: true});
              }}
  
              // Listen for the clearing of the current output cell
              var outputEl = gd.closest('.output');
              if (outputEl) {{
                  x.observe(outputEl, {childList: true});
              }}
          """
      )
      return to_html(
          item,
          auto_play=False,
          include_plotlyjs="require",
          post_script=post_script,
          full_html=False,
          default_width="100%",
          default_height=525,
          validate=True,
      )
  
imports: import textwrap
  import typing
  from plotly.basedatatypes import BaseFigure
  from plotly.io import to_html
  from typing_extensions import Literal
  from deepchecks.core import check_result
  from deepchecks.core.resources import requirejs_script
  from deepchecks.core.serialization.abc import ABCDisplayItemsHandler, HtmlSerializer
  from deepchecks.core.serialization.common import aggregate_conditions, form_output_anchor, plotlyjs_script
  from deepchecks.core.serialization.dataframe.html import DataFrameSerializer
  from deepchecks.utils.html import imagetag, linktag
label_desc: Use image node_id 8 for calling the DisplayItemsHandler obj's underlying member method code with example usage: obj.handle_figure(cls, item, index, plotly_to_image) and returns: to_html, imagetag