I have created a table using gviz_api. The table contains columns composed of string values and number values. The api automatically chooses the order in which columns render themselves; however, I would like the rendering to happen in the same order as I provide the description.
To create the table, I passed in a description, which is a dictionary and the data, which is a list of dictionaries for each row. I think that because the description is a dictionary, ordering is not explicit and so can be chosen at random. Is there a way to force the api to use the order of the columns that I specify in the description or any other way?
Here’s the example description and data code that I have:
descr = {'field1': ('string', 'Field1'),
'field2': ('number', 'Field2'),
'field3': ('string', 'Field3'),
'field4': ('string', 'Field3')}
data = [{'field1': 'value1-1', 'field2': value1-2, 'field3': 'value1-3', 'field4': 'value1-4},
{'field1': 'value2-1', 'field2': value2-2, 'field3': 'value2-3', 'field4': 'value2-4}]
data_table = gviz_api.DataTable(description)
data_table.LoadData(data)
code = data_table.ToJSCode(stats_type_name + '_data')
And the order in which things actually get rendered:
field3 field2 field1 field4
value1-3 value1-2 value1-1 value1-4
value2-3 value2-2 value2-1 value2-4
The order in which I would like things to appear is the same as I specify in the description:
field1 field2 field3 field4
value1-1 value1-2 value1-3 value1-4
value2-1 value2-2 value2-3 value2-4
So I kind of found a hack solution. From observation, I noticed that gviz_api uses alphabetical ordering when adding columns to the table. My column names had different labels (i.e. second entry in (‘string’, ‘label’) that were not in alphabetical order. To fix it, I prepended the label name with a number (i.e. (‘string’, ‘1label’)) to ensure the order of the columns to be how I want them to.
If anyone else finds a less hackier solution, please let me know.