UzonCalc User Guide

This document is generated with UzonCalc.

UzonCalc is a Python-based tool for hand-written engineering calculation reports. You only need to focus on writing content and calculation logic, without worrying about calculation results, layout, and other details. UzonCalc automatically substitutes values, calculates results, and generates polished calculation report documents, including tables of contents, mathematical formulas, tables, charts, and more. UzonCalc supports unit calculation and Excel table calculation calls, which makes it well suited to engineering calculation reports. By calling formulas in Excel, you can reuse existing Excel calculation models and reduce migration cost.


Key features:


UzonCalc is very simple to use. You do not need to be proficient in Python. If you have experience using formulas in Excel, you can get started, and you can treat every operation as a function call.

Do not assume it is difficult just because it uses Python and involves programming. The important point bears repeating: it is simple, simple, simple.
Table of Contents

Installation and Usage

Windows Desktop Installation

The desktop app provides extra features such as calculation report management and UI input, and is intended for users. If you are a calculation report author, the CLI method below is recommended. It can be used with VS Code for automatic formatting and syntax checking to improve authoring efficiency.

Install the desktop app as follows:

1. Download the software

Download the win-x64 version from Releases · uyoufu/UzonCalc, extract it, and double-click UzonCalc.exe to start.

2. Copy the following code into the new editor box

from uzoncalc import *

@uzon_calc()
async def sheet():
    doc_title("example")

    "Hello, UzonCalc!"

    w = 10*unit.m
    l = 5*unit.m
    A = w * l

3. Click the run button

UzonCalc run result
UzonCalc run result

CLI Installation

The CLI method is mainly intended for calculation report authors. It supports automatic formatting and syntax checking, which improves authoring efficiency. When editing calculation reports, AI-assisted authoring is recommended, especially for charts and complex calculation logic.

Installation steps:

1. Make sure Python is installed

  1. Install UzonCalc with pip install uzoncalc or uv add uzoncalc

3. Create a calculation report script

# example.py

from uzoncalc import *

@uzon_calc()
async def sheet():
    doc_title("example")

    "Hello, UzonCalc!"

    w = 10*unit.m
    l = 5*unit.m
    A = w * l

if __name__ == "__main__":
    view(sheet)

4. Run

There are two ways to run a calculation report in CLI mode:

The difference is that the former requires view(sheet) at the end of the code to start the calculation report service, while the latter does not require the if __name__ == "__main__" statement.

When you run the following command:

python --serve example.py

You will see `Serving document at: http://127.0.0.1:32180/`.. Click it or open it in a browser to view the result.

A calculation report service started this way does not hot reload. When the report code changes, the service must be restarted manually.

For convenient development, use uzoncalc example.py to run the calculation report. The service will reload automatically when the report code changes.

Basic Syntax

UzonCalc is built on Python and introduces no additional language definitions, so writing calculations means writing Python code.

If you have not used the Python programming language before, the following basic syntax introduction will help you get started quickly.

  1. Text: use single quotes ', double quotes ", or triple quotes """ to wrap text content
  2. Numbers: use integers and floating-point numbers directly, such as 42 or 3.14
  3. Boolean values: use True and False, such as isValid = True
  4. Lists: use square brackets [] to create lists, such as myList = [1, 2, 3, 4, 5]
  5. The = sign: used for assignment, such as a = 5, which assigns the number 5 to variable a
  6. +, -, *, /, ** operators: used for basic mathematical operations, such as addition, subtraction, multiplication, division, and exponentiation
  7. >, <, >=, <=, ==, != comparison operators: used to compare size or equality, such as a > b checking whether a is greater than b, and a == b checking whether a equals b
  8. Variable names may contain letters, numbers, and underscores, but cannot start with a number. In UzonCalc, camelCase naming is recommended, such as myVariableName, because underscores are used as subscript symbols.
  9. Indentation: Python uses indentation to represent code blocks, usually with 4 spaces
  10. Comments: content starting with # is a comment. Comments are not executed and are only used to explain code
  11. Function definitions: use the def keyword to define functions, such as def myFunction(param1, param2):, which defines a function named myFunction with two parameters, param1 and param2. Call it with myFunction(5, 10) and pass parameter values 5 and 10.
  12. Module imports: use import statements to import modules, such as from numpy import sqrt to import the sqrt function, then call sqrt(16) to calculate a square root.

