I have a Pandas dataframe and trying to return the column headers into WTForm dynamic list on a Flask application. However when I try to submit the HTML form, I get a validation error. Here is my form class error.
data = pd.read_excel("sampledata.xlsx")
col_headers = list(data.columns)
col_list = list(col_headers)
dic_list = {i : col_list[i] for i in range(0, len(col_list))}
class StatementForm(FlaskForm):
date = SelectField('Date', choices = [(key, dic_list[key]) for key in dic_list])
I have tested the HTML form by manually inputting a tuple in the choices list and it worked. Apparently the problem is in the 'date' parameter and the choices.
Your help in reviwing the code is most apreciated.
str(key)in your choices comprehension. In my experience the SelectField does not like integer keys.choices = [(str(x), y) for (x, y) in list(enumerate(data.columns))]and do it all in one line. Let me know if this worksform = StatementForm()date = form.date.datadate returns an index number