Functions: @handcalc#
The @handcalc decorator turns a calculation function into one that returns both its
result and its rendered calculation.
from handcalcs import handcalc
from handcalcs.renderers import MarkdownRenderer
@handcalc(MarkdownRenderer())
def moment(w, L):
M = w * L**2 / 8 # Simply supported, uniform load
return M
value, markdown = moment(5, 6)
# value == 22.5
The function runs normally. handcalcs captures the values of its local variables (with innerscope), then renders the function body with those values.
Signature#
handcalc(renderer=None, render_context=None, destination=None, *, docstring=True, display=False)
rendererA renderer instance. If it is omitted, the renderer named by the
default_rendereroption is used (the ASCII renderer unless you change it), as forHandCalcs.@handcalc()and a bare@handcalcboth work.render_contextA dictionary of render context fields for every call, for example
{"format_code": ".2f"}. These override the global configuration.destinationA file path (a string or a
pathlib.Path). If given, every call writes the render to that file, asHandCalcsdoes, and returns itsPathin place of the render. Each call overwrites the file; to keep one file per call, callHandCalcsin a loop with your own file names.docstringWhether the function’s docstring is rendered, as a note above the calculation (
True, the default). Usedocstring=Falsefor a docstring written for the code’s readers rather than for the calculation, such as a function in a library that is also used without handcalcs. The comment lines between the docstring and the first statement are rendered either way.displayWhether each call shows the render rather than returning it (see Showing the render).
Falseby default.
The decorated function returns a tuple (return_value, rendered). The type of
rendered depends on the renderer, as for HandCalcs. With
display, it returns only return_value.
What is rendered#
The body of the function (its lines between the def line and the return), with the
values from that call. A docstring is shown as a note at the top, unless
docstring=False. Arguments, local variables, closure variables and globals can all
be used. Comment commands work as usual.
An early return ends the rendered calculation where the function returned: the lines
after it did not run, so they are not shown.
from math import sqrt
@handcalc(MarkdownRenderer(), render_context={"format_code": ".3g"})
def shear_capacity(b_w, d, f_c):
## Concrete shear resistance
phi_c = 0.65
beta = 0.18
V_c = phi_c * beta * sqrt(f_c) * b_w * d / 1000 # kN
return V_c
Showing the render#
With display=True, each call shows the render where the code is running and returns
only the function’s result:
@handcalc(MarkdownRenderer(), display=True)
def moment(w, L):
M = w * L**2 / 8
return M
M = moment(5, 6) # shows the calculation; M == 22.5
In Jupyter (a notebook, JupyterLab, VS Code, Colab), the render is displayed as rich output, as
%%renderdisplays it: Markdown for the Markdown renderer, LaTeX for the LaTeX renderer, and so on.Anywhere else (a script, a terminal), it is printed.
The Word and Excel renderers don’t render text, so use them with
destination. With adestination, the file is written and its path is shown.
display may also be a function, which each call passes the render to (or the file’s
path, with a destination). Use it to send the render somewhere else, such as a web
page or a list of calculations for a report:
import streamlit as st
@handcalc(MarkdownRenderer(), display=st.markdown)
def moment(w, L):
...
jupyter_display=True, from handcalcs 1, still works as an alias for display=True,
with a deprecation warning; it will be removed in handcalcs 3.0.