CalcTree Help Pages
  • What is CalcTree?
  • Getting started
    • CalcTree Pages
    • Create a CalcTree Page
    • Add a calculation
    • Collaborate with a colleague
  • Calculations
    • Parameters
      • Math formulas
        • Parameter Data Types
        • Native Functions
          • Arithmetic Functions
          • Trigonometric Functions
          • Logical & Comparison Functions
          • Matrix & Vector Functions
          • Probability & Combinatorics Functions
          • Statistical Functions
          • String Functions
          • Utility Functions
          • Other Native Functions
        • Valid Expression Syntax
      • Supported Units
      • Dropdown List Parameters
        • Linking CSV Data to Dependent Dropdowns in CalcTree
      • Parameter Settings
    • Integrations
      • Python in CalcTree
        • Adding a Python Source
        • Defining Parameters in Python
        • Referencing Other Parameters in Python
        • Working with Units in Python
        • Creating Tables and Visuals in Python
        • Consuming Files in Python
        • Using Pre-installed Python Libraries
      • Spreadsheets [Coming Soon!]
      • File Upload
        • CSV files
      • 3rd Party Software Plugins
        • Excel
        • Grasshopper
        • ETABS [v20 & v21]
        • ETABS [v22]
        • SAP 2000
        • CSI Bridge [v26]
    • Templates [Coming Soon!]
    • Optimising your calculations
  • Pages & Reports
    • CalcTree Documents
    • Static content
    • Parametric content
      • Parametric equation
      • Inputs
      • Outputs
  • Export to PDF
  • API
    • GraphQL API
      • Generating an API key
      • Queries
        • GetCalculation
        • Calculate
      • Examples
        • Bulk calculations with Python
  • Collaborate
    • Add members
    • Review and approval
    • Add stakeholders
  • Administrate
    • CalcTree Workspace
    • Versioning and Audit trail
  • CalcTree for System Administrators
Powered by GitBook
On this page
  • Defining Quantities
  • Other Unit Operations
  • Unit-Aware Validation
  • Working with Page Parameters that include Units
  1. Calculations
  2. Integrations
  3. Python in CalcTree

Working with Units in Python

CalcTree supports unit-aware calculations in Python, allowing you to safely perform operations on physical quantities with automatic unit handling and validation.

PreviousReferencing Other Parameters in PythonNextCreating Tables and Visuals in Python

Last updated 1 day ago

Create physically meaningful calculations by attaching units to values in your Python scripts using ct.quantity(). This makes unit consistency a built-in feature of your calculations, reducing the risk of error when working with inputs or outputs across multiple sources.

Defining Quantities

Use ct.quantity() to create values with physical units:

force = ct.quantity("1 N")
area = ct.quantity("1 m^2")

pressure = force / area

You can inspect the result on the right-hand panel. CalcTree automatically handles the unit math.

Other Unit Operations

  • Create a unit with a magnitude of 1.0 using ct.units():

    kN = ct.units("kN")
  • Convert to a compatible unit using .to():

    pressure_kPa = pressure.to("kPa")
  • Extract the numeric value (without the unit) using .magnitude():

    value = pressure_kPa.magnitude()

Unit-Aware Validation

CalcTree checks for physical consistency. If you perform an invalid operation (e.g. add time and length), the script will raise an error:

invalid = ct.quantity("5 s") + ct.quantity("2 m")  # Raises an error

This acts as built-in quality control when linking Python to other parameter sources.

Working with Page Parameters that include Units

When you reference parameters from other sources (e.g. MathJS, tables, spreadsheets), they may already include physical units. These are passed into Python as ct.quantity() objects.

You can use them directly in calculations, convert them, or extract numeric values as needed.

Example:

Assume your page has two parameters:

  • width = 100 mm (from MathJS)

  • height = 2.5 m (from a spreadsheet)

In Python:

# These are already ct.quantity objects
area = width * height

# Convert area to m²
area_m2 = area.to("m^2")

# Get just the numeric value
value = area_m2.magnitude

There’s no need to manually re-wrap page parameters with ct.quantity()—they are unit-aware by default when accessed in Python.

CalcTree uses the Python library pint to enable handling units. You can access pint's documentation .

here