Creating entity in other workspace

Hi Support,
We would like to use an existing application within a new application. Therefore we would like to use the extended API (new app is running on viktor 14.13.0). I can find the method api.entity_compute() but this method only works for entitys that are already created. I can also find the method api.create_child_entity() but I think this method can only create a child within the workspace that is used.

Is there a way by using the extended API to create a new child in an existing (external) workspace, fill it with params, and run a method from the application controller?

Kind regards,
Bart Vosslamber

Hi Bart,

You can use workspace_id in create_child_entity() as an optional argument to create entities outside the context of the app.

The method entity_compute() is defined on the Workspace class, not on the API class. You can retrieve workspace objects using API().get_workspace().

Regards,

Raoul

Hi Raoul,

Thanks for the reply. I have tried to run a correct computation. Therefore I have used the following test script within the application:

# initialize the viktor API()
api = API(
    token='TOKEN_CREATED_IN_VIKTOR_ENVIRONMENT',
    environment='heijmans.viktor.ai',
)

# section name
section_name = f'NEW_SECTION_TO_BE_CREATED'

# create an entity in the correct workspace with the section_name
api.create_child_entity(
    parent_entity_id=48417,
    workspace_id=7,
    entity_type_name='RectangularCrossSectionCalculator',
    name=section_name,
    params=set_with_params, # dict containing the correct params
)

# get the correct workspace 
cross_section_calculator = api.get_workspace(id_=7)

# search the correct entity within the workspace
for project in cross_section_calculator.get_entity(id_=9619).children():
    if project.id == 48416:
        for object in project.children():
            if object.id == 48417:
                for child in object.children():
                    if child.name == section_name:
                        results = cross_section_calculator.entity_compute(
                            entity_id=child.id,
                            method_name='update_m_n_tables',
                            params=set_with_params,
                        )
                        print(results)

When I run the script, a correct entity is created within the external application. I can go to that entity and run it manually without errors. But the .entity_compute() method doesn’t run properly. I recieve the following error message:

2024-07-23 09:06:45.096 ERROR   : Exception is raised
Traceback (most recent call last):
  File "viktor_connector\\connector.pyx", line 295, in connector.Job.execute
  File "viktor\core.pyx", line 2006, in viktor.core._handle_job
  File "viktor\core.pyx", line 1943, in viktor.core._handle_job._handle_button
  File "C:\Users\bavo6\PycharmProjects\HI-LandhoofdApp\app\Abutment\controller.py", line 1681, in get_scia_results_from_scia_file_upload
    results = cross_section_calculator.entity_compute(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "viktor\api_v1.pyx", line 781, in viktor.api_v1.Workspace.entity_compute
  File "viktor\api_v1.pyx", line 1321, in viktor.api_v1._API.entity_compute
  File "viktor\api_v1.pyx", line 1111, in viktor.api_v1.Job.get_result
viktor.errors.ComputeError: Computation failed with an error (type: error_code, details: )

Can you help me?

Hi Bart,

I’m not yet sure what the error is that you are running into, but I can at least provide some tips on how to slightly simplify your code:

# initialize the viktor API()
api = API(
    token='TOKEN_CREATED_IN_VIKTOR_ENVIRONMENT',
    environment='company.viktor.ai',
)

# create an entity in the correct workspace with the section_name
new_section_entity = api.create_child_entity(
    parent_entity_id=48417,
    workspace_id=7,
    entity_type_name='RectangularCrossSectionCalculator',
    name=f'NEW_SECTION_TO_BE_CREATED',
    params=set_with_params, # dict containing the correct params
)
cleaned_params = new_section_entity.last_saved_params  # cleans params, including any missing values
results = new_section_entity.compute(method_name='update_m_n_tables', params=cleaned_params)
print(results)

The error basically means that an uncaught exception occurred during the computation. This could have a few potential sources:

  • The provided params were incomplete, which could cause a Attribute error
  • The provided params were incorrect, which lead to a processing error.
  • Other resources needed in the computation were not available. (unlikely)

You could try debugging this set-up, by running the app of the other workspace in your development environment (don’t forget to update the workspace_id and parent_entity_id to your development workspace ids) and then running your above piece of code in a separate python script (not in the same app, as this could cause racing conditions).

I hope this helps.

Hi Kevin,
This works, lot of thanks!

Great to hear.

That probably means that the set_with_params that you initially provided in the compute() function was missing an attribute/value somewhere.

By using the ‘cleaned_params’ as described above, we ensure that any missing values in the params get filled with their default or empty values.