/
notebook.py
115 lines (86 loc) · 3.49 KB
/
notebook.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding: utf-8 -*-
"""
Notebook plugin.
This plugin lets you easily include html-rendered IPython notebooks as part
of your Hyde site. This plugin requires IPython version 1.x
To use, simply add the following to your site.yaml file:
plugins:
- hyde.ext.plugins.notebook.NotebookPlugin
Then where there are notebooks in your site tree, an HTML version will be
deployed. If this is the contents of your site:
site.yaml
contents/
index.html <-- normal files ignored
notebook.ipynb <-- ipynb extensions converted
Then the deploy directory will look like this:
deploy/
index.html <-- files generated by hyde
notebook.html <-- converted by IPython.nbconvert
notebook.ipynb <-- raw notebook
"""
# TODO: - allow inserting notebooks into templates
import os
import shutil
from hyde.plugin import Plugin
from hyde.model import Expando
from commando.util import getLoggerWithNullHandler
from fswrap import File, Folder
logger = getLoggerWithNullHandler('hyde.ext.plugins.notebook')
try:
import IPython
if IPython.__version__[0] != '1':
raise ImportError("IPython version 1.x required")
except ImportError:
logger.error("The notebook plugin requires IPython version 1.x")
logger.error("`pip install -U ipython` to get it.")
raise
from IPython.nbformat import current as nbformat
from IPython.nbconvert.exporters import HTMLExporter
class NotebookPlugin(Plugin):
"""The plugin class for converting IPython notebooks to HTML"""
suffix = "ipynb"
def __init__(self, site):
self._notebook_config = None
super(NotebookPlugin, self).__init__(site)
@property
def plugin_name(self):
"""The name of the plugin"""
return "notebook"
@property
def settings(self):
"""Settings for this plugin"""
settings = Expando({})
try:
user_settings = getattr(self.site.config, self.plugin_name)
except AttributeError:
pass
else:
for name in dir(user_settings):
if not name.startswith("_"):
setattr(settings, name, getattr(user_settings, name))
return settings
def begin_site(self):
"""Event hook for when site processing begins"""
settings = self.settings
for resource in self.site.content.walk_resources():
if resource.source_file.path.endswith(self.suffix):
new_name = (resource.source_file.name_without_extension
+ ".html")
target_folder = File(resource.relative_deploy_path).parent
resource.relative_deploy_path = target_folder.child(new_name)
def begin_text_resource(self, resource, text):
"""Event hook for processing an individual resource."""
if not resource.source_file.path.endswith(self.suffix):
return text
# Copy the raw notebook file to the deploy directory
raw_file_dest = os.path.join(
resource.site.config.deploy_root,
File(resource.relative_deploy_path).parent.child(
resource.source_file.name))
shutil.copy(resource.source_file.path, raw_file_dest)
# use nbconvert to convert to HTML
exporter = HTMLExporter(template_file='full')
nb_json = nbformat.reads_json(text)
(body, resources) = exporter.from_notebook_node(nb_json)
# return the converted text
return body