nhandu 0.1.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.
- nhandu-0.1.0/LICENSE +21 -0
- nhandu-0.1.0/PKG-INFO +305 -0
- nhandu-0.1.0/README.md +264 -0
- nhandu-0.1.0/pyproject.toml +125 -0
- nhandu-0.1.0/setup.cfg +4 -0
- nhandu-0.1.0/src/nhandu/__init__.py +9 -0
- nhandu-0.1.0/src/nhandu/__main__.py +198 -0
- nhandu-0.1.0/src/nhandu/executor.py +235 -0
- nhandu-0.1.0/src/nhandu/models.py +111 -0
- nhandu-0.1.0/src/nhandu/parser.py +139 -0
- nhandu-0.1.0/src/nhandu/parser_py.py +217 -0
- nhandu-0.1.0/src/nhandu/renderer.py +344 -0
- nhandu-0.1.0/src/nhandu.egg-info/PKG-INFO +305 -0
- nhandu-0.1.0/src/nhandu.egg-info/SOURCES.txt +20 -0
- nhandu-0.1.0/src/nhandu.egg-info/dependency_links.txt +1 -0
- nhandu-0.1.0/src/nhandu.egg-info/entry_points.txt +2 -0
- nhandu-0.1.0/src/nhandu.egg-info/requires.txt +21 -0
- nhandu-0.1.0/src/nhandu.egg-info/top_level.txt +1 -0
- nhandu-0.1.0/tests/test_cli.py +282 -0
- nhandu-0.1.0/tests/test_executor.py +252 -0
- nhandu-0.1.0/tests/test_parser.py +169 -0
- nhandu-0.1.0/tests/test_renderer.py +415 -0
nhandu-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Tiago Tresoldi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
nhandu-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nhandu
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A literate programming tool for Python that weaves code and documentation into scientific reports
|
|
5
|
+
Author-email: Tiago Tresoldi <tiago.tresoldi@lingfil.uu.se>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/tresoldi/nhandu
|
|
8
|
+
Project-URL: Issues, https://github.com/tresoldi/nhandu/issues
|
|
9
|
+
Keywords: literate-programming,scientific-computing,reports,documentation
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Documentation
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: pyyaml>=6.0
|
|
23
|
+
Requires-Dist: mistune>=3.0
|
|
24
|
+
Requires-Dist: pygments>=2.19
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
28
|
+
Requires-Dist: mypy>=1.0; extra == "dev"
|
|
29
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
30
|
+
Requires-Dist: types-PyYAML; extra == "dev"
|
|
31
|
+
Requires-Dist: types-Pygments; extra == "dev"
|
|
32
|
+
Requires-Dist: build>=1.0; extra == "dev"
|
|
33
|
+
Requires-Dist: twine>=4.0; extra == "dev"
|
|
34
|
+
Provides-Extra: test
|
|
35
|
+
Requires-Dist: numpy>=1.20; extra == "test"
|
|
36
|
+
Requires-Dist: pandas>=2.0; extra == "test"
|
|
37
|
+
Requires-Dist: matplotlib>=3.5; extra == "test"
|
|
38
|
+
Provides-Extra: all
|
|
39
|
+
Requires-Dist: nhandu[dev,test]; extra == "all"
|
|
40
|
+
Dynamic: license-file
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
# Nhandu
|
|
44
|
+
|
|
45
|
+
> *Nhandu* (/ɲãndu/, approximately "NYAN-doo") means "spider" in many Tupi-Guarani languages, a fitting name for a tool that weaves together code and documentation, much like Knuth's original vision of literate programming.
|
|
46
|
+
|
|
47
|
+
**Literate programming for Python: Write executable documents in plain `.py` files.**
|
|
48
|
+
|
|
49
|
+
Nhandu transforms ordinary Python files with markdown comments into beautiful, reproducible reports. It's lighter than Jupyter, simpler than Quarto, and perfectly git-friendly.
|
|
50
|
+
|
|
51
|
+
## Why Nhandu?
|
|
52
|
+
|
|
53
|
+
Contemporary literate programming in Python faces a documentation dilemma:
|
|
54
|
+
|
|
55
|
+
- **Jupyter notebooks** are powerful but use JSON format (git diffs are messy), require a browser/server, and mix code with metadata
|
|
56
|
+
- **Quarto** is feature-rich but complex, with many configuration options and a learning curve
|
|
57
|
+
- **Pweave** has not been maintained for many years and is incompatible with currently supported Python versions.
|
|
58
|
+
- **Traditional scripts** lack integrated documentation and visualization
|
|
59
|
+
|
|
60
|
+
Nhandu offers a different solution:
|
|
61
|
+
|
|
62
|
+
- Write literate programs in **normal `.py` files**: no special format, just comments
|
|
63
|
+
- **Perfect git diffs**: plain text, not JSON, no timestamps, no hashes
|
|
64
|
+
- **No server or browser** required—just run the command
|
|
65
|
+
- **Zero configuration needed**: smart defaults get you started immediately
|
|
66
|
+
- **Python-native**: designed specifically for the Python ecosystem
|
|
67
|
+
|
|
68
|
+
Nhandu also supports traditional `.md` files with code blocks if you prefer that style.
|
|
69
|
+
|
|
70
|
+
## Quick Start
|
|
71
|
+
|
|
72
|
+
### Your First Literate Program
|
|
73
|
+
|
|
74
|
+
Create a file `analysis.py`:
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
#' # My First Analysis
|
|
78
|
+
#'
|
|
79
|
+
#' This is a literate Python program. Lines starting with `#'` are markdown.
|
|
80
|
+
#' Everything else is regular Python code.
|
|
81
|
+
|
|
82
|
+
import numpy as np
|
|
83
|
+
|
|
84
|
+
# Generate some data
|
|
85
|
+
data = np.random.normal(0, 1, 1000)
|
|
86
|
+
|
|
87
|
+
#' ## Results
|
|
88
|
+
#'
|
|
89
|
+
#' Let's compute some statistics:
|
|
90
|
+
|
|
91
|
+
print(f"Mean: {data.mean():.3f}")
|
|
92
|
+
print(f"Std Dev: {data.std():.3f}")
|
|
93
|
+
|
|
94
|
+
#' The data looks normally distributed, as expected.
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Generate Your Report
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
nhandu analysis.py
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
This creates `analysis.html` with your code, output, and nicely formatted documentation, from a plain Python script.
|
|
104
|
+
|
|
105
|
+
## Features
|
|
106
|
+
|
|
107
|
+
### Smart Output Capture
|
|
108
|
+
|
|
109
|
+
Nhandu automatically captures:
|
|
110
|
+
|
|
111
|
+
- **Print statements** and stdout
|
|
112
|
+
- **Matplotlib plots** (no `plt.show()` needed!)
|
|
113
|
+
- **Expression results** (like Jupyter cells)
|
|
114
|
+
- **Rich objects** (DataFrames render as tables in HTML)
|
|
115
|
+
|
|
116
|
+
### Syntax Highlighting
|
|
117
|
+
|
|
118
|
+
Server-side syntax highlighting with 50+ themes via Pygments. Popular themes include: `github-dark` (default), `monokai`, `dracula`, `one-dark`, `vs`, and `solarized-light`.
|
|
119
|
+
|
|
120
|
+
### Multiple Output Formats
|
|
121
|
+
|
|
122
|
+
Markdown output can be converted to PDF, Word, LaTeX, and more using [pandoc](https://pandoc.org/) or similar tools. Native HTML support is implemented out-of-the-box.
|
|
123
|
+
|
|
124
|
+
### Configuration (Optional)
|
|
125
|
+
|
|
126
|
+
Power users can customize their reports via YAML frontmatter:
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
#' ---
|
|
130
|
+
#' title: My Scientific Report
|
|
131
|
+
#' output: html
|
|
132
|
+
#' code_theme: dracula
|
|
133
|
+
#' plot_dpi: 150
|
|
134
|
+
#' ---
|
|
135
|
+
#'
|
|
136
|
+
#' # Introduction
|
|
137
|
+
#' ...
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
It is also possible to use a configuration file (`nhandu.yaml`) or CLI arguments.
|
|
141
|
+
|
|
142
|
+
## How It Works
|
|
143
|
+
|
|
144
|
+
### Literate Python Format (`.py` files)
|
|
145
|
+
|
|
146
|
+
Nhandu treats Python files specially when they contain markdown comments:
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
#' # This is a markdown heading
|
|
150
|
+
#'
|
|
151
|
+
#' Any line starting with #' is treated as **markdown**.
|
|
152
|
+
#' You can use all standard markdown features.
|
|
153
|
+
|
|
154
|
+
# This is a regular Python comment
|
|
155
|
+
x = 42 # Regular code continues to work normally
|
|
156
|
+
|
|
157
|
+
#' Back to documentation. Variables persist across blocks:
|
|
158
|
+
|
|
159
|
+
print(f"x = {x}")
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
**Hidden code blocks** let you run setup code without cluttering your report:
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
#| hide
|
|
166
|
+
import pandas as pd
|
|
167
|
+
import matplotlib.pyplot as plt
|
|
168
|
+
# Configuration code here—runs but doesn't appear in output
|
|
169
|
+
#|
|
|
170
|
+
|
|
171
|
+
#' Now let's analyze our data:
|
|
172
|
+
# This code WILL appear in the output
|
|
173
|
+
data = pd.read_csv("data.csv")
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Traditional Markdown Format (`.md` files)
|
|
177
|
+
|
|
178
|
+
You can also use standard markdown files with code blocks:
|
|
179
|
+
|
|
180
|
+
````markdown
|
|
181
|
+
# My Analysis
|
|
182
|
+
|
|
183
|
+
Here's some Python code:
|
|
184
|
+
|
|
185
|
+
```python
|
|
186
|
+
import numpy as np
|
|
187
|
+
x = np.linspace(0, 10, 100)
|
|
188
|
+
print(f"Generated {len(x)} points")
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
The output will appear in your rendered document. In case of HTML output, any figures are embedded in the file, so that you have a single file to distribute.
|
|
192
|
+
|
|
193
|
+
````
|
|
194
|
+
|
|
195
|
+
### Execution Model
|
|
196
|
+
|
|
197
|
+
- **Shared namespace**: All code blocks share the same Python environment
|
|
198
|
+
- **Sequential execution**: Blocks run in document order
|
|
199
|
+
- **Output capture**: stdout, plots, and expression results are all captured
|
|
200
|
+
- **Rich formatting**: Automatic handling of matplotlib figures, pandas DataFrames, and more
|
|
201
|
+
|
|
202
|
+
## Examples
|
|
203
|
+
|
|
204
|
+
Check out the [`examples/`](https://github.com/tresoldi/nhandu/tree/main/examples/) directory for complete demonstrations:
|
|
205
|
+
|
|
206
|
+
- **[01_hello_world.py](https://github.com/tresoldi/nhandu/tree/main/examples/01_hello_world.py)** - Basic syntax and concepts [[OUTPUT](https://htmlpreview.github.io/?https://github.com/tresoldi/nhandu/blob/main/examples/01_hello_world.py.html)]
|
|
207
|
+
- **[02_data_analysis.py](https://github.com/tresoldi/nhandu/tree/main/examples/02_data_analysis.py)** - Working with data using standard library [[OUTPUT](https://htmlpreview.github.io/?https://github.com/tresoldi/nhandu/blob/main/examples/02_data_analysis.py.html)]
|
|
208
|
+
- **[03_plotting.py](https://github.com/tresoldi/nhandu/tree/main/examples/03_plotting.py)** - Creating visualizations with matplotlib [[OUTPUT](https://htmlpreview.github.io/?https://github.com/tresoldi/nhandu/blob/main/examples/03_plotting.py.html)]
|
|
209
|
+
- **[04_scientific_computation.py](https://github.com/tresoldi/nhandu/tree/main/examples/04_scientific_computation.py)** - Numerical computing with NumPy [[OUTPUT](https://htmlpreview.github.io/?https://github.com/tresoldi/nhandu/blob/main/examples/04_scientific_computation.py.html)]
|
|
210
|
+
- **[05_advanced_report.py](https://github.com/tresoldi/nhandu/tree/main/examples/05_advanced_report.py)** - Complex report with pandas and multiple visualizations [[OUTPUT](https://htmlpreview.github.io/?https://github.com/tresoldi/nhandu/blob/main/examples/05_advanced_report.py.html)]
|
|
211
|
+
|
|
212
|
+
## Installation & Usage
|
|
213
|
+
|
|
214
|
+
### Install from PyPI
|
|
215
|
+
|
|
216
|
+
```bash
|
|
217
|
+
pip install nhandu
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### Install from Source
|
|
221
|
+
|
|
222
|
+
```bash
|
|
223
|
+
git clone https://github.com/tresoldi/nhandu.git
|
|
224
|
+
cd nhandu
|
|
225
|
+
pip install -e .
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Basic Usage
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
nhandu document.py # Process literate Python file → document.html
|
|
232
|
+
nhandu document.md # Process markdown file → document.html
|
|
233
|
+
nhandu document.py -o report.html # Specify output file
|
|
234
|
+
nhandu document.py --format md # Output as markdown
|
|
235
|
+
nhandu document.py --code-theme monokai # Custom syntax theme
|
|
236
|
+
nhandu document.py --verbose # Show processing details
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### CLI Options
|
|
240
|
+
|
|
241
|
+
```
|
|
242
|
+
nhandu [OPTIONS] INPUT
|
|
243
|
+
|
|
244
|
+
Options:
|
|
245
|
+
-o, --output PATH Output file path
|
|
246
|
+
--format {html,md} Output format (default: html)
|
|
247
|
+
--config PATH Configuration file (YAML)
|
|
248
|
+
--working-dir PATH Working directory for code execution
|
|
249
|
+
--timeout SECONDS Execution timeout
|
|
250
|
+
--code-theme THEME Syntax highlighting theme
|
|
251
|
+
--verbose, -v Enable verbose output
|
|
252
|
+
--version Show version
|
|
253
|
+
--help Show help message
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### Roadmap
|
|
257
|
+
|
|
258
|
+
Current priorities:
|
|
259
|
+
|
|
260
|
+
- [ ] Native PDF output support
|
|
261
|
+
- [ ] Inline code evaluation (`<%= expression %>` syntax)
|
|
262
|
+
- [ ] Custom HTML templates (Jinja2)
|
|
263
|
+
- [ ] Watch mode for live development
|
|
264
|
+
- [ ] Rich display for more object types (NumPy arrays, scikit-learn models)
|
|
265
|
+
- [ ] Caching system for faster re-renders
|
|
266
|
+
|
|
267
|
+
See [issues](https://github.com/tresoldi/nhandu/issues) for more details and to suggest features.
|
|
268
|
+
|
|
269
|
+
## Project Information
|
|
270
|
+
|
|
271
|
+
### Citation and Acknowledgements
|
|
272
|
+
|
|
273
|
+
If you use Nhandu in your research, please cite:
|
|
274
|
+
|
|
275
|
+
```bibtex
|
|
276
|
+
@software{tresoldi2025nhandu,
|
|
277
|
+
author = {Tresoldi, Tiago},
|
|
278
|
+
title = {Nhandu: Literate Programming for Python},
|
|
279
|
+
year = {2025},
|
|
280
|
+
publisher = {Department of Linguistics and Philology, Uppsala University},
|
|
281
|
+
address = {Uppsala, Sweden},
|
|
282
|
+
url = {https://github.com/tresoldi/nhandu},
|
|
283
|
+
orcid = {0000-0002-2863-1467}
|
|
284
|
+
}
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
The earliest stages of development took place within the context of
|
|
288
|
+
the [Cultural Evolution of Texts](https://github.com/evotext/) project, with funding from the
|
|
289
|
+
[Riksbankens Jubileumsfond](https://www.rj.se/) (grant agreement ID:
|
|
290
|
+
[MXM19-1087:1](https://www.rj.se/en/anslag/2019/cultural-evolution-of-texts/)).
|
|
291
|
+
|
|
292
|
+
### License
|
|
293
|
+
|
|
294
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
295
|
+
|
|
296
|
+
### Acknowledgments
|
|
297
|
+
|
|
298
|
+
Nhandu is inspired by:
|
|
299
|
+
- Donald Knuth's original [literate programming](https://en.wikipedia.org/wiki/Literate_programming) vision
|
|
300
|
+
- [knitr](https://yihui.org/knitr/) and R Markdown's approach to reproducible research
|
|
301
|
+
- [Jupyter](https://jupyter.org/)'s interactive computing paradigm
|
|
302
|
+
- [Quarto](https://quarto.org/)'s modern scientific publishing tools
|
|
303
|
+
- [Pweave](http://mpastell.com/pweave/)'s Python implementation (though no longer maintained)
|
|
304
|
+
|
|
305
|
+
Special thanks to the scientific Python community for building the ecosystem that makes tools like this possible.
|
nhandu-0.1.0/README.md
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
|
|
2
|
+
# Nhandu
|
|
3
|
+
|
|
4
|
+
> *Nhandu* (/ɲãndu/, approximately "NYAN-doo") means "spider" in many Tupi-Guarani languages, a fitting name for a tool that weaves together code and documentation, much like Knuth's original vision of literate programming.
|
|
5
|
+
|
|
6
|
+
**Literate programming for Python: Write executable documents in plain `.py` files.**
|
|
7
|
+
|
|
8
|
+
Nhandu transforms ordinary Python files with markdown comments into beautiful, reproducible reports. It's lighter than Jupyter, simpler than Quarto, and perfectly git-friendly.
|
|
9
|
+
|
|
10
|
+
## Why Nhandu?
|
|
11
|
+
|
|
12
|
+
Contemporary literate programming in Python faces a documentation dilemma:
|
|
13
|
+
|
|
14
|
+
- **Jupyter notebooks** are powerful but use JSON format (git diffs are messy), require a browser/server, and mix code with metadata
|
|
15
|
+
- **Quarto** is feature-rich but complex, with many configuration options and a learning curve
|
|
16
|
+
- **Pweave** has not been maintained for many years and is incompatible with currently supported Python versions.
|
|
17
|
+
- **Traditional scripts** lack integrated documentation and visualization
|
|
18
|
+
|
|
19
|
+
Nhandu offers a different solution:
|
|
20
|
+
|
|
21
|
+
- Write literate programs in **normal `.py` files**: no special format, just comments
|
|
22
|
+
- **Perfect git diffs**: plain text, not JSON, no timestamps, no hashes
|
|
23
|
+
- **No server or browser** required—just run the command
|
|
24
|
+
- **Zero configuration needed**: smart defaults get you started immediately
|
|
25
|
+
- **Python-native**: designed specifically for the Python ecosystem
|
|
26
|
+
|
|
27
|
+
Nhandu also supports traditional `.md` files with code blocks if you prefer that style.
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
### Your First Literate Program
|
|
32
|
+
|
|
33
|
+
Create a file `analysis.py`:
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
#' # My First Analysis
|
|
37
|
+
#'
|
|
38
|
+
#' This is a literate Python program. Lines starting with `#'` are markdown.
|
|
39
|
+
#' Everything else is regular Python code.
|
|
40
|
+
|
|
41
|
+
import numpy as np
|
|
42
|
+
|
|
43
|
+
# Generate some data
|
|
44
|
+
data = np.random.normal(0, 1, 1000)
|
|
45
|
+
|
|
46
|
+
#' ## Results
|
|
47
|
+
#'
|
|
48
|
+
#' Let's compute some statistics:
|
|
49
|
+
|
|
50
|
+
print(f"Mean: {data.mean():.3f}")
|
|
51
|
+
print(f"Std Dev: {data.std():.3f}")
|
|
52
|
+
|
|
53
|
+
#' The data looks normally distributed, as expected.
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Generate Your Report
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
nhandu analysis.py
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
This creates `analysis.html` with your code, output, and nicely formatted documentation, from a plain Python script.
|
|
63
|
+
|
|
64
|
+
## Features
|
|
65
|
+
|
|
66
|
+
### Smart Output Capture
|
|
67
|
+
|
|
68
|
+
Nhandu automatically captures:
|
|
69
|
+
|
|
70
|
+
- **Print statements** and stdout
|
|
71
|
+
- **Matplotlib plots** (no `plt.show()` needed!)
|
|
72
|
+
- **Expression results** (like Jupyter cells)
|
|
73
|
+
- **Rich objects** (DataFrames render as tables in HTML)
|
|
74
|
+
|
|
75
|
+
### Syntax Highlighting
|
|
76
|
+
|
|
77
|
+
Server-side syntax highlighting with 50+ themes via Pygments. Popular themes include: `github-dark` (default), `monokai`, `dracula`, `one-dark`, `vs`, and `solarized-light`.
|
|
78
|
+
|
|
79
|
+
### Multiple Output Formats
|
|
80
|
+
|
|
81
|
+
Markdown output can be converted to PDF, Word, LaTeX, and more using [pandoc](https://pandoc.org/) or similar tools. Native HTML support is implemented out-of-the-box.
|
|
82
|
+
|
|
83
|
+
### Configuration (Optional)
|
|
84
|
+
|
|
85
|
+
Power users can customize their reports via YAML frontmatter:
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
#' ---
|
|
89
|
+
#' title: My Scientific Report
|
|
90
|
+
#' output: html
|
|
91
|
+
#' code_theme: dracula
|
|
92
|
+
#' plot_dpi: 150
|
|
93
|
+
#' ---
|
|
94
|
+
#'
|
|
95
|
+
#' # Introduction
|
|
96
|
+
#' ...
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
It is also possible to use a configuration file (`nhandu.yaml`) or CLI arguments.
|
|
100
|
+
|
|
101
|
+
## How It Works
|
|
102
|
+
|
|
103
|
+
### Literate Python Format (`.py` files)
|
|
104
|
+
|
|
105
|
+
Nhandu treats Python files specially when they contain markdown comments:
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
#' # This is a markdown heading
|
|
109
|
+
#'
|
|
110
|
+
#' Any line starting with #' is treated as **markdown**.
|
|
111
|
+
#' You can use all standard markdown features.
|
|
112
|
+
|
|
113
|
+
# This is a regular Python comment
|
|
114
|
+
x = 42 # Regular code continues to work normally
|
|
115
|
+
|
|
116
|
+
#' Back to documentation. Variables persist across blocks:
|
|
117
|
+
|
|
118
|
+
print(f"x = {x}")
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
**Hidden code blocks** let you run setup code without cluttering your report:
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
#| hide
|
|
125
|
+
import pandas as pd
|
|
126
|
+
import matplotlib.pyplot as plt
|
|
127
|
+
# Configuration code here—runs but doesn't appear in output
|
|
128
|
+
#|
|
|
129
|
+
|
|
130
|
+
#' Now let's analyze our data:
|
|
131
|
+
# This code WILL appear in the output
|
|
132
|
+
data = pd.read_csv("data.csv")
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Traditional Markdown Format (`.md` files)
|
|
136
|
+
|
|
137
|
+
You can also use standard markdown files with code blocks:
|
|
138
|
+
|
|
139
|
+
````markdown
|
|
140
|
+
# My Analysis
|
|
141
|
+
|
|
142
|
+
Here's some Python code:
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
import numpy as np
|
|
146
|
+
x = np.linspace(0, 10, 100)
|
|
147
|
+
print(f"Generated {len(x)} points")
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
The output will appear in your rendered document. In case of HTML output, any figures are embedded in the file, so that you have a single file to distribute.
|
|
151
|
+
|
|
152
|
+
````
|
|
153
|
+
|
|
154
|
+
### Execution Model
|
|
155
|
+
|
|
156
|
+
- **Shared namespace**: All code blocks share the same Python environment
|
|
157
|
+
- **Sequential execution**: Blocks run in document order
|
|
158
|
+
- **Output capture**: stdout, plots, and expression results are all captured
|
|
159
|
+
- **Rich formatting**: Automatic handling of matplotlib figures, pandas DataFrames, and more
|
|
160
|
+
|
|
161
|
+
## Examples
|
|
162
|
+
|
|
163
|
+
Check out the [`examples/`](https://github.com/tresoldi/nhandu/tree/main/examples/) directory for complete demonstrations:
|
|
164
|
+
|
|
165
|
+
- **[01_hello_world.py](https://github.com/tresoldi/nhandu/tree/main/examples/01_hello_world.py)** - Basic syntax and concepts [[OUTPUT](https://htmlpreview.github.io/?https://github.com/tresoldi/nhandu/blob/main/examples/01_hello_world.py.html)]
|
|
166
|
+
- **[02_data_analysis.py](https://github.com/tresoldi/nhandu/tree/main/examples/02_data_analysis.py)** - Working with data using standard library [[OUTPUT](https://htmlpreview.github.io/?https://github.com/tresoldi/nhandu/blob/main/examples/02_data_analysis.py.html)]
|
|
167
|
+
- **[03_plotting.py](https://github.com/tresoldi/nhandu/tree/main/examples/03_plotting.py)** - Creating visualizations with matplotlib [[OUTPUT](https://htmlpreview.github.io/?https://github.com/tresoldi/nhandu/blob/main/examples/03_plotting.py.html)]
|
|
168
|
+
- **[04_scientific_computation.py](https://github.com/tresoldi/nhandu/tree/main/examples/04_scientific_computation.py)** - Numerical computing with NumPy [[OUTPUT](https://htmlpreview.github.io/?https://github.com/tresoldi/nhandu/blob/main/examples/04_scientific_computation.py.html)]
|
|
169
|
+
- **[05_advanced_report.py](https://github.com/tresoldi/nhandu/tree/main/examples/05_advanced_report.py)** - Complex report with pandas and multiple visualizations [[OUTPUT](https://htmlpreview.github.io/?https://github.com/tresoldi/nhandu/blob/main/examples/05_advanced_report.py.html)]
|
|
170
|
+
|
|
171
|
+
## Installation & Usage
|
|
172
|
+
|
|
173
|
+
### Install from PyPI
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
pip install nhandu
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Install from Source
|
|
180
|
+
|
|
181
|
+
```bash
|
|
182
|
+
git clone https://github.com/tresoldi/nhandu.git
|
|
183
|
+
cd nhandu
|
|
184
|
+
pip install -e .
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Basic Usage
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
nhandu document.py # Process literate Python file → document.html
|
|
191
|
+
nhandu document.md # Process markdown file → document.html
|
|
192
|
+
nhandu document.py -o report.html # Specify output file
|
|
193
|
+
nhandu document.py --format md # Output as markdown
|
|
194
|
+
nhandu document.py --code-theme monokai # Custom syntax theme
|
|
195
|
+
nhandu document.py --verbose # Show processing details
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### CLI Options
|
|
199
|
+
|
|
200
|
+
```
|
|
201
|
+
nhandu [OPTIONS] INPUT
|
|
202
|
+
|
|
203
|
+
Options:
|
|
204
|
+
-o, --output PATH Output file path
|
|
205
|
+
--format {html,md} Output format (default: html)
|
|
206
|
+
--config PATH Configuration file (YAML)
|
|
207
|
+
--working-dir PATH Working directory for code execution
|
|
208
|
+
--timeout SECONDS Execution timeout
|
|
209
|
+
--code-theme THEME Syntax highlighting theme
|
|
210
|
+
--verbose, -v Enable verbose output
|
|
211
|
+
--version Show version
|
|
212
|
+
--help Show help message
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### Roadmap
|
|
216
|
+
|
|
217
|
+
Current priorities:
|
|
218
|
+
|
|
219
|
+
- [ ] Native PDF output support
|
|
220
|
+
- [ ] Inline code evaluation (`<%= expression %>` syntax)
|
|
221
|
+
- [ ] Custom HTML templates (Jinja2)
|
|
222
|
+
- [ ] Watch mode for live development
|
|
223
|
+
- [ ] Rich display for more object types (NumPy arrays, scikit-learn models)
|
|
224
|
+
- [ ] Caching system for faster re-renders
|
|
225
|
+
|
|
226
|
+
See [issues](https://github.com/tresoldi/nhandu/issues) for more details and to suggest features.
|
|
227
|
+
|
|
228
|
+
## Project Information
|
|
229
|
+
|
|
230
|
+
### Citation and Acknowledgements
|
|
231
|
+
|
|
232
|
+
If you use Nhandu in your research, please cite:
|
|
233
|
+
|
|
234
|
+
```bibtex
|
|
235
|
+
@software{tresoldi2025nhandu,
|
|
236
|
+
author = {Tresoldi, Tiago},
|
|
237
|
+
title = {Nhandu: Literate Programming for Python},
|
|
238
|
+
year = {2025},
|
|
239
|
+
publisher = {Department of Linguistics and Philology, Uppsala University},
|
|
240
|
+
address = {Uppsala, Sweden},
|
|
241
|
+
url = {https://github.com/tresoldi/nhandu},
|
|
242
|
+
orcid = {0000-0002-2863-1467}
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
The earliest stages of development took place within the context of
|
|
247
|
+
the [Cultural Evolution of Texts](https://github.com/evotext/) project, with funding from the
|
|
248
|
+
[Riksbankens Jubileumsfond](https://www.rj.se/) (grant agreement ID:
|
|
249
|
+
[MXM19-1087:1](https://www.rj.se/en/anslag/2019/cultural-evolution-of-texts/)).
|
|
250
|
+
|
|
251
|
+
### License
|
|
252
|
+
|
|
253
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
254
|
+
|
|
255
|
+
### Acknowledgments
|
|
256
|
+
|
|
257
|
+
Nhandu is inspired by:
|
|
258
|
+
- Donald Knuth's original [literate programming](https://en.wikipedia.org/wiki/Literate_programming) vision
|
|
259
|
+
- [knitr](https://yihui.org/knitr/) and R Markdown's approach to reproducible research
|
|
260
|
+
- [Jupyter](https://jupyter.org/)'s interactive computing paradigm
|
|
261
|
+
- [Quarto](https://quarto.org/)'s modern scientific publishing tools
|
|
262
|
+
- [Pweave](http://mpastell.com/pweave/)'s Python implementation (though no longer maintained)
|
|
263
|
+
|
|
264
|
+
Special thanks to the scientific Python community for building the ecosystem that makes tools like this possible.
|