Skip to content

Commit

Permalink
Merge pull request #226 from moj-analytical-services/offline_charts
Browse files Browse the repository at this point in the history
save offline charts
  • Loading branch information
RobinL committed Nov 3, 2021
2 parents d50e4ac + cf5ae2e commit e03bb4a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
21 changes: 21 additions & 0 deletions splink/charts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pkgutil
import json
import os

altair_installed = True
try:
Expand Down Expand Up @@ -49,3 +50,23 @@ def _make_json(chart_or_dict):
return chart_or_dict.to_json(indent=None)
else:
return json.dumps(chart_or_dict)


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

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")

fmt_dict = _load_external_libs()

fmt_dict["mychart"] = _make_json(altair_chart)

with open(filename, "w") as f:
f.write(template.format(**fmt_dict))
31 changes: 31 additions & 0 deletions splink/files/templates/single_chart_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8"/>
<script>{vega}</script>
<script>{vega-lite}</script>
<script>{vega-embed}</script>
<style>
.chart-wrapper {{
height: auto !important
}}
</style>
</head>

<body>


<div id="mychart"></div>




<script type="text/javascript">

vegaEmbed('#mychart', {mychart}).catch(console.error);

</script>
</body>

</html>

0 comments on commit e03bb4a

Please sign in to comment.