ParamsFromFile input a Table

Hi,

My app is used to create a report for multiple images. The strucutre of my app is:

app
├── image_entity
─├── __init__.py
─├── controller.py
├── report_editor
─├── __init__.py
─├── controller.py
─├── parametrization.py
─└── logic.py

Before entering my app you upload these images as entities.This is done because after creating a report I want these images to be deleted. Within image_entity.controller there is this function store some infomation from the images:

image_table = []
image_options = []

class ImageController(ViktorController):
    ''' Controller class for the image entity '''
    label = "image_entity"

    @ParamsFromFile(file_types=['.png', '.jpg', '.jpeg'])
    def process_file(self, file: File, **kwargs) -> dict:
        image_dict = {}
        image_dict['image_name'] = kwargs['entity_name']
        image_dict['description'] = ''
        image_table.append(image_dict)
        image_options.append(kwargs['entity_name'])
        parent_entity = API().get_entity(kwargs['entity_id']).parent()
        parent_entity.last_saved_params['create_report_step']['report_table'] = image_table
        
        return {"create_report_step":{"report_table": image_table}}

I want to use the infomation from these images to set the values of the parameters within the report_editor. Within report_editor.parametrization I have this:

from ..image_entity.controller import image_table,image_options

def image_selection(**kwargs):
    return image_options

def image_selection(**kwargs):
    return image_table

class Parametrization(Parametrization):
    create_report_step.image_selection = OptionField('View image', options=image_selection)
    create_report_step.report_table = Table('Image descriptions',default=image_table)

Filling in the OptionField with the image_selection options works, however filling in the table does not because of this error:
image

I’ve tried to make the input JSON serializable by doing this:

create_report_step.report_table = Table('Image descriptions',default=list(json.dumps(image_selection()))) 

But this still doesn’t work.

Is there anyway to populate the table from the params gathered from the entities(images)?

Thanks,
Charlie

Hi Charlie, I admire your creativity here!

I have two points of advice.

  1. The application in production is running stateless, I don’t expect storing data on a global (e.g. image_options) is working in production. This is behaving differently on Windows in order to keep application job calls quick during development. I would advice to use a ChildEntityOptionField instead.
  2. It is not possible to set a dynamic default (Make the default depending on some value), which results in the ... function is not JSON ... error. However, when I look at your code it seems that you do not need to set a default, since you are updating the table during the upload of a child entity right?

The table is to be filled out by the user. One column is the image name, the other is the description to the image, which is then used to put under the images in the report.

Ideally, when the entities are uploaded I would like to get all the entity names so I can pre populate the table ‘names’ column.

My current solution is using a button. When pressed it called a setparams function and populates the table with the image names.

Ah I see the issue, you can change this:

- parent_entity.last_saved_params['create_report_step']['report_table'] = image_table
+ parent_params = parent_entity.last_saved_params  # retrieve current params
+ parent_params['create_report_step']['report_table'].append(image_dict)  # add row to table
+ parent_entity.set_params(parent_params)  # update with new params
1 Like