Summary
I need the user to be able to download the charts that are in the exact same format as the ones that render on web interface (i.e.web from streamlit run filename.py) . However I am unable to because settings in from altair_chart() do not come through:
a) when I set the widths of the column using st.columns and set use_container_width=True in alt.chart, then download the image using vl_convert, the adjusted width does not come through when you download image, thus dimensions of downloaded image are different.
b) This is a minor issue, but if theme in altair_chart() is set to ‘streamlit’, the theme does not come through in the downloaded image. This is not a major issue as I can just switch this off.
Expected behavior:
Chart images that have been downloaded are identical to what appears on web streamlit interface
Actual behavior:
Chart images that have been downloaded are not identical because the adjusted widths (from use_container_width=True) and streamlit theme does not pull through. Priority is the widths though as theme can be switched off.
Additional information
I have also tried using altair_saver (chart.save() ) but get the same results. Note if you run code below in jupyter notebook, then run a chart object e.g. chart 1, the widths are also not reflected.
Steps to reproduce
Code to reproduce below with toy dataset
Code snippet:
import altair as alt
import pandas as pd
import streamlit as st
import vl_convert as vlc
st.write("#### Toy Example")
data = {'Fruit': ['apple', 'apple', 'apple', 'banana', 'banana', 'watermelon', 'watermelon', 'pineapple', 'pineapple'],
'Fresh': ['no', 'yes', 'no', 'yes', 'yes', 'no', 'no', 'no', 'yes']}
df = pd.DataFrame(data)
col1, col2, col3 = st.columns([1.5, 1, 1])
with col1:
chart1 = alt.Chart(
df,
title='Fruit Failures'
).mark_bar(
stroke='black'
).encode(
x=alt.X('Fruit:N', title="Category"),
y=alt.Y('count():Q', title="Subjects", axis=alt.Axis(tickMinStep=1)),
color='Fresh'
)
st.altair_chart(chart1, use_container_width=True, theme = None)
vl_spec = chart1.to_json()
png_data = vlc.vegalite_to_png(vl_spec=vl_spec, scale=2)
st.download_button(label='DownloadChart', data=png_data, mime="image/png")
with col2:
chart2 = alt.Chart(
df,
title='Fruit Madness'
).mark_bar(
stroke='black'
).encode(
x=alt.X('Fruit:N', title="Category"),
y=alt.Y('count():Q', title="Subjects", axis=alt.Axis(tickMinStep=1)),
color='Fresh'
)
st.altair_chart(chart2, use_container_width=True, theme = 'streamlit')
vl_spec = chart2.to_json()
png_data = vlc.vegalite_to_png(vl_spec=vl_spec, scale=2)
st.download_button(label='DownloadChart', data=png_data, mime="image/png")