For now, understanding this basic syntax is enough to start writing calculation reports with UzonCalc. As you use it, gradually learn more Python syntax and features so you can use UzonCalc better.


For beginners, the following code style suggestions are recommended:

1. Use English for naming where possible and avoid Pinyin. English expresses meaning better and is easier to read.

2. Use camelCase for variable names and avoid underscores, because `_` is used as a subscript symbol.

3. Add comments where appropriate to help explain code logic.

4. Keep code concise, split complex logic into functions, and put code with different responsibilities into different files.

Automatic Table of Contents

Call toc('Table of Contents') to automatically generate a document table of contents at the call location. The table of contents in this document is generated by calling toc('Table of Contents'). It is generated automatically from heading levels in the document and page numbers are calculated automatically. See the actual table of contents above for the result.

Automatic Figure and Table Numbering

When using UzonCalc, you do not need to number figures and tables manually. The system generates numbers automatically, which greatly reduces maintenance work when figures or tables are added or removed.

When you call Img, Table, Echarts, or Plot, the system returns a placeholder string. You can use that placeholder later in the document to reference the figure or table.

The following uses an image as an example. The code is:

imgNo = Img(
        "https://oss.uzoncloud.com:2234/public/files/images/image-20260527133234259.png",
        alt="UzonCalc run result",
    )

f"Now you can use imgNo to reference this image: see {imgNo}."

Actual result:

UzonCalc run result
UzonCalc run result

Now you can use imgNo to reference this image: see .

From the example above, you can also see that the placeholder is ultimately replaced with the image number, and you can click that number to jump to the corresponding image.

Figure and table numbers can be used anywhere later in the code.

The prefixes for figure and table numbers can be set with figure_prefix("Figure") and table_prefix("Table"). This setting takes effect for subsequent figures and tables. The default prefixes are "Figure" and "Table" in this English example.

UzonCalc run result
UzonCalc run result

Now reference the image above: see . You can see that the prefix has changed to Figure.

Headings

The system has two built-in title functions: Title() and H1(). Use them with calls such as H1('Heading name'). There are 1 to 6 heading levels, corresponding to the H1, H2, H3, H4, H5, and H6 functions.

In general, H1 is used for the document main title, H2 for section headings, H3 for subsection headings, and so on.

Text Types

