We created an app and want to fill in fields with data read from an Excel. This works as expected for TextFields, BooleanFields, NumberFields etc.
For a Table, I expected to be able to use something along the lines of:
params = {
“table_content”: [{“col_1”: “entry1”}, {“col_1”: “entry2”}]
}
But this doesn’t seem to work. Any recommendations would be appreciated.
This seems indeed right.
Example Code
class Parametrization(vkt.Parametrization):
# Add a Table field
table_content = vkt.Table("Data Table")
table_content.col_1 = vkt.NumberField("Number")
table_content.col_2 = vkt.TextField("Text")
# Add a SetParamsButton
set_table_data = vkt.SetParamsButton("Set Table Data", method="set_table_data_from_excel")
class Controller(vkt.Controller):
label = "My Entity Type"
parametrization = Parametrization
def set_table_data_from_excel(self, params, **kwargs):
"""Set the Table data from a hardcoded dictionary."""
# Hardcoded dictionary with sample data
hardcoded_data = [
{"col_1": 1, "col_2": "t1"},
{"col_1": 2, "col_2": "t2"},
{"col_1": 3, "col_2": "t3"},
{"col_1": 4, "col_2": "t4"},
{"col_1": 5, "col_2": "t5"}
]
# Set the Table data
return vkt.SetParamsResult({"table_content": hardcoded_data})
Please check if the spelling is exact, and table_content is directly accessible in params, and is not nested under Steps/Pages or Sections!
Hopefully that helps!
Thank you @rvandijk! Nice to have a complete example.
The error was indeed related to too much nesting.
Nesting can be done as well of course. It would just be:
return vkt.SetParamsResult({"page": {"section":{table_content": hardcoded_data}}})
In this case the dicts become cluggy, so it becomes easier to do:
params.page.section.table_content = hardcoded_data
return vkt.SetParamsResult(params)