Export Altair to HTML/Json
You can call .to_json
and .to_html
on any chart that you've defined to get the json or html out.
days = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun']
p1 = (alt.Chart(plot_df)
.mark_line()
.encode(x='date', y='births', color=alt.Color('wday', sort=days))
.properties(width=125, height=125)
.interactive())
print(p1.to_html())
print(p1.to_json())
You can also save the json file to disk if you liked to be served as a file by a server later.
import pathlib
pathlib.Path("chart-schema.json").write_text(p1.to_json())
Demo
Below you'll see an example of the hosted chart.
We're using a little trick to help us host this via the justcharts project. Here's the code that we used:
<script src="https://cdn.jsdelivr.net/npm/vega@5"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@5"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-embed@6"></script>
<script src="https://cdn.jsdelivr.net/gh/koaning/justcharts/justcharts.js"></script>
<vegachart schema-url="chart-schema.json"></vegachart>
If this sounds interesting, you can learn more about it on the justcharts announcement page.