Update excel_to_base64 to always close file handles
@@ -6,7 +6,7 @@ import pandas as pd
def dataframe_to_base64(df: pd.DataFrame) -> str:
- """Converts a dataframe to a base64-encoded CSV representation of that data.
+ """Converts a dataframe into csv base64-encoded data.
This is useful for building datauris for use to download the data in the browser.
@@ -18,7 +18,7 @@ def dataframe_to_base64(df: pd.DataFrame) -> str:
return b64
-def excel_to_base64(str_excel_filename) -> str:
- data = open(str_excel_filename, 'rb').read()
- b64 = b64encode(data).decode()
- return b64
+def excel_to_base64(filename: str) -> str:
+ """Converts an excel document into base64-encoded data."""
+ with open(filename, 'rb') as fin:
+ return b64encode(fin.read()).decode()
