Bulk calculations with Python
Use case
Perform multiple calculations in a single batch request to improve speed and reduce API overhead.
When you have many sets of input data, it is much more efficient to process them together in one API call rather than sending a separate request for each set. This approach reduces network traffic, minimizes waiting time, and improves overall performance when working with large datasets.
In many cases, you can perform bulk calculations directly using the provided CalcTree plugins.
This example, however, shows how to perform the same bulk calculation process by interacting with the CalcTree API directly using Python.
Example
This example demonstrates how to automate sending multiple rows of data through a CalcTree template page using the API.
The script is broken into four main parts:
Extract IDs: Extract the
workspaceIdandcalculationIdfrom a CalcTree URL.Fetch Page Structure: Retrieve the calculation structure to match input names to
statementIds.Submit Batch Mutations: Package all input rows into a single GraphQL batch request.
Process Results: Parse the JSON response and format output values (including units) into a clean results array.
We’ll step through each part individually, and then bring everything together into a full working example at the end.
1. Setup and Constants
Define the API endpoint and revision ID.
2. Helper Functions
2.1 Extract IDs from URL
Here, we extract the workspaceId and calculationId from the page URL.
2.2 Fetch Calculation Structure
Here, we use the getCalculation query to collect the structure of the target calculation and statementIds.
2.3 Format Output Values
CalcTree's API returns results for parameters as json objects which break parameters up into their values, formula, units etc. This function extracts values and the default units of the response parameters and returns them in a neet results array.
3. API Call: Batch Calculation Mutation
Here, we use the Calculate GraphQL query, but structure it to send a batch request. Instead of making a separate API call for each row of input data, we package all rows into a single API call.
This approach is much more efficient because it reduces network overhead and speeds up the bulk processing of larger datasets.
4. Main Function: Bulk Calculate
The bulk_calculate function is the driver of the entire bulk calculation process.
It calls the prior functions to performs the following steps:
Extracts the workspace ID and calculation ID from the page URL.
Fetches the structure of the calculation to match input parameter names to their correct
statementId.Organizes each input row into a set of calculation statements.
Builds a list of batch requests to submit together.
Calls
calculate_mutations_batchto execute all calculations in one API call.Formats the output by cleaning up unit values and organizing results into a structured table.
This function allows you to run many calculations at once on a CalcTree page using only one API request, making the process both scalable and fast.
5. Example Usage
Here we run the the bulk_calculate function by feeding input data into the function.
Note that the header row of the input data array must use the name of the parameter, as opposed to the display name shown on CalcTree pages. You can find this name in the settings for your parameters in CalcTree, or from the getCalculation API call (used here in step 2.2).
6. Results
For a calculation page that multiples w*l, to calculate Area, this returns the results:
Last updated