Skip to content

Commit

Permalink
Update to pyproject.toml and package for nbconvert 7
Browse files Browse the repository at this point in the history
  • Loading branch information
t-makaro committed Jan 10, 2025
1 parent 7026947 commit 3ef68ce
Show file tree
Hide file tree
Showing 17 changed files with 190 additions and 215 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ celerybeat-schedule
.env

# virtualenv
.venv/
venv/
ENV/

Expand All @@ -95,4 +96,7 @@ example/_minted-test
*.png
*.out
*.aux
*.synctex.gz
*.synctex.gz

# image files
*.afphoto
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
recursive-include nb_pdf_template *.tplx
recursive-include share/templates/latex_authentic/ *
66 changes: 35 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,68 +1,72 @@
# classic.tplx
A more accurate representation of jupyter notebooks when converting to pdfs.
# An Authentic LaTeX Conversion Template for Jupyter Notebooks.
A more accurate representation of jupyter notebooks when converting to pdf.
This template was designed to make converted jupyter notebooks look (almost) identical to the actual notebook. If something doesn't exist in the original notebook then it doesn't belong in the conversion.

## Update for 7.0

Work is in progress for updating this for nbconvert 7.0. Minimal changes were needed to make the template itself work, so it should work with nbconvert 6.0 as well, but officially nbconvert 5 and 6 won't be supported. Once I have figured out the new template install process, I will push a release.
This repository also acts as a reference for how to package nbconvert templates to be pip installable without using exporters and entry points.

## Improvements
1. \maketitle is removed (If you want a title then add a markdown cell to the top of your notebook).
2. Sections are no longer numbered automatically (notebooks don't number sections so the pdf shouldn't).
8. Markdown paragraphs are no longer auto-indented in the pdf.
9. Syntax highlighting improvements. (Bonus if using XeLaTeX)
3. ~~**BOXES!** are drawn around code cells.~~ **(This change was merged into nbconvert 5.5.0)**
4. ~~In/Out counts will move to the left as the execution count increases instead of pushing code to the right (only numbers are displayed by default to save page width).~~ **(This change was merged into nbconvert 5.5.0)**
5. ~~$\LaTeX$ and $\Tex$ in markdown cells will no longer cause conversion to fail.~~ **(This change was merged into nbconvert 5.4.0)**
6. "\LaTeX" and "\TeX" are no longer converted into a logo on conversion to pdf unless they are in math mode. (This and the above point replicate the functionality of these commands in notebook markdown).
7. ~~In/Out prompt colours updated to match Jupyter.~~ **(This change was merged into nbconvert 5.5.0)**
2. Sections are no longer numbered automatically.
3. Syntax highlighting improvements. (Bonus if using XeLaTeX)
4. "\LaTeX" and "\TeX" are no longer converted into a logo on conversion to pdf unless they are in math mode. (replicating the functionality of these commands in notebook markdown).
3. ~~Markdown paragraphs are no longer auto-indented in the pdf.~~ **(This change was merged into nbconvert 5.5.0)**
6. ~~**BOXES!** are drawn around code cells.~~ **(This change was merged into nbconvert 5.5.0)**
7. ~~In/Out counts will move to the left as the execution count increases instead of pushing code to the right (only numbers are displayed by default to save page width).~~ **(This change was merged into nbconvert 5.5.0)**
8. ~~$\LaTeX$ and $\Tex$ in markdown cells will no longer cause conversion to fail.~~ **(This change was merged into nbconvert 5.4.0)**
9. ~~In/Out prompt colours updated to match Jupyter.~~ **(This change was merged into nbconvert 5.5.0)**
10. ~~Output text wrapping improvements.~~ **(This change was merged into nbconvert 5.5.0)**
11. ~~Code cell text wrapping.~~ **(This change was merged into nbconvert 5.5.0)**

Quick Comparison:
![comparison](example/comparison.png)
for a closer look see the example directory.

## Pygments

This repository also contains a pygments style called jupyter_python that can be used programmatically and was used in previous versions of the template. Syntax highlighting improvements are done without the style though as it was insufficient to achieve the highlighting quality I wanted, and it needed a shell escape.

