Skip to content
Open
Show file tree
Hide file tree
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
11 changes: 8 additions & 3 deletions sphinx_plotly_directive/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ def save_plotly_figure(fig, path):
>>> path = tempfile.NamedTemporaryFile(suffix=".html").name
>>> save_plotly_figure(fig, path)
"""
fig_html = plotly.offline.plot(fig, output_type="div", include_plotlyjs="cdn", auto_open=False)
with open(path, "w") as f:
f.write(fig_html)
ext = path.split(".")[-1]
if ext in ["htm", "html"]:
fig_html = plotly.offline.plot(fig, output_type="div",
include_plotlyjs="cdn", auto_open=False)
with open(path, "w") as f:
f.write(fig_html)
else:
fig.write_image(path)


def assign_last_line_into_variable(code, variable_name):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def tset_save_plotly_figure(tmpdir):
save_plotly_figure(fig, out_path)
assert os.path.exists(out_path)

out_path = os.path.join(tmpdir.strpath, "fig.png")
save_plotly_figure(fig, out_path)
assert os.path.exists(out_path)

out_path = os.path.join(tmpdir.strpath, "fig.pdf")
save_plotly_figure(fig, out_path)
assert os.path.exists(out_path)


def test_assign_last_line_into_variable():
code = """
Expand Down