@@ -1867,7 +1867,17 @@ def savefig(
18671867 layers : list | None = None ,
18681868 layer_by_layer : bool = False ,
18691869 verbose : bool = False ,
1870+ include_plotlyjs : bool | str = True ,
18701871 ):
1872+ """Render and save the canvas.
1873+
1874+ ``include_plotlyjs`` only applies to the Plotly backend when saving
1875+ to an ``.html``/``.htm`` file (see ``fig.write_html`` in the Plotly
1876+ docs). It defaults to ``True``, which bundles plotly.js into the
1877+ file so it works offline. Pass ``"cdn"`` to instead reference
1878+ plotly.js from a CDN, producing a much smaller file that requires
1879+ network access to render.
1880+ """
18711881 filename_no_extension , extension = os .path .splitext (filename )
18721882 if backend == "matplotlib" :
18731883 if layer_by_layer :
@@ -1942,7 +1952,9 @@ def savefig(
19421952 savefig = False ,
19431953 layers = layers ,
19441954 )
1945- self ._save_plotly (fig , full_filepath )
1955+ self ._save_plotly (
1956+ fig , full_filepath , include_plotlyjs = include_plotlyjs
1957+ )
19461958 if verbose :
19471959 print (f"Saved { full_filepath } " )
19481960 else :
@@ -1956,7 +1968,7 @@ def savefig(
19561968 savefig = False ,
19571969 layers = layers ,
19581970 )
1959- self ._save_plotly (fig , full_filepath )
1971+ self ._save_plotly (fig , full_filepath , include_plotlyjs = include_plotlyjs )
19601972 if verbose :
19611973 print (f"Saved { full_filepath } " )
19621974 elif backend == "tikzfigure" :
@@ -3037,11 +3049,13 @@ def plot_plotly(
30373049 self ._plotly_fig = fig
30383050 return fig
30393051
3040- def _save_plotly (self , fig , filename : str ) -> None :
3052+ def _save_plotly (
3053+ self , fig , filename : str , include_plotlyjs : bool | str = True
3054+ ) -> None :
30413055 _ , extension = os .path .splitext (filename )
30423056 extension = extension .lower ()
30433057 if extension in {".html" , ".htm" }:
3044- fig .write_html (filename )
3058+ fig .write_html (filename , include_plotlyjs = include_plotlyjs )
30453059 return
30463060 try :
30473061 fig .write_image (filename )
@@ -3051,6 +3065,33 @@ def _save_plotly(self, fig, filename: str) -> None:
30513065 "(e.g., `pip install -U kaleido`), or export to HTML instead."
30523066 ) from exc
30533067
3068+ def to_html (
3069+ self ,
3070+ layers : list | None = None ,
3071+ include_plotlyjs : bool | str = "cdn" ,
3072+ full_html : bool = True ,
3073+ verbose : bool = False ,
3074+ allow_unsupported : bool = False ,
3075+ ) -> str :
3076+ """Render the canvas with the Plotly backend and return standalone HTML.
3077+
3078+ The returned markup embeds the figure data and calls plotly.js to
3079+ render it in a browser. By default ``include_plotlyjs="cdn"``
3080+ references plotly.js from a CDN instead of bundling it, producing a
3081+ much smaller string suited to embedding in an existing page; pass
3082+ ``include_plotlyjs=True`` to inline plotly.js for offline use, as
3083+ ``savefig(..., backend="plotly")`` does. Set ``full_html=False`` to
3084+ get just the ``<div>``/``<script>`` fragment for embedding inside a
3085+ larger page (you must include plotly.js yourself in that case).
3086+ """
3087+ fig = self .plot_plotly (
3088+ show = False ,
3089+ layers = layers ,
3090+ verbose = verbose ,
3091+ allow_unsupported = allow_unsupported ,
3092+ )
3093+ return fig .to_html (include_plotlyjs = include_plotlyjs , full_html = full_html )
3094+
30543095 # Property getters
30553096
30563097 @property
0 commit comments