一直得到一个值错误,太多的值无法解包(1)

  • 本文关键字:太多 一个 错误 python
  • 更新时间 :
  • 英文 :


当尝试使用json将数据库表中的数据转换为JSchart时,不断得到此错误。不知道该怎么处理这个错误,让我闲了几个小时。我还想指出的是,当给出原始数据时,该图不显示饼状图。我在上面也有一个画布

@add_dash.route('/chart')
def chart():
income_vs_expenses = db.session.query(db.func.sum(IncomeExpenses.amount),
IncomeExpenses.type).group_by(IncomeExpenses.type).order_by(IncomeExpenses.type).all()
income_expenses = []
for total_amount, in income_vs_expenses:
income_expenses.append(total.amount)

return render_template("chart.html", user=current_user, income_vs_expenses = json.dumps(income_expenses))

我javascipt

{% block javascript %}
<script> 
let income_expenses = JSON.parse({{ income_vs_expenses| tojson }})
let income_vs_expense_chart = new Chart(income_vs_expenses, {
type: 'pie',
data: {
labels: ['expense', 'income'],
datasets: [{
label: "Income Vs Expenses",
data: income_expenses,
backgroundColor: ['#5DA5DA ', '#FAA43A', '#60BD68',
'#B276B2', '#E16851', '#FB8267'],
borderWidth: 1,
hoverBorderColor: "black",
hoverBorderWidth: 2,
hoverBackgroundColor: 'rgba(154, 245, 140)',
pointHoverRadius: 5
}],
},
options: {
title: {
display: true,
text: "Income Vs Expenses",
fontSize: 20,
},
legend: {
position: "right",
labels: {
fontColor: "blue"
},
display: true,
},
elements: {
hitRadius: 3,
}
}
})
</script>


{% endblock %}

完整回溯

File "C:UsersCbcbcAppDataLocalProgramsPythonPython310libsite-packagesflaskapp.py", line 2095, in __call__
def __call__(self, environ: dict, start_response: t.Callable) -> t.Any:
"""The WSGI server calls the Flask application object as theOpen an interactive python shell in this frame
WSGI application. This calls :meth:`wsgi_app`, which can be
wrapped to apply middleware.
"""
return self.wsgi_app(environ, start_response)
File "C:UsersCbcbcAppDataLocalProgramsPythonPython310libsite-packagesflaskapp.py", line 2080, in wsgi_app
try:
ctx.push()
response = self.full_dispatch_request()
except Exception as e:
error = e
response = self.handle_exception(e)
except:  # noqa: B001
error = sys.exc_info()[1]
raise
return response(environ, start_response)
finally:
File "C:UsersCbcbcAppDataLocalProgramsPythonPython310libsite-packagesflaskapp.py", line 2077, in wsgi_app
ctx = self.request_context(environ)
error: t.Optional[BaseException] = None
try:
try:
ctx.push()
response = self.full_dispatch_request()
except Exception as e:
error = e
response = self.handle_exception(e)
except:  # noqa: B001
error = sys.exc_info()[1]
File "C:UsersCbcbcAppDataLocalProgramsPythonPython310libsite-packagesflaskapp.py", line 1525, in full_dispatch_request
request_started.send(self)
rv = self.preprocess_request()
if rv is None:
rv = self.dispatch_request()
except Exception as e:
rv = self.handle_user_exception(e)
return self.finalize_request(rv)

def finalize_request(
self,
rv: t.Union[ResponseReturnValue, HTTPException],
File "C:UsersCbcbcAppDataLocalProgramsPythonPython310libsite-packagesflaskapp.py", line 1523, in full_dispatch_request
self.try_trigger_before_first_request_functions()
try:
request_started.send(self)
rv = self.preprocess_request()
if rv is None:
rv = self.dispatch_request()
except Exception as e:
rv = self.handle_user_exception(e)
return self.finalize_request(rv)

def finalize_request(
File "C:UsersCbcbcAppDataLocalProgramsPythonPython310libsite-packagesflaskapp.py", line 1509, in dispatch_request
getattr(rule, "provide_automatic_options", False)
and req.method == "OPTIONS"
):
return self.make_default_options_response()
# otherwise dispatch to the handler for that endpoint
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)

def full_dispatch_request(self) -> Response:
"""Dispatches the request and on top of that performs request
pre and postprocessing as well as HTTP exception catching and
error handling.
File "C:UsersCbcbcDesktopStChwebsiteadd_dash.py", line 56, in chart
income_vs_expenses = db.session.query(db.func.sum(IncomeExpenses.amount),
IncomeExpenses.type).group_by(IncomeExpenses.type).order_by(IncomeExpenses.type).all()

income_expenses = []
for total_amount in income_vs_expenses:
income_expenses.append(total.amount)


return render_template("chart.html", user=current_user, income_vs_expenses = json.dumps(income_expenses))

必须去掉for循环中total_amount后面的逗号

相关内容

最新更新