Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[collapsible_headings] new features! #892

Merged
merged 19 commits into from
Feb 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
4150d19
[collapsible_headings] remove unnecessary variables toggle_..._class
jcb91 Jan 21, 2017
d394b53
[collapsible_headings] remove unused variable bracket_clicks
jcb91 Jan 21, 2017
4b453d7
[collapsible_headings] simplify config loading
jcb91 Jan 21, 2017
684405d
[collapsible_headings] remove outdated warning
jcb91 Jan 21, 2017
ff37bd3
[collapsible_headings] remove unused function
jcb91 Jan 21, 2017
cc24c96
[collapsible_headings] ensure event handlers get unbound during noteb…
jcb91 Jan 22, 2017
459ffe5
[collapsible_headings] promisify config loading, expose setting options
jcb91 Jan 22, 2017
26c1066
[collapsible_headings] get dependencies for patches through promises
jcb91 Jan 22, 2017
5ee1677
[collapsible_headings] fixes for non-live notebooks
jcb91 Jan 22, 2017
ca8dc8c
[collapsible_headings] improve function comments
jcb91 Jan 22, 2017
ed6ebcb
[collapsible_headings] use env in place of Jupyter in all action hand…
jcb91 Jan 22, 2017
f504169
[collapsible_headings] whitespace
jcb91 Jan 22, 2017
b1c537a
[collapsible_headings] remove outdated file-level comment
jcb91 Jan 26, 2017
eca104d
[collapsible_headings] allow left keyboard shortcut to skip to higher…
jcb91 Jan 27, 2017
0845bab
[collapsible_headings] heading cell status needs updating before others
jcb91 Feb 11, 2017
996a7c3
[collapsible_headings] add nbconvert support by inlining css & js
jcb91 Feb 12, 2017
57f1ccc
[collapsible_headings] fix tests for nbconvert exporting
jcb91 Feb 18, 2017
60e15d7
[collapsible_headings] update exporting-related docs
jcb91 Feb 18, 2017
f4430a3
[collapsible_headings] update conda recipe with correct imports
jcb91 Feb 18, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion conda.recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ test:
- jupyter_contrib_nbextensions.install
- jupyter_contrib_nbextensions.migrate
- jupyter_contrib_nbextensions.nbconvert_support
- jupyter_contrib_nbextensions.nbconvert_support.collapsible_headings
- jupyter_contrib_nbextensions.nbconvert_support.embedhtml
- jupyter_contrib_nbextensions.nbconvert_support.pp_highlighter
- jupyter_contrib_nbextensions.nbconvert_support.pre_codefolding
- jupyter_contrib_nbextensions.nbconvert_support.pre_collapsible_headings
- jupyter_contrib_nbextensions.nbconvert_support.pre_pymarkdown
- jupyter_contrib_nbextensions.nbconvert_support.pre_svg2pdf
- jupyter_contrib_nbextensions.nbconvert_support.toc2
Expand Down
32 changes: 15 additions & 17 deletions docs/source/exporting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ Retaining Codefolding
.. autoclass:: CodeFoldingPreprocessor


Collapsible Headings
^^^^^^^^^^^^^^^^^^^^

.. autoclass:: CollapsibleHeadingsPreprocessor


Retaining Highlighting
^^^^^^^^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -90,6 +84,20 @@ Export Table of Contents
.. autoclass:: TocExporter


Inlining css & javascript
^^^^^^^^^^^^^^^^^^^^^^^^^

.. autoclass:: ExporterInliner

Forms the basis for :class:`ExporterCollapsibleHeadings`.


Collapsible Headings
^^^^^^^^^^^^^^^^^^^^

.. autoclass:: ExporterCollapsibleHeadings


Templates
---------

Expand Down Expand Up @@ -122,16 +130,6 @@ The supported cell metadata tags are:

Detailed description:

This will hide any cell marked as `hidden` (used for collapsible headings extension):
.. code-block::

{% block any_cell scoped %}
{%- if cell.metadata.hidden -%}
{%- else -%}
{{ super() }}
{%- endif -%}
{% endblock any_cell %}

This will hide the input of either an individual code cell or all code cells of the notebook:
.. code-block::

Expand Down Expand Up @@ -166,7 +164,7 @@ This will suppress the prompt string if the input of a code cell is hidden:
nbextensions.tplx
^^^^^^^^^^^^^^^^^

This template implements the features to hide cells used by extensions like collapsible_headings, hide_input, etc.
This template implements the features to hide cells used by extensions like hide_input, etc.
It won't produce a valid LaTeX file alone, use it to extend your own template (see printviewlatex.tplx).

printviewlatex.tplx
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def main():
'html_toc = jupyter_contrib_nbextensions.nbconvert_support.toc2:TocExporter', # noqa: E501
'selectLanguage = jupyter_contrib_nbextensions.nbconvert_support.nbTranslate:NotebookLangExporter', # noqa: E501
'html_embed = jupyter_contrib_nbextensions.nbconvert_support.embedhtml:EmbedHTMLExporter', # noqa: E501
'html_ch = jupyter_contrib_nbextensions.nbconvert_support.collapsible_headings:ExporterCollapsibleHeadings', # noqa: E501
],
},
scripts=[os.path.join('scripts', p) for p in [
Expand Down
12 changes: 7 additions & 5 deletions src/jupyter_contrib_nbextensions/nbconvert_support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,28 @@

import os

from .collapsible_headings import ExporterCollapsibleHeadings
from .embedhtml import EmbedHTMLExporter
from .exporter_inliner import ExporterInliner
from .nbTranslate import NotebookLangExporter
from .pp_highlighter import HighlighterPostProcessor, HighlighterPreprocessor
from .pre_codefolding import CodeFoldingPreprocessor
from .pre_collapsible_headings import CollapsibleHeadingsPreprocessor
from .pre_pymarkdown import PyMarkdownPreprocessor
from .pre_svg2pdf import SVG2PDFPreprocessor
from .toc2 import TocExporter
from .nbTranslate import NotebookLangExporter

__all__ = [
'templates_directory',
'CodeFoldingPreprocessor',
'CollapsibleHeadingsPreprocessor',
'EmbedHTMLExporter',
'ExporterCollapsibleHeadings',
'ExporterInliner',
'HighlighterPostProcessor',
'HighlighterPreprocessor',
'NotebookLangExporter',
'PyMarkdownPreprocessor',
'SVG2PDFPreprocessor',
'templates_directory',
'TocExporter',
'NotebookLangExporter'
]


Expand Down
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))
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

This file was deleted.

Loading