Download zip-file consisting of figures

Hi VIKTOR team,

I would like to ask a question regarding downloading multiple figures in a zip-file, since I am working downloading images out of an stix file. For downloading the files as a zip, I have build a dictionary. However, when I want to download this zip file (code snippet #2), I encounter an error (code snippet #3). When I put a single figure as the DownloadResult (see first code snippet), my code works well.
This is my (shortened) method in the controller.

def export_stix_figure(params, **kwargs):
        # import stix files
        stix_file_resources: List[FileResource] = params.input_section.stix_files
        # import figure extension
        fig_output = params.figure_selection.field
        fig_format = fig_output # in this case ['PNG'] 
        # convert stix files into DStabilityModel()
        (...)
        # start dictionary
        fig_zip: dict = {}
        for (...):         # loop through 1) all stix-files 2) all stages
              # make fig
              fig_plotly = go.Figure()
              (...) # filled plotly figure
              fig_bytes = fig_plotly.to_image(format=fig_format)
              fig_name = f'{counter}.{fig_format.lower()}'
              counter += 1
              fig_zip[fig_name] = fig_bytes
        # example zipped files
        # DownloadResult(zipped_files={'my_file_1.txt': my_file_1, 'my_file_2.txt': my_file_2}, file_name="my_file.zip")
        print(fig_zip.keys())
        return DownloadResult(fig_bytes, fig_name)
        # return DownloadResult(zipped_files=fig_zip, file_name='my_figures.zip')
        print(fig_zip.keys())
        #  return DownloadResult(fig_bytes, fig_name)
        return DownloadResult(zipped_files=fig_zip, file_name='my_figures.zip')

The error I get is this:

dict_keys(['1.png', '2.png', '3.png', '4.png', '5.png', '6.png', '7.png', '8.png', '9.png'])
2022-07-18 14:58:57.970 ERROR   : Exception is raised
Traceback (most recent call last):
  File "viktor_connector\connector.pyx", line 400, in connector.Job.execute
  File "viktor\core.pyx", line 1788, in viktor.core._handle_job
  File "viktor\core.pyx", line 1783, in viktor.core._handle_job.non_view_result
  File "viktor\result.pyx", line 112, in viktor.result.DownloadResult._serialize
  File "viktor\result.pyx", line 119, in viktor.result.DownloadResult._serialize
NotImplementedError

Could you please help me? Thanks in advance :pray:

1 Like

Hi Yida,

The zipped_files accepts a dictionary with a BytesIO or File object as value (SDK Reference - Result | VIKTOR Documentation).

In your case, probably something like:

fig_zip[fig_name] = File.from_data(fig_bytes)