Coming from handcalcs v1#
handcalcs v2 is a rewrite. The parser now builds a tree of nodes, and separate renderers turn that tree into Markdown, LaTeX, HTML, plain text, Excel or Word. Most notebooks need only a few changes.
Note
pip install --upgrade handcalcs now installs 2.0. To keep a project on 1.x until you
have updated it, pin it: pip install "handcalcs<2" (or handcalcs<2 in your
requirements.txt or pyproject.toml).
Tip
hc-migrate makes most of the changes
below for you. hc-migrate beam.ipynb --to ipynb writes beam_v2.ipynb, converted for
handcalcs 2; --to py turns a notebook into a handcalcs 2 script instead. A folder
converts every notebook in it, and anything it can’t convert is listed for you to check.
Install it with pip install hc-migrate.
Quick reference#
v1 |
v2 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
the default (or |
|
|
|
|
|
|
|
still LaTeX math, in the LaTeX and Markdown renderers; other LaTeX in a comment ( |
|
|
|
|
|
the docstring is rendered as a note; |
|
|
|
renderer class attributes (see LaTeX and Markdown tokens) |
|
the |
|
not available in 2.0. Index the value instead ( |
|
the |
A command on its own line, such as # hc: -m, -f .2f or -p, stays in effect
for every line after it, until another command changes that setting (its scope is the
rest of the cell or file, not the next line). To apply a command to one line, put it at
the end of that line:
M_u = omega_2 * pi / L * sqrt(E * I_y * G * J + (pi * E / L)**2 * I_y * C_w) # hc: -m
M_p = Z_x * F_y # one line again
To turn a setting off again, use the assignment form: # hc: multiline = False,
# hc: param_line = False, # hc: format_code = ".5g". See
comment commands.
What’s new#
Renderers. Choose the output format, restyle it, or write your own.
A Python interface.
HandCalcs(renderer)(source)renders a string or a.pyfile outside Jupyter (see the object-oriented interface).Comment commands.
# hc:commands work per line or per section, instead of one override per cell.Headings and prose.
##comments become headings and comment lines become paragraphs, so a single cell can hold a whole documented calculation.Jinja in comments.
{{ name }}inserts a value into a comment.Nested conditionals and
forloops.Excel output with live formulas (see the Excel renderer).
Better names. Comma subscripts (
N_pl_Rd→ Npl,Rd), Greek letters joined to other letters (DeltaH→ ΔH), accents (V_dot→ V̇) and display specs (thesymbolsoption). See naming variables.Long equations break where the source line continues with
\, and a bare expression line shows its result (see writing calculations).%render, a line magic that runs and renders one line (see Jupyter).Checks and page breaks.
# hc: --checkmarks a comparison ✓ or ✗, and# hc: --pagebreakstarts a new page in exported output (see the cookbook).More Python. Conditional expressions (
a if test else b), methods on expressions ((a * b).to("mm")),cbrt,factorialand infinity.Text output for terminals and logs from
ASCIIRenderer(ascii), with underlined headings and=signs lined up (see the ASCII renderer). If you tried a 2.0 pre-release, this is the renderer that was calledPlainTextRenderer(plain_text).Word output with native equations (see the Word renderer).
Many input sets. Render one script for a table of inputs with
HandCalcs(...)(path, inputs=...)or%%render inputs=case.
Converting a cell#
A v1 cell with cell-wide overrides:
%%render params 2
b = 300
h = 600
%%render long 2
I_x = b * h**3 / 12
S_x = I_x / (h / 2)
becomes one v2 cell, with each setting where it applies:
## Section properties
# hc: -f .2f
b = 300; h = 600
I_x = b * h**3 / 12 # hc: -m
S_x = I_x / (h / 2)
Section properties
# Section properties
$$\begin{aligned} b &= 300.00 \; &h &= 600.00 \; \end{aligned}$$
$$\begin{aligned} I_{x} &= \frac{b \cdot h^{3}}{12} \\ &= \frac{300.00 \cdot 600.00^{3}}{12} \\ &= 5400000000.00 \\ S_{x} &= \frac{I_{x}}{\frac{h}{2}} = \frac{5400000000.00}{\frac{600.00}{2}} = 18000000.00 \end{aligned}$$
The heading and the ; grid replace a separate params cell, and -m applies to the
one long line rather than the whole cell.
Names that render differently#
v1 |
v2 |
To get the v1 output |
|---|---|---|
|
xa,b (comma) |
|
|
ΠD, φM, η2, χ2: a Greek name before an uppercase letter or digit is a letter |
|
|
accents and |
a |
|
|
— |
Global options that were removed#
The v1 options decimal_separator, latex_block_start, latex_block_end,
math_environment_start, math_environment_end, line_break,
use_scientific_notation, display_precision, underscore_subscripts,
greek_exclusions, param_columns, preferred_string_formatter, custom_symbols and
custom_brackets no longer exist. v2 has four options (format_code, default_renderer,
jupyter_renderer, line_values) plus any custom options; the rest
is controlled by the renderer.
An old config.json from v1 is not read; v2 stores its configuration in a new location
(see Configuration).