Hello everyone, I would like to download a streamlit table to pdf. Once I click on a button the table will be downloaded automatically. I really need it so please help me out
Hi Loubna! And welcome to the Streamlit forum!
Iâm not aware of any way to do this, but Devs may know!
Let me ask them and come back to you!
Best,
Charly
@ash2shukla @Charly_Wargnier @Loubna_Massaoudi
I have tried like this on the foundation of @ash2shukla ,
but result is not perfect, need some one to make it more perfect.
code:
import streamlit as st
from fpdf import FPDF
import base64
import pandas as pd
df=pd.read_excel("temp.xlsx")
st.write(df)
export_as_pdf = st.button("Export Report")
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>'
if export_as_pdf:
pdf = FPDF()
pdf.add_page()
pdf.set_xy(10,10)
pdf.set_font('Arial', 'B', 16)
temp=[]
for i in range(df.shape[0]):
for j in range(df.shape[1]):
temp.append(df.iloc[[i],[j]].values)
for i in range(df.shape[0]):
pdf.ln()
for j in range(df.shape[1]):
pdf.cell(-120)
pdf.ln()
pdf.cell(i, j, str(temp[i]),align='C')
pdf.ln()
html = create_download_link(pdf.output(dest="S").encode("latin-1"), "test")
st.markdown(html, unsafe_allow_html=True)
Hello again @Loubna_Massaoudi!
Thanks @BeyondMyself for your suggestions
So I checked with Devs and at the moment thereâs nothing available natively (good news: itâs in the work! )
Also you may want to try to go the Plotly way!
With Plotly, you can save charts and dataframes as images, which may suit what youâre after!
Let us know if that works for you and happy Streamlitinâ!
Best,
Charly
Hello plotly.graph_objects library deosnât exist, and module âmatplotlib.pyplotâ has no attribute âTableâ; Iâm still searching to export my table as pdf but to no avail
Hello your method didnât work out for me but thank you for sharing
hi @Loubna_Massaoudi. I had the same problem and the best approach I found was combining the libraries pdfkit and jinja2. There is a good article on medium: Creating PDF Reports with Python, Pdfkit, and Jinja2 Templates | by Mark Nagelberg | Towards Data Science. Baseically you desing your tables in a html-template and css, the convert your dataframe to html using jinja2 and send the html to the pdf. All design stuff such as as font size, padding, colors etc can be done with css. I have used this approach successfully in my work environment. There is also an app on heroku: PGMN waterlevels · Streamlit (pgmn-waterlevel-reports.herokuapp.com), menu item generate pdf-report
(see screenshot). The code is on github if ou are interested. I had problems there with blank pages which has to do with the version of html2pdf installed on the server. If you need it for your work locally or in your network, you can use the most recent version and you donât have this problem
.
Hi, thanks, I can not open the url:
https://towardsdatascience.com/creating-pdf-reports-with-python-pdfkit-and-jinja2-templates-64a89158fa2d
could you please share an example code of dataframe to pdf at here?
thank you.
The example code would be my waterlevel report generator app shown above published here. Checkout the pgmn_report_generator.py module, all the logic is there. You can also try to contact the author Mark Nagelberg to send you a copy of the article.
Plotly charts/tables can be exported as images, not pdf. Besides, displaying these tables should be doable via:
st.plotly_chart(data)
That said, @godot63âs hack seems even better!
Best,
Charly
Great hack @godot63!
Hello thank you for your help. Itâs much appreciated. However the link to the app on heroku didnât work for me at all. Hope you can help with that
here we go again, I think last time it did not include the https-prefix.
PGMN waterlevels · Streamlit (https://pgmn-waterlevel-reports.herokuapp.com)
Hello thank you for your help again, the app is very helpful I was able to create a html file and convert it into pdf. However I get an error while displaying the html file inside the streamlit app âmodule âtoolsâ has no attribute âget_base64_encoded_imageâ Specifically in this code of the file generator app
base64_pdf = tools.get_base64_encoded_image(PDF_TARGET_FILE)
pdf_display = fââ
st.markdown(pdf_display, unsafe_allow_html=True)
st.markdown(tools.get_binary_file_downloader_html(PDF_TARGET_FILE), unsafe_allow_html=True)
I really appreciate if you could help me
hi @Loubna_Massaoudi
did you import the tools module in the file that has the tools.get_base64_encoded_image
command? also tools itslef must import base64. At this point in the code, you should already have created the pdf file, the command above is just to preview the pdf. Can you open the pdf file outside streamlit?
Hello, thank you for your answer I figure out a way to export the dataframe to a pdf file because of your help, thank you. Now I need to deploy the app to my boss taking in mind that he knows nothing about coding in general. I liked the link that you shared with me about the app. Now if you can help me how can I do it for my app to give my boss just a link he can enter and use the app
sorry @Loubna_Massaoudi , I see your question just now, could you resolve it? basically you have to either run the app on a server or install it locally on his machine. I can explain further, if this has not been resolved yet.
hi @godot63,
picking on this topic, is there a way we can create repeating pages pdfs using this ?
basically i have a large table of data that i want to print to pdf with repeating header and footer , any help or guidance is appreciated
hi @sheneeb I have not done that but got an idea how to approach it. I will test it first and post the solution in a few days, if I find one. Just to confirm: are talking about header and footer in the table or the page? normally the answer to such a question is: both, probably header and footer on the page and repeating table header-row for the table? but if you can clarify, please do. if you found a solution to this in the meantime please let me know.
hi @sheneeb. I made an example app and have pusblished it here). Basically you have to define the number of lines per page and the apps insert page breaks and table headers for the next table as soon as the row number is reached. It works on the happy path, but I am sure you can take it from there. Hope this helps.