I use this template to pass values from Flask to the frontend (chart)
@app.route("/allusers", methods=["POST", "GET"])
def allusers():
if request.method == "GET":
user_collection = mongo.db.users
df = pd.DataFrame(list(user_collection.find()))
df["price"] = df["price"].apply(pd.to_numeric)
result = round(df["price"].mean(), 2)
return render_template(
"/result.html",
result=result,
nrow=df.shape[0],
series=list(df["price"]),
description=list(df["description"]),
)
The price values give work perfectly fine, but the strings which I console.log
{{description}}
in the HTML gives me this:
['dfsf', 'dfsfggg', 'dfsf', 'dfsdfs']
console.log("{{description}}");
This gives me:
['dfsf', 'dfsfggg', 'dfsf', 'dfsdfs']
I have to use it in JS like this:
<script>
$(function() {
console.log("{{description}}");
$("#container").highcharts({
chart: {
animation: {
duration: 1000
}
},
xAxis: {
categories: ["Bla", "bla2", "bla3", "Bla4"]
},
series: [
{
data: JSON.parse("{{series}}"),
name: "Price"
}
]
});
});
</script>
The strings seem to get converted somehow. Can anyone tell me what happens there and what I can do about it?
JSON.parse("["dfsf", "dfsfggg", "dfsf", "dfsdfs"]");is the resultJSON.parse('{{ series | tojson }}'), like how it is in the answer linked.