-
Notifications
You must be signed in to change notification settings - Fork 809
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #892 from jcb91/ch
[collapsible_headings] new features!
- Loading branch information
Showing
14 changed files
with
687 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/jupyter_contrib_nbextensions/nbconvert_support/collapsible_headings.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"""Nbconvert exporter to inline css & js for collapsible_headings.""" | ||
|
||
import json | ||
import os | ||
|
||
from notebook.services.config import ConfigManager | ||
|
||
from jupyter_contrib_nbextensions import __file__ as contrib_init | ||
from .exporter_inliner import ExporterInliner | ||
|
||
|
||
class ExporterCollapsibleHeadings(ExporterInliner): | ||
""" | ||
HTMLExporter which inlines the collapsible_headings nbextension. | ||
Export collapsible_headings nbextension functionality to html | ||
by inlining relevant css and js content. | ||
Example usage:: | ||
jupyter nbconvert --to html_ch FILE.ipynb | ||
""" | ||
|
||
def _template_file_default(self): | ||
return 'collapsible_headings' | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(ExporterCollapsibleHeadings, self).__init__(*args, **kwargs) | ||
|
||
ch_dir = os.path.join( | ||
os.path.dirname(contrib_init), 'nbextensions', | ||
'collapsible_headings') | ||
|
||
with open(os.path.join(ch_dir, 'main.css'), 'r') as f: | ||
main_css = f.read() | ||
self.inliner_resources['css'].append(main_css) | ||
|
||
self.inliner_resources['css'].append(""" | ||
/* no local copies of fontawesome fonts from basic templates, so get them from cdn */ | ||
@font-face { | ||
font-family: 'FontAwesome'; | ||
src: url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.eot'); | ||
src: url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.eot?#iefix') format('embedded-opentype'), | ||
url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.woff') format('woff'), | ||
url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.ttf') format('truetype'), | ||
url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/fonts/fontawesome-webfont.svg#fontawesomeregular') format('svg'); | ||
font-weight: normal; | ||
font-style: normal; | ||
} | ||
""") # noqa: E501 | ||
|
||
with open(os.path.join(ch_dir, 'main.js'), 'r') as f: | ||
self.inliner_resources['js'].append( | ||
f.read().replace( | ||
"define([", | ||
"define('nbextensions/collapsible_headings/main', [") | ||
) | ||
|
||
cm = ConfigManager() | ||
collapsible_headings_options = cm.get('notebook').get( | ||
'collapsible_headings', {}) | ||
self.inliner_resources['js'].append(""" | ||
require([ | ||
'jquery', | ||
'nbextensions/collapsible_headings/main' | ||
], function ( | ||
$, | ||
nbext | ||
) { | ||
nbext.set_collapsible_headings_options(%s); | ||
$(document).ready(function () { | ||
nbext.refresh_all_headings(); | ||
}); | ||
}); | ||
""" % json.dumps(collapsible_headings_options)) |
46 changes: 46 additions & 0 deletions
46
src/jupyter_contrib_nbextensions/nbconvert_support/exporter_inliner.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Nbconvert exporter to inline css & js for collapsible_headings.""" | ||
|
||
from __future__ import print_function | ||
|
||
from nbconvert.exporters.html import HTMLExporter | ||
from traitlets import Dict | ||
|
||
|
||
class ExporterInliner(HTMLExporter): | ||
|
||
inliner_resources = Dict( | ||
{'css': [], 'js': []}, config=True, | ||
help='css and js scripts to wrap in html <style> or <script> tags') | ||
|
||
def _template_file_default(self): | ||
return 'inliner' | ||
|
||
def from_notebook_node(self, nb, resources=None, **kw): | ||
|
||
# ensure resources used by template actually exist, add in any from | ||
# config | ||
if resources is None: | ||
resources = {} | ||
inliner_resources = resources.setdefault('inliner', {}) | ||
for tt in ('css', 'js'): | ||
existing_items = inliner_resources.setdefault(tt, []) | ||
existing_items += [ | ||
item for item in self.inliner_resources[tt] | ||
if item not in existing_items] | ||
|
||
return super(ExporterInliner, self).from_notebook_node( | ||
nb, resources, **kw) | ||
|
||
@property | ||
def default_config(self): | ||
c = super(ExporterInliner, self).default_config | ||
# import here to avoid circular import | ||
from jupyter_contrib_nbextensions.nbconvert_support import ( | ||
templates_directory) | ||
contrib_templates_dir = templates_directory() | ||
|
||
template_path = c.TemplateExporter.setdefault('template_path', []) | ||
if templates_directory not in template_path: | ||
template_path.append(contrib_templates_dir) | ||
|
||
return c |
58 changes: 0 additions & 58 deletions
58
src/jupyter_contrib_nbextensions/nbconvert_support/pre_collapsible_headings.py
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.