Hey guys,
first of all Id like to appreciate how great Streamlit is.
I fell in love with it the moment i started using it.
On my current Project im plotting several Graphs on my Page, when trying to Download them my PDF is empty.
In order to Download the Report im using PyFPDF, also im doing the plotting with seaborn.
Id appreciate every help, im kinda stuck on this problem and dont know how to continue.
Thank you very much
import streamlit as st
import matplotlib.pyplot as plt
from fpdf import FPDF
import base64
import numpy as np
import pandas as pd
import seaborn as sns
from tempfile import NamedTemporaryFile
st.set_option('deprecation.showPyplotGlobalUse', False)
def create_download_link(val, filename):
b64 = base64.b64encode(val) # val looks like b'...'
return f'<a href="data:application/octet-stream;base64,{b64.decode()}" download="{filename}.pdf">Download file</a>'
fields = ['data_age', 'data_dwell', 'data_gender', 'data_views']
gt = pd.read_csv('TestErgebnisse2.csv', usecols=fields, header=0, sep=";") #Groundtruth Dataset
df = pd.read_csv('TestErgebnisse.csv', usecols=fields, sep=";") #Test Data
figs = []
plt.figure(1)
st.write("Ground Truth Data")
gt['Data']="Korrekte Daten"
df['Data']="Ermittelte Daten"
fig=pd.concat([gt,df]) #combine
sns.barplot(x='data_age',y='data_age',data=fig,hue='Data')
st.pyplot()
plt.figure(2)
gt['Data']="Korrekte Daten"
df['Data']="Ermittelte Daten"
res=pd.concat([gt,df]) #combine
sns.lineplot(x='data_age',y='data_views',data=res,hue='Data')
st.pyplot()
plt.figure(3)
gt['Data']="Korrekte Daten"
df['Data']="Ermittelte Daten"
res=pd.concat([gt,df]) #combine
sns.boxplot(x='data_gender',y='data_age',data=res,hue='Data')
st.pyplot()
export_as_pdf = st.button("Download Report") # Download Button
if export_as_pdf:
pdf = FPDF()
for fig in figs:
pdf.add_page()
with NamedTemporaryFile(delete=False, suffix=".png") as tmpfile:
fig.savefig(tmpfile.name)
pdf.image(tmpfile.name)
html = create_download_link(pdf.output(dest="S").encode("latin-1"), "testfile")
st.markdown(html, unsafe_allow_html=True)