javascript - Struggling with Pandas to JSON in Django -
I have the following panda dataframe that is built in the DEGENGO VIEW:
Real Ticks 26-Sep-14 20 29-Sep-14 40 15-Oct-14 -10 20-Oct-14 5
The data is read from a CSV file using pundas , Then changed to a Jason object, and then the DJ ango was sent to the template:
df = pd.read_csv ('article / DailyStats.csv', usecols = ['date',] 'Real tick']) Df.to_json () Returns render_to_response ('article / test.html', {'df': df})
Now where do I get the problem Or try getting when I used to come every JSON object in my template:
& lt; Script type = "text / javascript" & gt; $ .getJSON ({{df}}, function (data) {// do something}) & lt; / Script & gt;
I get an error in my Chrome browser console: Unwanted syntax error: Unexpected identifier
If I look at sources in the browser, I see the following:
& lt; Script type = "text / javascript" & gt; $ .getJSON (actual tick date dated 26-Sep-14 20 29-Sep-14 40 15-Oct-14 -10 20-Oct-14 5, Function (Data) {}) & lt; / Script & gt;
I want all the keys of a JSON (i.e. date), and with the list of values (i.e. "real tick"). I'm guessing that there is something to do with column headings, which makes something wrong.
Eventually I want to be able to loop through Jasonson to display dates and similar values, but until this error does not occur
UPDATE: @ Thanks for @nanaal , Who said that I was not specifying the JSN object in a variable. I have done this now: df_json = df.to_json ()
But as a result Jason is not valid:
[[& amp; Quot; 26-Sep-14 & amp; ; Quot ;, 20], [& amp; Quot; 29-Sep-14 & amp; Quot ;, 40], [& amp; Quot; 15-Oct-14 & amp; Quot ;, -10], [& amp; Quot; 20-Oct-14 & amp; Quot;, 5]]
How to make a valid JSON valid?
The problem is, you json to render_to_response
Objects are not passing, just as df
as a DataFrame .
# This line is calling you at call_json, but has not assigned df.to_json () to df_json (or DF if you wish), and then render_to_response Df_json = df.to_json () Return it near
return render_to_response ('article / test.html', {'df': df_json}) and in your template, because you previously You have passed from the json object, you do not need any Ajax calls, just parse that Jason will be fine, something like that has not been tested yet Pekshit should work), now < | Wrapped with citations guided by Safe
and updated with quoted guides by @Raco Gerlich:
Comments
Post a Comment