Date format using gviz_api.py

I am using gviz_api (google-visualization-python) to create some line charts. http://code.google.com/p/google-visualization-python/

I edited a sample diagram taken from Google documentation.
However, I'm not sure how to pass a date to a DataTable

Here is an edited example that I worked with. https://gist.github.com/3941946

In this code, I had a question about

# Creating the data description = {"year": ("string", "Year"), "sales": ("number", "Sales"), "expenses": ("number", "Expenses")} data = [{"year": '2004', "sales": 1000, "expenses": 300}, {"year": '2005', "sales": 1200, "expenses": 400}, {"year": '2006', "sales": 1300, "expenses": 500}, {"year": '2007', "sales": 1400, "expenses": 600}, {"year": '2008', "sales": 1500, "expenses": 800}] # Loading it into gviz_api.DataTable data_table = gviz_api.DataTable(description) data_table.LoadData(data) 

How can I load a date in a DataTable using gviz_api?

The google documentation describes how to create a new Date () using javascript, however I would like to continue using gviz_api.py.

Notes from Google documentation from https://developers.google.com/chart/interactive/docs/dev/implementing_data_source#jsondatatable

* JSON modifications The Google support libraries and all requests sent to Google return a slightly non-standard version of JSON / JSONP. If you do not parse the returned code yourself, it does not matter to you. The visualization API client supports both standard and modified versions of JSON. The following is a brief overview of the differences:

JSON does not support JavaScript Date values ​​(for example, "new Date (2008,1,28,0,31,26)", the implementation of the API does. However, the API now supports a custom JSON representation of the date as a string in the following format: Date (year , month, day [, hour, minute, second [, millisecond]]), where everything the day after tomorrow is optional and the months are based on zero.

JSON uses double quotes for dictionary keys; API implementations use unmentioned keys.

JSON requires double quotes around string values; the API implementation uses single quotes. *

+4
source share
1 answer

In fact, you can create dates in the same way as in standard Python - the API handles the conversion to JS for you. All you have to do is import datetime at the beginning of your script, change the column type for year from string to date , and then create the dates, as in standard Python:

 # This is the first modification - importing the library import datetime import gviz_api # page_template stays the same # ... def main(): # Creating the data # Here we change the type of column "year" to "date" description = {"year": ("date", "Year"), "sales": ("number", "Sales"), "expenses": ("number", "Expenses")} # Here we switch out the string dates with an actual Python datetime.date # The conversion happens in the the subsequent functions, giving you a # date that is usable in the JS data = [{"year": datetime.date(2007,3,7), "sales": 1000, "expenses": 300}, {"year": datetime.date(2009,6,11), "sales": 1200, "expenses": 400}, {"year": datetime.date(2009,3,1), "sales": 1300, "expenses": 500}, {"year": datetime.date(2010,8,6), "sales": 1401, "expenses": 600}, {"year": datetime.date(2011,7,13), "sales": 1500, "expenses": 800}] # Loading it into gviz_api.DataTable data_table = gviz_api.DataTable(description) data_table.LoadData(data) # Creating a JavaScript code string jscode = data_table.ToJSCode("jscode_data", columns_order=("year", "sales", "expenses"), order_by="year") # Creating a JSon string json = data_table.ToJSon(columns_order=("year", "sales", "expenses"), order_by="year") # Putting the JS code and JSon string into the template print "Content-type: text/html" print print page_template % vars() if __name__ == '__main__': main() 
+5
source

Source: https://habr.com/ru/post/1441672/


All Articles