forked from kirbs-/hide_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhide_code_pdf_exporter.py
More file actions
52 lines (40 loc) · 1.79 KB
/
Copy pathhide_code_pdf_exporter.py
File metadata and controls
52 lines (40 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import os.path
import pdfkit
# from hide_code.hide_code_html_exporter import HideCodeHTMLExporter
from traitlets.config import Config
from traitlets import default
# from nbconvert.exporters.pdf import PDFExporter
from nbconvert.exporters.html import HTMLExporter
from traitlets.log import get_logger
class HideCodePDFExporter(HTMLExporter):
def __init__(self, config=None, **kw):
# self.register_preprocessor('hide_code.HideCodePreprocessor', True)
super(HideCodePDFExporter, self).__init__(config, **kw)
# self.preprocessors = ['hide_code.HideCodePreprocessor']
# self._init_preprocessors()
export_from_notebook = 'Hide Code PDF (HTML)'
@default('file_extension')
def _file_extension_default(self):
return '.pdf'
def from_notebook_node(self, nb, resources=None, **kw):
output, resources = super(HideCodePDFExporter, self).from_notebook_node(nb, resources, **kw)
output = pdfkit.from_string(output, False)
return output, resources
def _template_file_default(self):
return 'hide_code_full.tpl'
@property
def template_path(self):
"""
We want to inherit from HTML template, and have template under
`./templates/` so append it to the search path. (see next section)
"""
return super(HideCodePDFExporter, self).template_path + [os.path.join(os.path.dirname(__file__), "Templates")]
@property
def template_paths(self):
"""
We want to inherit from HTML template, and have template under
``./templates/`` so append it to the search path. (see next section)
Note: nbconvert 6.0 changed ``template_path`` to ``template_paths``
"""
return super()._template_paths() + [os.path.join(os.path.dirname(__file__), "Templates")]