hc-migrate 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,160 @@
1
+ Metadata-Version: 2.4
2
+ Name: hc-migrate
3
+ Version: 0.2.0
4
+ Summary: Turn Jupyter notebooks into handcalcs v2 scripts, and migrate handcalcs v1 notebooks and scripts to v2.
5
+ Author: Connor Ferster
6
+ License-Expression: Apache-2.0
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: Framework :: Jupyter
9
+ Classifier: Environment :: Console
10
+ Requires-Dist: black>=25.1
11
+ Requires-Dist: nbconvert>=7.16 ; extra == 'nbconvert'
12
+ Requires-Python: >=3.11
13
+ Project-URL: Homepage, https://github.com/StructuralPython/hc-migrate
14
+ Project-URL: handcalcs v1 → v2 guide, https://handcalcs.structuralpython.com/getting-started/migrating-from-v1.html
15
+ Provides-Extra: nbconvert
16
+ Description-Content-Type: text/markdown
17
+
18
+ # hc-migrate
19
+
20
+ Two conversions for Jupyter notebooks and [handcalcs](https://github.com/connorferster/handcalcs):
21
+
22
+ 1. **`--to ipynb`**: update notebooks written for handcalcs v1 so they work with
23
+ handcalcs v2. They stay notebooks.
24
+ 2. **`--to py`** (the default): turn notebooks, written for v1 or v2, into
25
+ handcalcs v2 scripts (`.py`), ready to render or publish.
26
+
27
+ ```console
28
+ $ hc-migrate beam.ipynb --to ipynb --output beam_v2.ipynb
29
+ $ hc-migrate beam.ipynb --to py --output beam.py
30
+ $ hc-migrate calcs/ --to py --output scripts --report report.md
31
+ exported Concrete/Basic Beam Shear.ipynb -> Basic Beam Shear.py (5 changes, 0 warnings)
32
+ exported Wood/Nails in single shear.ipynb -> Nails in single shear.py (15 changes, 2 warnings)
33
+ ...
34
+ 6 notebooks exported, 6 warnings.
35
+ Wrote scripts
36
+ Report: report.md
37
+ ```
38
+
39
+ Without `--output`, a script is written next to its notebook (`beam.py`), and
40
+ a converted notebook gets `_v2` in its name (`beam_v2.ipynb`; a folder is
41
+ copied to `<folder>_v2`). **Existing files are never changed**, unless you
42
+ pass `--force`.
43
+
44
+ ## Notebooks to scripts (`--to py`)
45
+
46
+ A script renders as its notebook showed (with `handcalcs beam.py`,
47
+ `HandCalcs(...)(path)` or YMPrint's `_hc` block):
48
+
49
+ | In the notebook | In the script |
50
+ |---|---|
51
+ | A Markdown cell | Comment lines: prose, and headings at the same level |
52
+ | A `%%render` cell | Its lines; settings its commands changed are set back after it |
53
+ | Any other code cell | Its lines between `# hc: -i` and `# hc: -s`: run, not shown (`--show-code` shows them) |
54
+ | `%render` lines, `display(Markdown(f"..."))`, calls to `@handcalc(display=True)` functions | Shown |
55
+ | Magics, `!` commands, front matter | Dropped |
56
+
57
+ A notebook written for handcalcs v1 is converted to v2 first; v1 or v2 is told
58
+ from the source (`--from v1|v2` decides).
59
+
60
+ hc-migrate joins two earlier tools, handcalcs-migrate and nb_pyexporter.
61
+
62
+ ## Install
63
+
64
+ hc-migrate needs Python 3.11 or later (and black, which comes with it).
65
+
66
+ ```console
67
+ $ pipx install hc-migrate # or: uv tool install hc-migrate
68
+ $ uvx hc-migrate calcs/ # or run it once, without installing
69
+ ```
70
+
71
+ The scripts and converted notebooks need handcalcs 2.0.
72
+ `pip install "hc-migrate[nbconvert]"` adds `jupyter nbconvert --to hcpy`.
73
+
74
+ ## v1 notebooks to v2 (`--to ipynb`)
75
+
76
+ The same conversion runs before a v1 notebook is exported with `--to py`.
77
+ A `.py` file written for v1 is converted too.
78
+
79
+
80
+ | handcalcs v1 | handcalcs v2 |
81
+ |---|---|
82
+ | `import handcalcs.render`, `%load_ext handcalcs.render` | `import handcalcs` |
83
+ | `from handcalcs.decorator import handcalc` | `from handcalcs import handcalc` |
84
+ | `%%render 2` | `%%render` with `# hc: -f .2f` |
85
+ | `%%render sci_not`, `%%render 3 sci_not` | `# hc: -f .3e` |
86
+ | `%%render long` | `# hc: -m` |
87
+ | `%%render params` | `# hc: -g` above each run of assignments (a parameter grid) |
88
+ | `%%render symbolic` | `# hc: mode = "ass sym"` |
89
+ | `%%render short` | `%%render` (the default) |
90
+ | `%%tex` (printed the LaTeX source) | `%%source latex` (prints it in v2) |
91
+ | `%%render sympy` | `# hc: -y`: a `name = name` line whose right-hand name holds a SymPy expression runs as that expression |
92
+ | `## text` between equations | `# text` (in v2, `##` is a heading) |
93
+ | a `# comment` line (v1 didn't show it) | the same comment, wrapped in `# hc: -i` / `# hc: -s` so it stays hidden |
94
+ | `x = 1 # ignore` | `x = 1 # hc: -i` |
95
+ | `@handcalc(override="long", precision=2)` | `@handcalc(LatexRenderer(), render_context={"format_code": ".2f", "multiline": True})` |
96
+ | `@handcalc(jupyter_display=True)` | `@handcalc(LatexRenderer(), display=True)` |
97
+ | a decorated function with a docstring | adds `docstring=False` (v1 didn't render docstrings) |
98
+ | `latex, value = f(...)` for a v1-decorated `f` | `value, latex = f(...)` (v2 returns the value first) |
99
+ | `set_option("display_precision", 2)` | `set_option("format_code", ".2f")` |
100
+ | `set_option("param_columns", 4)` | `set_option("param_cols", 4)` |
101
+ | options v2 removed (`latex_block_start`, `custom_symbols`, ...) | commented out, with the reason |
102
+
103
+ v1's `%%render` arguments are read exactly as v1 read them, including its
104
+ habit of accepting part of a word (`%%render param` is `params`).
105
+
106
+ ## What it asks you to check
107
+
108
+ Some v1 code has no direct v2 equivalent. It is converted as far as it can be,
109
+ and listed under **warnings** (in the terminal and in `--report`):
110
+
111
+ - `@handcalc(left=..., right=...)` and `@handcalc(record=True)`.
112
+ - Uses of a decorated function's result other than `latex, value = f(...)`:
113
+ v1 returned `(latex, value)` and v2 returns `(value, latex)`.
114
+ - `custom_symbols`, `custom_brackets`, `greek_exclusions` and other removed
115
+ options.
116
+
117
+ Names also render a little differently in v2: `x_a_b` is x<sub>a,b</sub> (not
118
+ nested), and `phiM` is φM. `--v1-names` keeps the v1 look.
119
+
120
+ ## Options
121
+
122
+ ```text
123
+ -o, --output DEST where to write: a file, or a folder for a tree
124
+ --to {py,ipynb} py: notebooks become v2 scripts (default);
125
+ ipynb: v1 notebooks are converted to v2 and stay notebooks
126
+ --from {auto,v1,v2} the handcalcs the source was written for (default: auto)
127
+ -f, --force overwrite files (or DEST) that exist (also --overwrite)
128
+ -n, --dry-run report what would be written, and write nothing
129
+ --report FILE also write the report to FILE (Markdown)
130
+ --show-code scripts: show the code cells the notebook didn't render
131
+ --no-black scripts: don't format with black
132
+ --keep-v1-precision v1: 3 decimal places where v1 used its default
133
+ (v2's default is 5 significant figures)
134
+ --v1-names v1: keep v1's nested subscripts and Greek names
135
+ --clear-outputs --to ipynb: clear the outputs of converted cells
136
+ --exclude PATTERN skip matching files and folders (repeatable)
137
+ --only-converted with a folder DEST, write only notebooks and Python files
138
+ -q, --quiet report only files with warnings
139
+ ```
140
+
141
+ `.git`, `.ipynb_checkpoints`, `__pycache__` and `.venv` are always skipped.
142
+ `python -m hc_migrate` is the same command.
143
+
144
+ See the handcalcs guide,
145
+ [Coming from handcalcs v1](https://handcalcs.structuralpython.com/getting-started/migrating-from-v1.html),
146
+ for everything that changed.
147
+
148
+ ## Development
149
+
150
+ ```console
151
+ $ uv run --group test pytest
152
+ ```
153
+
154
+ The tests convert a v1 notebook and run the result with handcalcs v2 in an
155
+ IPython shell, so every converted cell is checked to work, and render the
156
+ script exported from it with handcalcs.
157
+
158
+ ## License
159
+
160
+ Apache 2.0.
@@ -0,0 +1,143 @@
1
+ # hc-migrate
2
+
3
+ Two conversions for Jupyter notebooks and [handcalcs](https://github.com/connorferster/handcalcs):
4
+
5
+ 1. **`--to ipynb`**: update notebooks written for handcalcs v1 so they work with
6
+ handcalcs v2. They stay notebooks.
7
+ 2. **`--to py`** (the default): turn notebooks, written for v1 or v2, into
8
+ handcalcs v2 scripts (`.py`), ready to render or publish.
9
+
10
+ ```console
11
+ $ hc-migrate beam.ipynb --to ipynb --output beam_v2.ipynb
12
+ $ hc-migrate beam.ipynb --to py --output beam.py
13
+ $ hc-migrate calcs/ --to py --output scripts --report report.md
14
+ exported Concrete/Basic Beam Shear.ipynb -> Basic Beam Shear.py (5 changes, 0 warnings)
15
+ exported Wood/Nails in single shear.ipynb -> Nails in single shear.py (15 changes, 2 warnings)
16
+ ...
17
+ 6 notebooks exported, 6 warnings.
18
+ Wrote scripts
19
+ Report: report.md
20
+ ```
21
+
22
+ Without `--output`, a script is written next to its notebook (`beam.py`), and
23
+ a converted notebook gets `_v2` in its name (`beam_v2.ipynb`; a folder is
24
+ copied to `<folder>_v2`). **Existing files are never changed**, unless you
25
+ pass `--force`.
26
+
27
+ ## Notebooks to scripts (`--to py`)
28
+
29
+ A script renders as its notebook showed (with `handcalcs beam.py`,
30
+ `HandCalcs(...)(path)` or YMPrint's `_hc` block):
31
+
32
+ | In the notebook | In the script |
33
+ |---|---|
34
+ | A Markdown cell | Comment lines: prose, and headings at the same level |
35
+ | A `%%render` cell | Its lines; settings its commands changed are set back after it |
36
+ | Any other code cell | Its lines between `# hc: -i` and `# hc: -s`: run, not shown (`--show-code` shows them) |
37
+ | `%render` lines, `display(Markdown(f"..."))`, calls to `@handcalc(display=True)` functions | Shown |
38
+ | Magics, `!` commands, front matter | Dropped |
39
+
40
+ A notebook written for handcalcs v1 is converted to v2 first; v1 or v2 is told
41
+ from the source (`--from v1|v2` decides).
42
+
43
+ hc-migrate joins two earlier tools, handcalcs-migrate and nb_pyexporter.
44
+
45
+ ## Install
46
+
47
+ hc-migrate needs Python 3.11 or later (and black, which comes with it).
48
+
49
+ ```console
50
+ $ pipx install hc-migrate # or: uv tool install hc-migrate
51
+ $ uvx hc-migrate calcs/ # or run it once, without installing
52
+ ```
53
+
54
+ The scripts and converted notebooks need handcalcs 2.0.
55
+ `pip install "hc-migrate[nbconvert]"` adds `jupyter nbconvert --to hcpy`.
56
+
57
+ ## v1 notebooks to v2 (`--to ipynb`)
58
+
59
+ The same conversion runs before a v1 notebook is exported with `--to py`.
60
+ A `.py` file written for v1 is converted too.
61
+
62
+
63
+ | handcalcs v1 | handcalcs v2 |
64
+ |---|---|
65
+ | `import handcalcs.render`, `%load_ext handcalcs.render` | `import handcalcs` |
66
+ | `from handcalcs.decorator import handcalc` | `from handcalcs import handcalc` |
67
+ | `%%render 2` | `%%render` with `# hc: -f .2f` |
68
+ | `%%render sci_not`, `%%render 3 sci_not` | `# hc: -f .3e` |
69
+ | `%%render long` | `# hc: -m` |
70
+ | `%%render params` | `# hc: -g` above each run of assignments (a parameter grid) |
71
+ | `%%render symbolic` | `# hc: mode = "ass sym"` |
72
+ | `%%render short` | `%%render` (the default) |
73
+ | `%%tex` (printed the LaTeX source) | `%%source latex` (prints it in v2) |
74
+ | `%%render sympy` | `# hc: -y`: a `name = name` line whose right-hand name holds a SymPy expression runs as that expression |
75
+ | `## text` between equations | `# text` (in v2, `##` is a heading) |
76
+ | a `# comment` line (v1 didn't show it) | the same comment, wrapped in `# hc: -i` / `# hc: -s` so it stays hidden |
77
+ | `x = 1 # ignore` | `x = 1 # hc: -i` |
78
+ | `@handcalc(override="long", precision=2)` | `@handcalc(LatexRenderer(), render_context={"format_code": ".2f", "multiline": True})` |
79
+ | `@handcalc(jupyter_display=True)` | `@handcalc(LatexRenderer(), display=True)` |
80
+ | a decorated function with a docstring | adds `docstring=False` (v1 didn't render docstrings) |
81
+ | `latex, value = f(...)` for a v1-decorated `f` | `value, latex = f(...)` (v2 returns the value first) |
82
+ | `set_option("display_precision", 2)` | `set_option("format_code", ".2f")` |
83
+ | `set_option("param_columns", 4)` | `set_option("param_cols", 4)` |
84
+ | options v2 removed (`latex_block_start`, `custom_symbols`, ...) | commented out, with the reason |
85
+
86
+ v1's `%%render` arguments are read exactly as v1 read them, including its
87
+ habit of accepting part of a word (`%%render param` is `params`).
88
+
89
+ ## What it asks you to check
90
+
91
+ Some v1 code has no direct v2 equivalent. It is converted as far as it can be,
92
+ and listed under **warnings** (in the terminal and in `--report`):
93
+
94
+ - `@handcalc(left=..., right=...)` and `@handcalc(record=True)`.
95
+ - Uses of a decorated function's result other than `latex, value = f(...)`:
96
+ v1 returned `(latex, value)` and v2 returns `(value, latex)`.
97
+ - `custom_symbols`, `custom_brackets`, `greek_exclusions` and other removed
98
+ options.
99
+
100
+ Names also render a little differently in v2: `x_a_b` is x<sub>a,b</sub> (not
101
+ nested), and `phiM` is φM. `--v1-names` keeps the v1 look.
102
+
103
+ ## Options
104
+
105
+ ```text
106
+ -o, --output DEST where to write: a file, or a folder for a tree
107
+ --to {py,ipynb} py: notebooks become v2 scripts (default);
108
+ ipynb: v1 notebooks are converted to v2 and stay notebooks
109
+ --from {auto,v1,v2} the handcalcs the source was written for (default: auto)
110
+ -f, --force overwrite files (or DEST) that exist (also --overwrite)
111
+ -n, --dry-run report what would be written, and write nothing
112
+ --report FILE also write the report to FILE (Markdown)
113
+ --show-code scripts: show the code cells the notebook didn't render
114
+ --no-black scripts: don't format with black
115
+ --keep-v1-precision v1: 3 decimal places where v1 used its default
116
+ (v2's default is 5 significant figures)
117
+ --v1-names v1: keep v1's nested subscripts and Greek names
118
+ --clear-outputs --to ipynb: clear the outputs of converted cells
119
+ --exclude PATTERN skip matching files and folders (repeatable)
120
+ --only-converted with a folder DEST, write only notebooks and Python files
121
+ -q, --quiet report only files with warnings
122
+ ```
123
+
124
+ `.git`, `.ipynb_checkpoints`, `__pycache__` and `.venv` are always skipped.
125
+ `python -m hc_migrate` is the same command.
126
+
127
+ See the handcalcs guide,
128
+ [Coming from handcalcs v1](https://handcalcs.structuralpython.com/getting-started/migrating-from-v1.html),
129
+ for everything that changed.
130
+
131
+ ## Development
132
+
133
+ ```console
134
+ $ uv run --group test pytest
135
+ ```
136
+
137
+ The tests convert a v1 notebook and run the result with handcalcs v2 in an
138
+ IPython shell, so every converted cell is checked to work, and render the
139
+ script exported from it with handcalcs.
140
+
141
+ ## License
142
+
143
+ Apache 2.0.
@@ -0,0 +1,49 @@
1
+ [project]
2
+ name = "hc-migrate"
3
+ version = "0.2.0"
4
+ description = "Turn Jupyter notebooks into handcalcs v2 scripts, and migrate handcalcs v1 notebooks and scripts to v2."
5
+ readme = "README.md"
6
+ license = "Apache-2.0"
7
+ requires-python = ">=3.11"
8
+ dependencies = ["black>=25.1"]
9
+ classifiers = [
10
+ "Programming Language :: Python :: 3",
11
+ "Framework :: Jupyter",
12
+ "Environment :: Console",
13
+ ]
14
+
15
+ [[project.authors]]
16
+ name = "Connor Ferster"
17
+
18
+ [project.optional-dependencies]
19
+ nbconvert = ["nbconvert>=7.16"]
20
+
21
+ [project.scripts]
22
+ hc-migrate = "hc_migrate.cli:main"
23
+
24
+ [project.entry-points."nbconvert.exporters"]
25
+ hcpy = "hc_migrate.nbconvert_exporter:HandcalcsScriptExporter"
26
+
27
+ [project.urls]
28
+ Homepage = "https://github.com/StructuralPython/hc-migrate"
29
+ "handcalcs v1 → v2 guide" = "https://handcalcs.structuralpython.com/getting-started/migrating-from-v1.html"
30
+
31
+ [dependency-groups]
32
+ test = [
33
+ "pytest>=8",
34
+ "ipython>=9.18.1",
35
+ "handcalcs>=2.0.0rc5",
36
+ "sympy>=1.12",
37
+ "nbconvert>=7.16",
38
+ ]
39
+ lint = ["black>=25.1"]
40
+ doc = [
41
+ "sphinx>=8.1",
42
+ "shibuya>=2025.1",
43
+ "myst-parser>=4.0",
44
+ "sphinx-copybutton>=0.5",
45
+ ]
46
+
47
+ [build-system]
48
+ requires = ["uv_build>=0.8,<0.12"]
49
+ build-backend = "uv_build"
@@ -0,0 +1,51 @@
1
+ [project]
2
+ name = "hc-migrate"
3
+ version = "0.2.0"
4
+ description = "Turn Jupyter notebooks into handcalcs v2 scripts, and migrate handcalcs v1 notebooks and scripts to v2."
5
+ readme = "README.md"
6
+ license = "Apache-2.0"
7
+ authors = [{ name = "Connor Ferster" }]
8
+ requires-python = ">=3.11"
9
+ # black formats the exported scripts.
10
+ dependencies = ["black>=25.1"]
11
+ classifiers = [
12
+ "Programming Language :: Python :: 3",
13
+ "Framework :: Jupyter",
14
+ "Environment :: Console",
15
+ ]
16
+
17
+ [project.optional-dependencies]
18
+ # `jupyter nbconvert --to hcpy`: the export as an nbconvert exporter.
19
+ nbconvert = ["nbconvert>=7.16"]
20
+
21
+ [project.scripts]
22
+ hc-migrate = "hc_migrate.cli:main"
23
+
24
+ [project.entry-points."nbconvert.exporters"]
25
+ hcpy = "hc_migrate.nbconvert_exporter:HandcalcsScriptExporter"
26
+
27
+ [project.urls]
28
+ Homepage = "https://github.com/StructuralPython/hc-migrate"
29
+ "handcalcs v1 → v2 guide" = "https://handcalcs.structuralpython.com/getting-started/migrating-from-v1.html"
30
+
31
+ [dependency-groups]
32
+ # handcalcs v2 checks that converted cells parse and render.
33
+ test = [
34
+ "pytest>=8",
35
+ "ipython>=9.18.1",
36
+ "handcalcs>=2.0.0rc5",
37
+ "sympy>=1.12",
38
+ "nbconvert>=7.16",
39
+ ]
40
+ lint = ["black>=25.1"]
41
+ # The documentation site (docs/), built and published by publish-docs-to-bunny.yml.
42
+ doc = [
43
+ "sphinx>=8.1",
44
+ "shibuya>=2025.1",
45
+ "myst-parser>=4.0",
46
+ "sphinx-copybutton>=0.5",
47
+ ]
48
+
49
+ [build-system]
50
+ requires = ["uv_build>=0.8,<0.12"]
51
+ build-backend = "uv_build"
@@ -0,0 +1,3 @@
1
+ """Turn notebooks into handcalcs v2 scripts, and migrate handcalcs v1 notebooks and scripts to v2."""
2
+
3
+ __version__ = "0.2.0"
@@ -0,0 +1,5 @@
1
+ import sys
2
+
3
+ from .cli import main
4
+
5
+ sys.exit(main())