Skip to content

Compute fragments

ComputeFragmentsMixin

alert staticmethod

alert(headline, message, style=..., show=True, help_text='')

Adds an alert to the calculation report.

Parameters:

Name Type Description Default

headline

str

The headline of the alert, displayed prominently in bold.

required

message

str

The detailed message of the alert.

required

style

str

The style of the alert. Defaults to AlertStyle.Normal.

...

show

bool

Whether the alert is visible in the report. Default is True.

True

help_text

str

Tooltip text to display in the web app. Default is an empty string.

''

Returns:

Type Description
None

None

Examples:

1
2
3
4
5
cb.alert(
    headline="Calculation Warning",
    message="The input value exceeds the recommended range.",
    style=AlertStyle.Warning,
)

calc staticmethod

calc(key, equation=None, value=None, description='', reference='', unit='', format_str='{:.4g}', show=True, help_text='', override_previous=False)

Adds a calculation to the report.

Parameters:

Name Type Description Default

key

str

A unique identifier for the calculation.

required

equation

Optional[str]

The equation to compute. Variables must match previously defined keys.

None

value

Optional[NumericValueT]

The numerical value of the calculation. Default is None.

None

description

str

A description of the calculation. Default is an empty string.

''

reference

str

A reference or citation for the calculation. Default is an empty string.

''

unit

str

The unit of the calculation result. Default is an empty string.

''

format_str

str

The format string for displaying the result. Default is '{:.4g}'.

'{:.4g}'

show

bool

Whether the calculation is visible in the report. Default is True.

True

help_text

str

Tooltip text to display in the web app. Default is an empty string.

''

override_previous

bool

Whether to override a previously defined calculation with the same key. Default is False.

False

Returns:

Name Type Description
NumericValueT NumericValueT

The computed value of the calculation.

Examples:

1
2
3
4
5
6
cb.calc(
    key="area",
    equation="length * width",
    description="Area of the rectangle",
    unit="m^2"
)

collapsible staticmethod

collapsible(end=False, title='', show=True, help_text='')

Adds a container collapsible to the calculation report.

Parameters:

Name Type Description Default

end

bool

Marks whether the container has ended or not. Default = False.

False

title

str

Text to show in the collapsible bar. Default = "".

''

show

bool

Whether the image is visible in the app. Defaults to True.

True

help_text

str

Tooltip text to display in the web app. Defaults to an empty string.

''

Returns:

Type Description
None

None

dataframe staticmethod

dataframe(key, data, show_index=False, description='', show=True, help_text='')

Adds a dataFrame to the report.

Parameters:

Name Type Description Default

key

str

A unique identifier for the DataFrame.

required

data

DataFrame

The DataFrame to be added to the report.

required

show_index

bool

Whether to include the index in the displayed DataFrame. Defaults to False.

False

description

str

A description of the DataFrame. Defaults to an empty string.

''

show

bool

Whether the DataFrame is visible in the report. Defaults to True.

True

help_text

str

Tooltip text to display in the web app. Defaults to an empty string.

''

Returns:

Type Description
DataFrame

pd.DataFrame: The processed DataFrame (with or without the index, based on show_index).

decision staticmethod

decision(key, calc_key, headlines=None, messages=None, role_styles=None, util_bands=None, show_base_calc=False, description='', show=True, help_text='')

Adds a decision to the calculation report.

Parameters:

Name Type Description Default

key

str

A unique identifier for the decision.

required

calc_key

str

The key of the calculation this decision is based on.

required

headlines

Optional[List[str]]

A list of headline strings for the decision. Defaults to None.

None

messages

Optional[List[str]]

A list of messages corresponding to the decision. Defaults to None.

None

role_styles

Optional[List[AlertStyle]]

A list of styles for each role in the decision. Defaults to None.

None

util_bands

Optional[List[float]]

Utility bands for decision thresholds. Defaults to None.

None

show_base_calc

bool

Whether to show the base calculation in the report. Defaults to False.

False

description

str

A description of the calculation. Default is an empty string.

''

show

bool

Whether the decision is visible in the report. Defaults to True.

True

help_text

str

Tooltip text to display in the web app. Defaults to an empty string.

