Python
Create Python-powered calculations
CalcTree provides Python scripts as a fully integrated calculation option. You won't need to install development tools or learn deployment nuances to create a Python calculation in CalcTree.
On this page you will learn to create Python calculations. To learn more about creating parametric content based on these calculations, continue on Parametric content
Adding a Python source
From the right-hand panel, click on Code. A Python source will be added to the page. The Python editor slides in from bottom of the view showing this new Python source. The Python source initially contains guide comments in green color and one sample line of code:


Python editor
Calctree provides a Python code editor, which you can use to view and edit Python scripts. The Python editor provides common helpful features such as find and replace, color coding, auto completion, keyboard shortcuts and contextual error reports.
Some useful keyboard shortcuts inside the Python editor include:
Undo
Ctrl + Z
Redo
Ctrl + Y
Cut
Ctrl + X
Copy
Ctrl + C
Paste
Ctrl + V
Toggle Line Comment
Ctrl + /
Search
Ctrl + F
Replace
Shift + Ctrl + F
Increase indent
Tab or Ctrl + ]
Decrease indent
Shift + Tab or Ctrl + [
Move a line up
Alt + Up
Move a line down
Alt + Down
Select all
Ctrl + A
Select to the end
Alt + Shift + Right
Select to the start
Alt + Shift + Left
CalcTree uses a Microsoft technology called Monaco Editor to provide the integrated Python editor. You can find more technical documentation about Monaco Editor here
Python Parameters
CalcTree sources define and calculate parameters. For Python sources, parameters are defined by variables of the Python script. All variables on the root level of the script -e.g. are not inside a function-, will be taken as Python parameters and will be added to the CalcTree Page's pool of parameters.
Example Python parameter:
Type the below variable declaration in your Python source editor. After a moment, you will notice a parameter called a
is created under the Python source on the right-hand panel:

Python parameters with units
A number and a physical unit comprise a quantity in CalcTree. You can use quantities in Python scripts. For example, you can divide 1 N
by 1 m^2
to get 1 kPa
. To create a quantity inside a Python script, use the ct.quantity()
method. For example, copy the following lines in a Python source and inspect the values on the right-hand panel:

You can also define a quantity of 1.0 magnitude of a unit, using the ct.units()
method. Additionally, use the .to()
method to convert a quantity to a compatible unit and use the .magnitude()
method to get the numeric value of a quantity.
Note that if you try operations on quantities that are not physically meaningful, you will receive an error. For example adding a length to a time will result in an error:

This unit-awareness acts as a built-in quality control, because it checks the operations are done on compatible quantities. This is especially useful when linking parameters from other sources in Python scripts. Read more here: #chaining-python-to-other-sources
CalcTree uses the Python library pint
to enable handling units. You can access pint's documentation here .
Python tables
If you declare a Python variable as an array of arrays, it will create a tabular parameter. For example the following will result in a tabular parameter for the Python source:

Python figures
You can generate figures using the Matplotlib Python library. If a Python source includes a figure created using Matplotlib, it will be extracted as a Python source figure. For example, the following script creates a figure under the Python source:

You can learn more about creating various charts or diagrams using the Matplotlib library here
Valid data types for CalcTree parameters
For Python sources, parameters are defined by variables of the Python script. All variables on the root level of the script, such as the ones not inside a function, will be taken as Python parameters and will be added to the CalcTree Page's pool of parameters. However, the Python variable should hold a value with a type that is among CalcTree parameter data types . If a Python variable holds a Python data type that is not recognisable by CalcTree, you will receive a string warning as the value of that parameter in CalcTree. For example:

Linking Python to other sources
You can link multiple sources to create more complex calculations. That means a source picks up a result from another source and does more calculations on it.
In order to link a Python script to other calculations, it needs to refer to the value of another source's parameters. That is simply achievable by typing the parameter name inside Python script. For example, if you have two parameters called width
, and height
which are defined by other sources, you can refer to them in your Python script:


In the above example, the parameters width
and length
could be defined by another source of any type.
Available Python libraries
The following Python libraries are already installed on CalcTree's Python. You can start using them by regular importing, for example:
anaStruct 2D structural analysis in Python
REDi REDi (Resilient Design for the Next Generation of Buildings) seismic engine documentation developed at ARUP
BeamBending A teaching aid for 1-D shear-force and bending-moment diagrams
energy-py-linear Optimize energy assets using mixed-integer linear programming
eurocodepy a Python package for calculating structures according to Eurocodes
a Python package to create new DXF documents and read/modify/write existing DXF
GemPy an open-source, Python-based 3-D structural geological modeling software
Groundhog A general-purpose geotechnical package
handcalcs Python library for converting Python calculations into rendered latex
joypy Joyplots in Python with matplotlib & pandas
matplotlib plotting with Python
numpy The fundamental package for scientific computing with Python
OpenSeesPy + OpsVis Finite element applications for simulating the response of structural and geotechnical systems subjected to earthquakes
Pandas Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
pyCalculiX Python 3 library to automate and build finite element analysis (FEA) models in Calculix
pycba Python Continuous Beam Analysis
pyFrame3DD Python bindings to Frame3DD
PyNite A 3D structural engineering finite element library for Python
pysal Python Spatial Analysis Library
Scikit-learn Machine learning in Python
Scipy Software for mathematics, science, and engineering. It includes modules for statistics, optimization, integration, linear algebra, Fourier transforms, signal and image processing, ODE solvers, and more.
seaborn Statistical data visualization in Python
sectionproperties Analysis of an arbitrary cross-section in python using the finite element method.
SimPy A process-based discrete-event simulation framework
StructPy Structural Analysis Library for Python based on the direct stiffness method
SymPy Symbolic mathematics
topojson An extension of GeoJSON that encodes topology
The list of available Python libraries is growing. Please follow our product roadmap items and status here and let us know what you think!
Providing necessary files to your Python script
You can upload any file type to a CalcTree page via the File Upload from the right-hand panel. The uploaded file will be available to Python scripts.


After uploading of any filetype through File upload, the file appears on the right-hand panel, and is accessible to Python via ct.page_files['
file_name
']
. To see an example of providing a CSV file to a Python script, continue on: Using CSV files through Python .
Using this approach, you will be able to provide arbitrary preset data to your Python scripts on CalcTree. That especially works well for Python libraries that can manipulate specific file types.
CalcTree can be used for creating calculations using Python, as explained in this page. These Python features offer endless possibilities for creating calculations. Furthermore, you can create parametric content using these calculations. Take a look at our template pages for some inspiration!
Learn how to create parametric content from calculations:
Add documentation
Parametric content
Last updated