Home > Enterprise >  ValueError: too many values to unpack (expected 2) in templates/results.html:13: ValueError
ValueError: too many values to unpack (expected 2) in templates/results.html:13: ValueError

Time:02-03

I am working on a Text Classification NLP problem. I have trained the model and using the trained model for classification. Prediction data is an ordered dictionary on which predictions needs to be done.

Prediction Output:

OrderedDict([('Subject: fwd : next tuesday at ', 'SPAM'), ('Subject: visit may 4 th  vince ', 'SPAM'), ('Subject: enjoy media ( ejym ) e', 'SPAM')])

I am saving it as json file and then loading it in a display function for rendering using render_template

with open('labels.json', 'w') as fp:
 json.dump(result,fp)
f = open('labels.json')
predict = json.load(f)
return render_template("result.html",predictions=predict)

Template not rendering with the json values: Error-

ValueError: too many values to unpack (expected 2)  in templates

Test case Failure:

    {% for email, result in predictions %}
    E   ValueError: too many values to unpack (expected 2)
    code/templates/results.html:13: ValueError

labels.json

{"Subject: fwd : next tuesday at ": "SPAM", "Subject: visit may 4 th  vince ": "SPAM", "Subject: enjoy media ( ejym ) e": "SPAM"}

CodePudding user response:

You need to call .items() on the dict to create a list of tuples of (email, result):

{% for email, result in predictions.items() %}

Edit: Since you mentioned the template files are fixed, you can also alter the template variable and apply .items() there:

return render_template("result.html",predictions=predict.items())
  •  Tags:  
  • Related