Transferring Data from DataGroup into Downloadable JSON File

Hi All

I’m wrapping up the first version of my app and aiming to include a feature to enhance its appeal. I want users to be able to export geometry to Revit as native objects via a JSON file. I’m stuck transferring data from the datagroup in the convert_dynamo_file_to_data_items method to the content_json method. Could you take a look and help out?
Thank you.

@rdejonge @ThomasN

Class Controller(ViktorController):

    # Extract the output values from the output file for display on Viktor.
    @staticmethod
    def convert_dynamo_file_to_data_items(input_file: DynamoFile, output_file: File) -> DataGroup:
        """Extracts the output of the Dynamo results by using the input and output files."""
        # Collect ids for the computational output from the dynamo file (numerical output)
        output_site_area_id = input_file.get_node_id("Parking Lot Area")
        output_parking_count_id = input_file.get_node_id("Parking Count")
        output_green_area_id = input_file.get_node_id("Green Surface Area")
        output_green_percent_id = input_file.get_node_id("Green Area %")

        # Collect the numerical results from the output file using the collected ids
        with output_file.open_binary() as f:
            site_area = get_dynamo_result(f, id_=output_site_area_id)
            parking_count = get_dynamo_result(f, id_=output_parking_count_id)
            green_area = get_dynamo_result(f, id_=output_green_area_id)
            green_percent = get_dynamo_result(f, id_=output_green_percent_id)

        # Add values to a structured data group
        data_group = DataGroup(
            DataItem(label="Site Area", value=site_area, suffix="m²"),
            DataItem(label="Parking Count", value=parking_count, suffix="spots"),
            DataItem(label="Green Area", value=green_area, suffix="m²"),
            DataItem(label="Green Area Percentage", value=green_percent, suffix="%"),
        )

        # Return the data group for display of outputs in the Viktor app.
        return data_group
    

    # Define the contents of the JSON file to be downloaded.
    def content_json(self):
        """Define function to create JSON file contents for user download."""
         
        # Define the content to be placed in the download JSON file.
        content = "how do I get a value from the convert_dynamo_file_to_data_items function in here?"

        # Return the content.
        return content
    

    # Define function to download the JSON file to create the designed elements in the user's software.
    def download_json(self):
        """Define function to download the JSON file."""
        return DownloadResult(self.content_json(), 'create_elements.json')

Hi Bayo,

I have to admit I have little experience with processing dynamo output data. But if I look in the docs it seems that the get_dynamo_result method returns string values. So you could easily write those to a json using something like a dict:

with output_file.open_binary() as f:
    site_area = get_dynamo_result(f, id_=output_site_area_id)
    parking_count = get_dynamo_result(f, id_=output_parking_count_id)
    green_area = get_dynamo_result(f, id_=output_green_area_id)
    green_percent = get_dynamo_result(f, id_=output_green_percent_id)

output_data = {
    "Site Area": site_area,
    "Parking Count": parking_count,
    "Green Area": green_area,
    "Green Area Percentage": green_percent
}

json_file = json.dumps(output_data)

Does that answer your question?

1 Like

Hello

Thanks for the insights. I attempted a similar approach and encountered an error (not previously mentioned in my initial post). It seems that DataGroup doesn’t accommodate returning tuples, which might be causing issues in the Viktor environment. Has anyone found a workaround for this? I tried creating a separate function outside of the DataGroup , but couldn’t get it to function correctly. Any guidance would be appreciated. Cheers!
Thank you.

Here is the error:

Traceback (most recent call last):
  File "viktor_connector\connector.pyx", line 295, in connector.Job.execute
  File "viktor\core.pyx", line 1926, in viktor.core._handle_job
  File "viktor\core.pyx", line 1915, in viktor.core._handle_job._handle_view
  File "viktor\views.pyx", line 902, in viktor.views._ViewResult._serialize
  File "viktor\views.pyx", line 917, in viktor.views._DataSubResult._serialize
  File "viktor\views.pyx", line 932, in viktor.views._DataSubResult._get_maximum_data_depth
AttributeError: 'tuple' object has no attribute 'values'

Here is the code:

    # Extract the output values from the output file for display on Viktor.
    @staticmethod
    def convert_dynamo_file_to_data_items(input_file: DynamoFile, output_file: File) -> DataGroup:
        """Extracts the output of the Dynamo results by using the input and output files."""
        # Collect ids for the computational output from the dynamo file (numerical output)
        output_site_area_id = input_file.get_node_id("Parking Lot Area")
        output_parking_count_id = input_file.get_node_id("Parking Count")
        output_green_area_id = input_file.get_node_id("Green Surface Area")
        output_green_percent_id = input_file.get_node_id("Green Area %")

        # Collect the numerical results from the output file using the collected ids
        with output_file.open_binary() as f:
            site_area = get_dynamo_result(f, id_=output_site_area_id)
            parking_count = get_dynamo_result(f, id_=output_parking_count_id)
            green_area = get_dynamo_result(f, id_=output_green_area_id)
            green_percent = get_dynamo_result(f, id_=output_green_percent_id)

        output_data = {
            "Site Area": site_area,
            "Parking Count": parking_count,
            "Green Area": green_area,
            "Green Area Percentage": green_percent,
        }

        json_file = json.dumps(output_data)

        # Add values to a structured data group
        data_group = DataGroup(
            DataItem(label="Site Area", value=site_area, suffix="m²"),
            DataItem(label="Parking Count", value=parking_count, suffix="spots"),
            DataItem(label="Green Area", value=green_area, suffix="m²"),
            DataItem(label="Green Area Percentage", value=green_percent, suffix="%"),
        )

        # Return the data group for display of outputs in the Viktor app.
        return data_group, json_file

I think you have forgotten to specify the use of the actual data_group object in another method somewhere where you’re using the results of this convert_dynamo_file_to_data_items method. Because you’re now returning two objects they form a Tuple.

1 Like

Thanks a lot.
As you noted, I had to make some adjustments to the code above.