Use single quotes ('), double quotes ("), or triple quotes (""") to mark content that should be output as a paragraph.

Example:

'Single-quoted text'

"Double-quoted text"

"""
Triple-quoted text,
this is line 1,
this is line 2
"""

The code above outputs:

Single-quoted text

Double-quoted text

Triple-quoted text, this is line 1, this is line 2


Single-quoted and double-quoted strings are suitable for single-line text, while triple-quoted strings are suitable for multi-line text. Triple-quoted strings can span multiple lines, but they are merged into a single paragraph during rendering and line breaks are not preserved. This is useful when writing longer paragraphs.

This differs from everyday Word document input. In UzonCalc, all text must be wrapped in quotes. This is required by Python syntax.

Numeric Types

Numeric values do not need to be wrapped in quotes. Enter them directly.

Example:

# Integer
integerNumber = 100
# Floating-point number
floatNumber = 3.1415
# Scientific notation
scientificNumber = 1.2e3
# Complex number
complexNumber = 2 + 3j

The output is:

integerNumber=100

floatNumber=3.1415=3.142

scientificNumber=1200.0=1200

complexNumber=2+3j

Operations and Comparisons

Use standard arithmetic operators +, -, *, /, and ** (** means power) to calculate numeric values.

Use >=, <=, ==, and != to compare values.

Example:

operatorResult = (5 + 3) * 2 - 4 / 2**2
comparisonResult = (5 > 3) and (2 == 2) or (4 != 5)
        

operatorResult=(5+3)·2-422=15

comparisonResult=5>32245=True

Using Variables

A variable is a container defined in a program to store data. It lets you reuse the same data without entering it repeatedly.

You can also think of variables as parameter symbols in formulas, such as f_c, E, and I.

Consider the following code:

N_s = 100 * unit.KN
A_s = 50 * unit.mm**2
sigma_s = N_s / A_s
         

Ns=100kN

As=50mm²

σs=NsAs=100kN50mm²=2kN/mm²

Variable Naming Rules

Variable names can contain letters, numbers, and underscores, but cannot start with a number.

In UzonCalc, camelCase naming is recommended, such as myVariableName, because _ underscores are used as subscript symbols.

Variable Aliases

Because Python variable names are limited, you cannot directly use non-ASCII characters as variable names. Sometimes you may want to use a friendlier name in the document, and aliases are designed for that.

Define an alias with the alias("variableName", "alias") function:

Example:

# Concrete strength grade
f_c = 30 * unit.MPa
alias("f_c", "concrete strength grade")

# After the alias is defined, the variable is displayed with the alias until the alias is removed.
f"Now the alias will be output: {f_c}"

# Define a None alias to remove the alias.
alias("f_c", None)

# The alias has been removed and f_c is displayed with its original name again.
f"The alias has been removed. Now the original name is output: {f_c}"

fc=30MPa

Now the alias will be output: 30MPa

The alias has been removed. Now the original name is output: 30MPa

Input Variables

This feature is only available in UI mode. Call the UI() function to create an input form. The form renders input boxes in the user interface. When the program detects a UI, it pauses and waits for user input. After input is complete, the program continues and uses the user's input as the function return value.

The UI function has 3 parameters. The first two are required and the last one is optional:

The code is:

inputs = await UI(
    "Structural Parameter Input",
    [
        Field("width", "Width", FieldType.number, value=10),
        Field("length", "Length", FieldType.number, value=30),
        Field("height", "Height", FieldType.number, value=20),
    ],
)

f"The user-entered width is {inputs.width}, length is {inputs.length}, and height is {inputs.height}."

The UI effect of the code above is shown in the figure below:

From , you can see that the system converts your input code into a visual UI, where users can enter values and interact with the program.

The output of the code above is:

The user-entered width is 10, length is 30, and height is 20.

Variable Subscripts

Default Subscript Rules

If you want a word to become a subscript, put an underscore after it. For example, H2 is rendered as H₂.

Example:

a_x = 10 * unit.meter / unit.second**2
speed_car = a_x * 2 * unit.second

ax=10m/s²

speedcar=ax·2s=10m/s²·2s=20m/s

Non-ASCII Character Subscripts

Because Python field names are restricted, non-ASCII characters cannot be used directly as variable names. If you want to use non-ASCII characters as subscripts, use aliases.

You can use an alias to represent non-ASCII characters as a subscript.

Example:

alias("speed_car", "speed_车")
f"Now the alias subscript will be output: {speed_car}"
distance = speed_car * 5 * unit.second
alias("speed_car", None)
f"The alias has been removed, and speed_car is restored to its original name: {speed_car}"

Now the alias subscript will be output: 20m/s

distance=speed·5s=20m/s·5s=100m

The alias has been removed, and speedcar is restored to its original name: 20m/s

Array Subscripts

For arrays, the content inside [] is automatically handled as a subscript.

arr2d = np.array([[1, 2, 3], [4, 5, 6]])
firstRow = arr2d[0, :]
secondColumn = arr2d[:, 1]
firstCell = arr2d[0, 0]
list1 = [10, 20]
list2 = [30, 40]
combinedList = list1 + list2
secondItem = combinedList[1]

arr2d=np.array([123456])=[123456]

firstRow=arr2d0,:=[1,2,3]

secondColumn=arr2d:,1=[2,5]

firstCell=arr2d0,0=1

list1=[10,20]

list2=[30,40]

combinedList=list1+list2=[10,20]+[30,40]=[10,20,30,40]

secondItem=combinedList1=20

Multidimensional Arrays

The general representation of multidimensional arrays is often called a tensor. It can represent scalars (0-dimensional tensors), vectors (1-dimensional tensors), matrices (2-dimensional tensors), and higher-dimensional arrays.

UzonCalc supports tensor calculation with NumPy.

Here are some tensor examples:

arr = np.array([[1, 2, 3], [4, 5, 6]])
firstRow = arr[0, :]
secondColumn = arr[:, 1]
firstCell = arr[0, 0]

arr=np.array([123456])=[123456]

firstRow=arr0,:=[1,2,3]

secondColumn=arr:,1=[2,5]

firstCell=arr0,0=1

In UzonCalc, variables are displayed in italic. Tensors with more than one dimension are displayed in bold upright style, matching the notation commonly used in papers.

Units

When calculating formulas, UzonCalc supports calculation with units. Different units with the same dimension do not need to be converted manually and can be calculated directly. For example:

totalLength=10m+20cm=10.2m

Using Units

UzonCalc uses pint as the unit calculation engine. Use units through unit.*, where * is the specific unit symbol, such as unit.m for meters. For the specific unit list, see https://github.com/hgrecco/pint/blob/master/pint/default_en.txt . Here are some unit calculation examples.

l_cp = 5 * unit.meter
w_cp = 10 * unit.m
h_cp = 2 * unit.meter
v_cp = l_cp * w_cp * h_cp

lcp=5m

wcp=10m

hcp=2m

vcp=lcp·wcp·hcp=5m·10m·2m=100

force = 100 * unit.newton
A_top = l_cp * w_cp
stress_cp = force / A_top

force=100N

Atop=lcp·wcp=5m·10m=50

stresscp=forceAtop=100N50=2N/m²

Unit Calculation

You can perform mathematical operations directly on quantities with units: addition, subtraction, multiplication, division, powers, square roots, and so on.

sqrtArea = sqrt(A_top + 10 * unit.meter**2)
acceleration = force * 1000 / (10 * unit.kilogram)

sqrtArea=Atop+10=50+10=7.746m

acceleration=force·100010kg=100N·100010kg=10000N/kg

Different units with the same dimension do not need to be converted manually and can be calculated directly. For example:

totalLength=10m+20cm=10.2m

Unit Conversion

Use the to() method to convert a quantity with units to another unit.

speedMPerS = 18 * unit.meter / unit.second
speedKmPerH = speedMPerS.to(unit.kilometer / unit.hour)
"Before unit conversion, speed (m/s):"
speedMPerS
"After unit conversion, speed (km/h):"
speedKmPerH

speedMPerS=18m/s

speedKmPerH=speedMPerS.to(km/h)=64.8km/h

Before unit conversion, speed (m/s):

speedMPerS=18m/s

After unit conversion, speed (km/h):

speedKmPerH=64.8km/h

Greek Letter Conversion

Use Greek letter English names directly, such as alpha (α), beta (β), gamma (γ), and delta (δ). The system automatically renders them as the corresponding Greek letters.

Names starting with lowercase letters are rendered as lowercase Greek letters, and names starting with uppercase letters are rendered as uppercase Greek letters.

If you do not want a name to be converted to a Greek letter, add \ before the name, for example, \\alpha.

rho_water = 1000 * unit.kilogram / unit.meter**3
gamma_0 = 9.81 * unit.meter / unit.second**2

ρwater=1000kg/m³

γ0=9.81m/s²

Function Styles

The following functions are automatically converted to mathematical style:

Square Root

Use sqrt(x) to represent the square root of x.

edge1 = 3 * unit.meter
edge2 = 4 * unit.meter
diagonal = sqrt(edge1**2 + edge2**2)

edge1=3m

edge2=4m

diagonal=edge12+edge22=(3m)2+(4m)2=5m

Absolute Value

Use abs(x) to represent the absolute value of x.

value = -15 * unit.newton
absValue = abs(value)

value=-15N=-15N

absValue=|value|=|-15N|=15N

Advanced String Usage

f-string

An f-string, also called a formatted string literal, lets you embed expressions directly in a string and calculate their values at runtime.

To create an f-string, add `f` or `F` before the opening quote of the string.

name = "Uzon"
f"Hello, {name}! Welcome to UzonCalc."
pi = 3.1415926535
f"Value of pi up to 3 decimal places: {pi:.3f}"

Run result:

name=Uzon

Hello, Uzon! Welcome to UzonCalc.

π=3.1415926535=3.142

Value of π up to 3 decimal places: 3.142

Displaying Formulas in f-strings

By default, expressions in f-strings display only their calculation results. Sometimes you may want to display both the formula and the calculation process in the document. Use the enable_fstring_equation() function to enable this feature. After it is enabled, expressions in f-strings display both formulas and calculation results, until you call disable_fstring_equation() to turn it off.

enable_fstring_equation()
width = 10 * unit.meter
length = 20 * unit.meter
f"Area is calculated as {width * length}."
disable_fstring_equation()

width=10m

length=20m

Area is calculated as width·length=10m·20m=200.

Assigning f-string Calculation Results

Use the `:=` walrus operator to assign the result of an expression in an f-string to a variable for later calculations.

f"Area is calculated as {(tempArea := width * length)}."
enable_fstring_equation()
f"Area is calculated as {(tempArea := width * length)}."
disable_fstring_equation()
tempArea

Run result:

Area is calculated as 200.

Area is calculated as tempArea=width·length=10m·20m=200.

tempArea=200

String Escaping

As mentioned above, English names of characters and variable names connected with _ are rendered as Greek letters and subscripts. To preserve the original characters, add \ before the character, for example, \alpha.

Charts

Use ECharts and Matplotlib to create charts in UzonCalc.

You can also use other plotting libraries you prefer, such as Plotly and Seaborn.

JavaScript charts are interactive, while Matplotlib charts are rendered as static images and are suitable for print output.

ECharts Example

Use the echarts library to create rich interactive charts. For more details, see the official documentation and examples: https://echarts.apache.org/examples/zh/index.html#chart-type-line

# Create an ECharts chart.
EChart(options)

The options parameter in this example refers to: https://echarts.apache.org/examples/zh/editor.html?c=area-stack

ECharts 3D Example

Use ECharts GL to create 3D charts. You can rotate and zoom the chart with the mouse to view different angles and details.

hide()
    ROOT_PATH = "https://oss.uzoncloud.com:2234/public/files/images"
    show()

    EChart(
        {
            "backgroundColor": "#000",
            "globe": {
                "baseTexture": ROOT_PATH + "/earth.jpg",
                "heightTexture": ROOT_PATH + "/bathymetry_bw_composite_4k.jpg",
                "displacementScale": 0.1,
                "shading": "lambert",
                "environment": ROOT_PATH + "/starfield.jpg",
                "light": {"ambient": {"intensity": 0.1}, "main": {"intensity": 1.5}},
                "layers": [
                    {
                        "type": "blend",
                        "blendTo": "emission",
                        "texture": ROOT_PATH + "/night.jpg",
                    },
                    {
                        "type": "overlay",
                        "texture": ROOT_PATH + "/clouds.png",
                        "shading": "lambert",
                        "distance": 5,
                    },
                ],
            },
            "series": [],
        },
        use_gl=True,
        caption="3D Earth Example",
    )
3D Earth Example

Matplotlib Example

hide()

def get_contour3d_plot():
    '''
    Create an example 3D contour plot.
    reference: https://matplotlib.org/stable/gallery/mplot3d/contour3d_3.html
    '''
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import axes3d
    ax = plt.figure().add_subplot(projection="3d")
    X, Y, Z = axes3d.get_test_data(0.05)
    # Plot the 3D surface
    ax.plot_surface(
        X, Y, Z, edgecolor="royalblue", lw=0.5, rstride=8, cstride=8, alpha=0.3
    )
    # Plot projections of the contours for each dimension.  By choosing offsets
    # that match the appropriate axes limits, the projected contours will sit on
    # the 'walls' of the graph.
    ax.contour(X, Y, Z, zdir="z", offset=-100, cmap="coolwarm")
    ax.contour(X, Y, Z, zdir="x", offset=-40, cmap="coolwarm")
    ax.contour(X, Y, Z, zdir="y", offset=40, cmap="coolwarm")
    ax.set(
        xlim=(-40, 40),
        ylim=(-40, 40),
        zlim=(-100, 100),
        xlabel="X",
        ylabel="Y",
        zlabel="Z",
    )
    return plt
show()
Plot(get_contour3d_plot(), caption="3D Contour Example")
3D Contour Example
3D Contour Example

Tables

Call the Table() function to create tables.

The table header definition may look complex, but it is flexible and powerful, supporting merged cells and other features. See the example below:

This rule is designed based on HTML table definitions. You can refer to HTML table materials for detailed usage.

In the example, rowspan means the number of rows a cell spans, and colspan means the number of columns a cell spans.

Table(
    [
        [
            th("Component", rowspan=3),
            th("Material", rowspan=3),
            th("Elastic Modulus (MPa)", colspan=2),
            th("Design Strength (MPa)", colspan=2),
            th("Standard Strength (MPa)", colspan=2),
        ],
        [
            "Ec/Es",
            "Compressive",
            "Tensile",
            "Compressive",
            "Tensile",
        ],
    ],
    [
        ["Cap Beam", "C60", 3.6e4, 26.5, 1.96, 38.5, 2.85],
        ["Cap Beam 2", "C60", 3.6e4, 26.5, 1.96, 38.5, 2.85],
    ],
    title="Example Table",
)
Example Table
ComponentMaterialElastic Modulus (MPa)Design Strength (MPa)Standard Strength (MPa)
Ec/EsCompressiveTensileCompressiveTensile
Cap BeamC603600026.51.9638.52.85
Cap Beam 2C603600026.51.9638.52.85

The Table() function returns the table reference. You can use it directly later, for example: the reference to the table above is .

Calling Existing Excel Calculation Tables

You can update cell values in an Excel workbook from a calculation report, execute formulas in Excel, then retrieve and output calculation results in the report.

This is useful for reusing existing Excel calculation tables.

The P function can output table content as a paragraph.

P(
    get_excel_table(
        excel_path="examples/data/calculation.xlsx",
        values={
            "Sheet2!A3": 6,
            "Sheet2!B3": 10,
            "Sheet2!C3": 2,
        },
        range="Sheet2!A1:D3",
    )
)

No Excel/xlwings engine available. Please install Excel/xlwings and try again.

By default, if the input values do not change, the Excel table is cached to speed up the next render.

Saving Documents

Saving as an HTML File

Inside a function defined with uzoncalc, use the save() function to save the document as an HTML file.

You can get the calculation context object from the return value of runsync(), then call the object's save() method to save the document.

ctx = run_sync(sheet2)
ctx.save("../output/example.en.html")
        

Printing as a PDF File

Open the generated HTML file in a browser, then use the browser's built-in print feature to save the document as a PDF file.

Converting to a Word Document

Use the pandoc command to convert an HTML document to a Word document. See the official pandoc documentation for detailed usage.

Future Plans

UzonCalc will add the following features in the future:

1. Add UI and calculation report publishing features.

2. Add AI support. After you accumulate enough calculation templates, AI can automatically generate initial drafts of new calculation reports.

Conclusion

AI has arrived. Standing still means falling behind. Let us embrace AI and begin a new calculation journey.