Save a diagram with altair saver in streamlit

I want to save a diagram with streamlit and the altair saver as png. Without streamlit the code works perfectly. But when I run it in streamlit I get the following error:

JSONDecodeError: Expecting value: line 2 column 1 (char 2)

This is my Code:

import streamlit as st
import pandas as pd
import altair as alt
import streamlit as st
from altair_saver import save

def main():
    st.title("Save Diagram as PNG")
    
    source = pd.DataFrame({
            'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
            'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
        })
        
    diagram= alt.Chart(source).mark_bar().encode(
            x='a',
            y='b'
        )
    
    st.altair_chart(diagram, use_container_width=True)
    
    save = st.button("Save diagram as PNG")
                
    if save :    
        diagram.properties(width=500).save(r'C:\Temp\diagram.png')
        

if __name__ == '__main__':
    main()

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.