Spaces:
Running
Running
James McCool
commited on
Commit
·
51316bf
1
Parent(s):
4d6e5f5
Refactor PDF export functionality in app.py
Browse files- Replaced the previous PDF conversion method with a new implementation that converts DataFrames to HTML before generating PDFs using pdfkit.
- Added error handling to notify users if pdfkit is not installed, ensuring a smoother user experience.
- This update enhances the PDF export capabilities of the application, providing better support for exporting data in PDF format.
app.py
CHANGED
|
@@ -9,7 +9,7 @@ import numpy as np
|
|
| 9 |
import pandas as pd
|
| 10 |
import streamlit as st
|
| 11 |
import gspread
|
| 12 |
-
import
|
| 13 |
|
| 14 |
@st.cache_resource
|
| 15 |
def init_conn():
|
|
@@ -99,7 +99,15 @@ def convert_df_to_csv(df):
|
|
| 99 |
return df.to_csv().encode('utf-8')
|
| 100 |
|
| 101 |
def convert_df_to_pdf(df):
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
matchups, overall_ms, team_frame, team_list, team_dict = init_baselines()
|
| 105 |
|
|
|
|
| 9 |
import pandas as pd
|
| 10 |
import streamlit as st
|
| 11 |
import gspread
|
| 12 |
+
import pdfkit
|
| 13 |
|
| 14 |
@st.cache_resource
|
| 15 |
def init_conn():
|
|
|
|
| 99 |
return df.to_csv().encode('utf-8')
|
| 100 |
|
| 101 |
def convert_df_to_pdf(df):
|
| 102 |
+
# Convert DataFrame to HTML first
|
| 103 |
+
html = df.to_html()
|
| 104 |
+
# Convert HTML to PDF using pdfkit
|
| 105 |
+
try:
|
| 106 |
+
pdf = pdfkit.from_string(html, False)
|
| 107 |
+
return pdf
|
| 108 |
+
except ImportError:
|
| 109 |
+
st.error("pdfkit is not installed. Please install pdfkit and wkhtmltopdf to enable PDF export.")
|
| 110 |
+
return None
|
| 111 |
|
| 112 |
matchups, overall_ms, team_frame, team_list, team_dict = init_baselines()
|
| 113 |
|