-1

Trying to send an email with Python build table. However, I'm experiencing the following error when I output the dataframe as a table. What is the issue here?

from pretty_html_table import build_table

riskDataFrame  = pd.read_sql(query, sql_conn)   
outputTable = build_table(riskDataFrame, 'blue_light')

Error is Unknown Error local variable 'body' referenced before assignment

File "C:\Python-3.9\lib\site-packages\pretty_html_table\pretty_html_table.py", line 141, in build_table body = body + """

""" UnboundLocalError: local variable 'body' referenced before assignment
nsivakr
  • 1,565
  • 2
  • 25
  • 46
  • Show the full traceback of the error as properly formatted text in the question. – Michael Butscher Dec 05 '20 at 22:21
  • Please provide the expected [MRE](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. – Prune Dec 05 '20 at 22:22
  • I'm not seeing body referenced in your snippet – hd1 Dec 05 '20 at 22:25
  • Adding the code that exactly shows the area, where the error message is. – nsivakr Dec 05 '20 at 22:27
  • 1
    According to build_table() source code (https://github.com/sbi-rviot/ph_table/blob/master/pretty_html_table/pretty_html_table.py) body variable is created inside loop. This means if you pass an empty df, the method will not enter the loop and body is undefined. Check if your df is empty before passing it to build_table() – Harsh Dec 05 '20 at 22:37
  • @Harsh, Please add the above as the answer and I'll mark it as accepted. – nsivakr Dec 06 '20 at 21:08
  • Hello! I am the developer of this library, we added a patch that will return empty when the table is empty or undefined. I hope it helps! – RenauV Apr 19 '21 at 14:48

1 Answers1

1

According to build_table() source code, variable body is created inside a loop. The loop is entered if the DataFrame passed in the argument is not empty

This means if you pass an empty DataFrame object in the parameter, the method build_table() never enters the loop and thus body remains undefined.

Check if your df is empty before passing it to build_table()

Harsh
  • 372
  • 2
  • 15