Using CSV Data in Selection lists

Connect a CSV file to Selection lists in CalcTree using Python for structured data filtering and selection.

This guide walks you through how to read structured data from a CSV file and build dependent Selection lists in CalcTree using Python and math variables. The pattern is commonly used in workflows that require a selection of material or section type and then extracting specific properties for the selection for use in calculations.

1. Load and Structure Your Data

Upload your CSV file (e.g. CT - Standard Steel Section Capacity - DB.csv) using the Files tab, then read it into a structured format using pandas. Learn more here.

Once this is complete, create a new Python node, and add the below code.

import pandas as pd 

def read_csv(csv):
    return pd.read_csv(ct.page_files[csv])

section_db = read_csv("CT - Standard Steel Section Capacity - DB.csv") \
    .astype('object') \
    .fillna("") \
    .to_dict("records")

This loads your CSV into a list of dictionaries (records) that are easy to filter by field name.

In CalcTree, we recommend using Python dictionaries instead of pandas DataFrames for better performance. While DataFrames are supported, they are heavier and better suited to large-scale data processing outside of interactive calculation pages.

2. Apply the Two-Step Filter

2.1 Extract Primary Filter Options

In the same code, get the unique values from a primary column (e.g. "Type"):

type_list = list({row["Type"] for row in section_db})

2.2 Create a Selection list

Create a new math variable in the page for the list and make it equal to type_list.

Section_Type_ = type_list

Then in the settings for this variable, set the visibility to 'Selection'.

2.3 Filter the Secondary Options Based on type_list Selection

Filter the list of available "Section" values based on the currently selected "Section_Type_".

section_list = list({
    row["Section"] for row in section_db if row["Type"] == Section_Type_.upper()
})

Make sure the "Type" name matches the variable name you set, in this case, Section_Type_.

2.4 Repeat step 2.2 for the new dependent list

Insert section_list into the page creating a new variable Section = section_list . Then in the settings for this variable, set the visibility to 'list'.

3. Use the Selected Section in Your Logic

Once the user has made a selection, extract the full row from the database and use its fields in your calculation.

selected_row = next((row for row in section_db if row["Section"] == Section), {})

depth = float(selected_row.get("Depth", 0))
area = float(selected_row.get("Area", 0))
moment_capacity = float(selected_row.get("M_ult", 0)) / 1000  # Nm → kNm

4. Use the Selected Item in Your Logic

Once a specific section is selected (e.g., "UB 203x102x23"), use it to retrieve or process further data in a Code Source 3.

Summary

This approach lets you:

  • Link a CSV database to interactive parameters

  • Build dependent Selection lists (e.g. section type → section name)

  • Extract and use structured data in calculations

It’s ideal for any workflow where selections narrow down from a database — like selecting standard parts, profiles, or material specs.

Last updated