## Installation
If you have multiple python interpreters installed, but sure to install on the one with nbconvert. Otherwise, the templates will be put into the wrong data folder.

```
```bash
pip install nb_pdf_template
python -m nb_pdf_template.install
```

### Updating
```
```bash
pip install -U nb_pdf_template
python -m nb_pdf_template.install
```

### Manual Install:
Drop all of the "*.tplx" files into the folder containing the other LaTeX nbconvert templates. If using anaconda, it should be something like:
> */Anaconda3/Lib/site-packages/nbconvert/templates/latex
Drop the "latex_authentic" folder into the folder containing the other LaTeX nbconvert templates. If using anaconda, it should be something like:
> */Anaconda3/share/jupyter/nbconvert/templates
### Uninstalling
```bash
pip uninstall nb_pdf_template
```
or if manually installed then remove the files manually.

## Use
From the command line:
```bash
jupyter nbconvert --to pdf filename.ipynb --template classic
jupyter nbconvert --to pdf filename.ipynb --template latex_authentic
```

You can add ```--template-file m.tex.j2``` to change the index file of the template to the one with the prompts above the cells instead of to the left of the cells.

Adding:
```python
c.LatexExporter.template_file = 'classic'
c.LatexExporter.template_file = 'latex_authentic'
```
to the ```jupyter_nbconvert_config.py``` file will let you drop the "--template classic", and to the ```jupyter_notebook_config.py``` file will let you use "download as pdf" from within the Jupyter notebook.
to the ```jupyter_nbconvert_config.py``` file will let you drop the "--template latex_authentic", and to the ```jupyter_notebook_config.py``` file will let you use "download as pdf" from within the Jupyter notebook/lab with the template.

Replace ```classic``` with your template of choice.
## Build

### Templates
This package offers the following templates:

Template | Use
---------|-------
classic.tplx **(Recommended)**| For most accurate recreation of the default Jupyter Notebook style.
classicm.tplx | m for modified. Similar to classic.tplx, but in/out prompts are above cells instead of in the margin. Bonus left margins are smaller so code cells are wider.
To build the package use
```bash
hatch build
```

## Tips (Good for any template)
[Moved to the wiki](https://github.com/t-makaro/nb_pdf_template/wiki/Tips)
Binary file modified example/comparison.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 0 additions & 12 deletions example/create_tests.ps1

This file was deleted.

Binary file removed example/test_classic_template.pdf
Binary file not shown.
Binary file removed example/test_classicm_template.pdf
Binary file not shown.
Binary file removed example/test_default_template.pdf
Binary file not shown.
Binary file added example/test_latex.pdf
Binary file not shown.
Binary file added example/test_latex_authentic.pdf
Binary file not shown.
Binary file added example/test_latex_authentic_m.pdf
Binary file not shown.
37 changes: 0 additions & 37 deletions nb_pdf_template/install.py

This file was deleted.

34 changes: 34 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "nb_pdf_template"
version = "5.0.0"
description = "LaTeX templates for Jupyter notebook conversion to PDF"
readme = "README.md"
requires-python = ">=3.6"
license = { text = "MIT" }
authors = [
{ name = "Tyler Makaro", email = "" }
]
urls = { "Homepage" = "https://github.com/t-makaro/nb_pdf_template" }
classifiers = [
"License :: OSI Approved :: MIT License",
"Development Status :: 5 - Production/Stable",
"Topic :: Text Processing :: Markup :: LaTeX",
"Programming Language :: Python :: 3",
]

[tool.hatch.build.targets.wheel]
include = [
"nb_pdf_template/**",
"README.md",
"LICENSE",
]

[project.entry-points."pygments.styles"]
jupyter_python = "nb_pdf_template:Jupyter_PythonStyle"

[tool.hatch.build.targets.wheel.shared-data]
"share/templates" = "share/jupyter/nbconvert/templates"
25 changes: 0 additions & 25 deletions setup.py

This file was deleted.

9 changes: 9 additions & 0 deletions share/templates/latex_authentic/conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"base_template": "latex",
"mimetypes": {
"text/latex": true,
"text/tex": true,
"application/pdf": true
}
}

Loading

0 comments on commit 3ef68ce

Please sign in to comment.