Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple plots in iframe renderer #3123

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions packages/python/plotly/plotly/io/_base_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,10 @@ class IFrameRenderer(MimetypeRenderer):
Note that the HTML files in `iframe_figures/` are numbered according to
the IPython cell execution count and so they will start being overwritten
each time the kernel is restarted. This directory may be deleted whenever
the kernel is restarted and it will be automatically recreated.
the kernel is restarted and it will be automatically recreated. If a cell
contains multiple plots, a second index is appended to the file name. For
example, figure_8_2.html would be the third figure in the eighth cell
executed.

mime type: 'text/html'
"""
Expand All @@ -545,6 +548,8 @@ def __init__(
self.animation_opts = animation_opts
self.include_plotlyjs = include_plotlyjs
self.html_directory = html_directory
self.last_focus_cell = None
self.last_focus_cell_output_ct = 1

def to_mimebundle(self, fig_dict):
from plotly.io import write_html
Expand Down Expand Up @@ -608,8 +613,17 @@ def to_mimebundle(self, fig_dict):
def build_filename(self):
ip = IPython.get_ipython() if IPython else None
cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
filename = "{dirname}/figure_{cell_number}.html".format(
dirname=self.html_directory, cell_number=cell_number
if self.last_focus_cell == cell_number:
output_ct_suffix = f"_{self.last_focus_cell_output_ct}"
self.last_focus_cell_output_ct += 1
else:
self.last_focus_cell = cell_number
self.last_focus_cell_output_ct = 1
output_ct_suffix = ""
filename = "{dirname}/figure_{cell_number}{output_ct_suffix}.html".format(
dirname=self.html_directory,
cell_number=cell_number,
output_ct_suffix=output_ct_suffix,
)
return filename

Expand Down