''

Returns:

Type Description
None

None

image staticmethod

image(image_filename, is_local=True, width=450, caption='', label='', image_renderer=None, description='', show=True, help_text='')

Adds an image to the calculation report.

Parameters:

Name Type Description Default

image_filename

str

The name of the image file to be added.

required

is_local

bool

Whether the image is stored locally in the app folder or in the shared folder. Defaults to True.

True

width

int

The width of the image in pixels. Defaults to 450.

450

caption

str

A caption for the image. Defaults to an empty string.

''

label

str

A label for the image. Defaults to an empty string.

''

image_renderer

Optional[ImageRendererBase]

The renderer to use for the image. If not provided, defaults to ImageRendererBasic.

None

description

str

A description of the image. Defaults to an empty string.

''

show

bool

Whether the image is visible in the app. Defaults to True.

True

help_text

str

Tooltip text to display in the web app. Defaults to an empty string.

''

Returns:

Type Description
None

None

plotly_chart staticmethod

plotly_chart(key, figure, width_override=None, description='', caption='', show=True, new_window_only=False, help_text='')

Adds a Plotly chart to the report.

Parameters:

Name Type Description Default

key

str

A unique identifier for the chart.

required

figure

Figure

The Plotly figure to be added to the report.

required

width_override

Optional[int]

Overrides the default width of the chart. Defaults to None.

None

description

str

The description of the chart. Defaults to an empty string.

''

caption

str

A caption for the chart. Defaults to an empty string.

''

show

bool

Whether the chart is visible in the report. Defaults to True.

True

new_window_only

bool

If True, the chart will only be displayed in a new window. Defaults to False.

False

help_text

str

Tooltip text to display in the web app. Defaults to an empty string.

''

Returns:

Type Description
None

None

plotly_chart_many staticmethod

plotly_chart_many(key, figures, description='', captions='', labels='', width_override=None, show=True, new_window_only=False, help_text='')

Adds Multiple Plotly charts to the calculation report.

Parameters:

Name Type Description Default

key

str

A unique identifier for the chart.

required

figures

List[Figure]

The Plotly figures to be added to the report.

required

description

str

The description of the group of charts. Defaults to an empty string.

''

width_override

Optional[int]

Overrides the default width of all charts. Defaults to None.

None

labels

Optional[List[str]]

The titles of the plotly charts. Defaults to a list of empty strings.

''

captions

Optional[List[str]]

The captions for each plotly chart. Defaults to a list of empty strings.

''

show

bool

Whether the chart is visible. Defaults to True.

True

new_window_only

bool

If True, the charts will only be displayed in a new window. Defaults to False.

False

help_text

str

Tooltip text to display in the web app. Defaults to an empty string.

''

Returns:

Type Description
None

None

pyplot staticmethod

pyplot(figure, width=450, caption='', label='', description='', show=True, is_expanded=True, help_text='')

Adds a Matplotlib figure to the calculation report.

Parameters:

Name Type Description Default

figure

Figure

The Matplotlib figure to be added to the report.

required

width

int

The width of the figure in pixels. Defaults to 450.

450

caption

str

A caption for the figure. Defaults to an empty string.

''

label

str

A label for the figure. Defaults to an empty string.

''

description

str

A description of the figure. Defaults to an empty string.

''

show

bool

Whether the figure is visible in the report. Defaults to True.

True

is_expanded

bool

Whether the figure is displayed in an expandable view. Defaults to True.

True

help_text

str

Tooltip text to display in the web app. Defaults to an empty string.

''

Returns:

Type Description
None

None

write staticmethod

write(text, style=None, show=True, help_text='')

Adds a text element to the calculation report.

Parameters:

Name Type Description Default

text

str

The string to represent within the calculation report.

required

style

Optional[str]

The style of the text. Options are ["p", "h1", "h2", "h3", "h4", "h5", "h6"]. If no style is provided, the text will be rendered in Markdown. Default is None.

None

show

bool

Whether the component is visible in the report. Default is True.

True

help_text

str

Tooltip text to display in the web app. Default is an empty string.

''

Returns:

Type Description
None

None

Examples:

1
cb.write("# Sample Calculation Heading")