| 302 | ipython_display.display_html(script, raw=True) |
| 303 | |
| 304 | def to_mimebundle(self, fig_dict): |
| 305 | from plotly.io import to_html |
| 306 | |
| 307 | include_mathjax = "cdn" |
| 308 | |
| 309 | # build post script |
| 310 | post_script = [ |
| 311 | """ |
| 312 | var gd = document.getElementById('{plot_id}'); |
| 313 | var x = new MutationObserver(function (mutations, observer) {{ |
| 314 | var display = window.getComputedStyle(gd).display; |
| 315 | if (!display || display === 'none') {{ |
| 316 | console.log([gd, 'removed!']); |
| 317 | Plotly.purge(gd); |
| 318 | observer.disconnect(); |
| 319 | }} |
| 320 | }}); |
| 321 | |
| 322 | // Listen for the removal of the full notebook cells |
| 323 | var notebookContainer = gd.closest('#notebook-container'); |
| 324 | if (notebookContainer) {{ |
| 325 | x.observe(notebookContainer, {childList: true}); |
| 326 | }} |
| 327 | |
| 328 | // Listen for the clearing of the current output cell |
| 329 | var outputEl = gd.closest('.output'); |
| 330 | if (outputEl) {{ |
| 331 | x.observe(outputEl, {childList: true}); |
| 332 | }} |
| 333 | """ |
| 334 | ] |
| 335 | |
| 336 | # Add user defined post script |
| 337 | if self.post_script: |
| 338 | if not isinstance(self.post_script, (list, tuple)): |
| 339 | post_script.append(self.post_script) |
| 340 | else: |
| 341 | post_script.extend(self.post_script) |
| 342 | |
| 343 | html = to_html( |
| 344 | fig_dict, |
| 345 | config=self.config, |
| 346 | auto_play=self.auto_play, |
| 347 | include_plotlyjs=self.include_plotlyjs, |
| 348 | include_mathjax=include_mathjax, |
| 349 | post_script=post_script, |
| 350 | full_html=self.full_html, |
| 351 | animation_opts=self.animation_opts, |
| 352 | default_width="100%", |
| 353 | default_height=525, |
| 354 | validate=False, |
| 355 | ) |
| 356 | |
| 357 | return {"text/html": html} |
| 358 | |
| 359 | |
| 360 | class NotebookRenderer(HtmlRenderer): |