Dear KNIMErs and developers,
I am exploring the new python based KNIME extension. I need to generate the fields (parameters) in node configuration dialogue from a JSON file. I’m first parsing the JSON attribute and its values into a python list and then based on type of value, generate corresponding knext.parameter. This is how it is done in my code example below.
- Parse the JSON attribute and value as lists.
import json
keys = []
values = []
with open('config_json.json', "r") as read_file:
data = json.load(read_file)
for key, value in data.items():
keys.append(key)
values.append(value)
- Depending upon the attribute value type generate knext parameter. e.g.
import knime.extension as knext
if isinstance(value, str):
aa = knext.StringParameter(key[idx], key[idx] ,value)
if isinstance(value, float):
bb = knext.DoubleParameter(key[idx], key[idx] ,value)
This works fine and configuration dialogue is rendered with correct parameters. However, Since content of JSON file can vary I have no way of knowing how many parameter object I would need. I thought of using dictionary to solve this issue. Something like below.
import knime.extension as knext
variable_dict = {}
for k, v in enumerate(value):
if isinstance(v, str):
variable_dict[k] = knext.StringParameter(k, k ,v)
if isinstance(v, float):
variable_dict[k] = knext.DoubleParameter(k, k,v)
But this doesn’t work and parameters fields are not rendered in configuration dialogue.
I’m looking for suggestion, if this is something feasible or how to do it?
Thanks.
Bilal