Index of /besson/publis/fix-iocaml-notebook-exports-to-pdf.git

[ICO]NameLast modifiedSizeDescription
[PARENTDIR]Parent Directory  - Root of the website (in English or in French)
[DIR]images/2017-09-07 16:19 -  
[TXT]LICENSE.txt2017-09-07 16:18 1.1K 
[   ]Makefile2017-09-08 09:52 1.3K 
[TXT]README.md2017-09-07 17:57 3.6KSome details about this page
[   ]_config.yml2017-09-07 18:06 25  
[TXT]fix-iocaml-notebook-exports-to-pdf.py2017-09-08 09:50 3.7K 
[TXT]test_input_noocaml.html2017-09-07 17:53 314Ktest_input_noocaml
[   ]test_input_noocaml.ipynb2017-09-08 09:50 36K 
[   ]test_input_noocaml.pdf2017-09-07 17:54 61K 
[TXT]test_input_noocaml.py2017-09-07 17:55 15K 
[TXT]test_input_ocaml.html2017-09-07 17:53 317Ktest_input_ocaml
[   ]test_input_ocaml.ipynb2017-09-07 17:22 44K 
[TXT]test_input_ocaml.ml2017-09-07 17:54 12K 
[   ]test_input_ocaml.pdf2017-09-07 17:54 107K 
# How to convert to PDF IOCaml notebooks

## Explanation :sunglasses:
- I use the [unofficial IOCaml kernel]( for my teaching activities, to use the awesome [Jupyter notebooks]( with the [OCaml language](,
- I like to write solutions to my practical sessions in a notebook, so it can be nicely viewed online.

## Issue :boom: !
- But by default the PDF generation was not perfect: the `stdout` output was kept fine, but the type annotations were removed from the output of the PDF. See for instance, [this HTML notebook]( and [this PDF notebook]( to see what was missing.
- And without the type annotations, the solution is not complete and not understandable.

## Solution :rocket: !
- I wrote [this small Python script]( to pre-process a `.ipynb` notebook file *before* calling `jupyter-nbconvert --to pdf`, and keep the type annotations.
- Now, instead of using this Bash alias to do `j2pdf My_OCaml_notebook.ipynb`:

alias j2pdf='jupyter-nbconvert --to pdf'
- [I use this Bash function the same way](
function j2pdf() {
  for old in "$@"; do
    [ -f "$new" ] && mv -vf "$new" /tmp/ "$old" "$new"
    if [ $? = 0 ]; then
      jupyter-nbconvert --to pdf "$new"
      [ -f "${old%.ipynb}.pdf" ] && mv -vf "${old%.ipynb}.pdf" /tmp/
      mv -vf "${new%.ipynb}.pdf" "${old%.ipynb}.pdf"
      mv -vf "$new" /tmp/
      jupyter-nbconvert --to pdf "$old"

- It tries to fix the notebook, and convert either the new one to PDF or the old one.

> Note: [I also use this Bash function](
>  ```bash
>  function j2html() {
>    for old in "$@"; do
>      jupyter-nbconvert --to html "$old"
>    done
>  }
>  ```


## Test
With [this Makefile](Makefile), run the tests with:
make -B test

- It runs the script on two example notebooks, one using the IOCaml kernel, one using another kernel (Python 3),
- It converts them to PDF and to HTML, and open the results,
- You should check manually that the PDF of the [`test_input_ocaml.ipynb`](test_input_ocaml.ipynb) notebook has both `stdout` output and type annotations.


## :scroll: License ? [![GitHub license](](
[MIT Licensed]( (file [LICENSE](LICENSE)).
© [Lilian Besson](, 2017.

[![Ask Me Anything !](](

[![ForTheBadge built-with-swag](](

[![ForTheBadge uses-badges](](
[![ForTheBadge uses-git](](