Skip to content

Commit

Permalink
improve offline chart
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinL committed Nov 7, 2021
1 parent 1b9717b commit c8bab6f
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions splink/charts.py
Expand Up @@ -52,15 +52,24 @@ def _make_json(chart_or_dict):
return json.dumps(chart_or_dict)


def save_offline_chart(altair_chart, filename="my_chart.html", overwrite=False):
iframe_message = """
To view in Jupyter you can use the following command:
from IPython.display import IFrame
IFrame(src="./{filename}", width=1000, height=500)
"""


def save_offline_chart(
altair_chart, filename="my_chart.html", overwrite=False, print_msg=True
):

if os.path.isfile(filename) and not overwrite:
raise ValueError(
f"The path {filename} already exists. Please provide a different path."
)

# get altair chart as json
altair_chart_json = _make_json(altair_chart)
path = "files/templates/single_chart_template.txt"
template = pkgutil.get_data(__name__, path).decode("utf-8")

Expand All @@ -70,3 +79,7 @@ def save_offline_chart(altair_chart, filename="my_chart.html", overwrite=False):

with open(filename, "w") as f:
f.write(template.format(**fmt_dict))

if print_msg:
print(f"Chart saved to {filename}")
print(iframe_message.format(filename=filename))

0 comments on commit c8bab6f

Please sign in to comment.