Skip to content

Commit

Permalink
Merge pull request #233 from moj-analytical-services/improve_chart_msg
Browse files Browse the repository at this point in the history
improve offline chart
  • Loading branch information
RobinL committed Nov 7, 2021
2 parents 1b9717b + c8bab6f commit b6e0dd3
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions splink/charts.py
Original file line number Diff line number Diff line change
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 b6e0dd3

Please sign in to comment.