Skip to content

Commit

Permalink
Merge pull request #83 from kirbs-/dev
Browse files Browse the repository at this point in the history
v0.5.5
  • Loading branch information
kirbs- authored Feb 17, 2020
2 parents 5c732cb + 434569c commit d94b1c5
Show file tree
Hide file tree
Showing 14 changed files with 26,966 additions and 43 deletions.
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ dev-copy.py
hide_code.egg*
hide_code.sublime-project
hide_code.sublime-workspace
/hide_code/test/*.ipynb
# /hide_code/test/*.ipynb
/hide_code/test/.ipynb_checkpoints/*
*.iml
*.idea
*ipynb
# *.ipynb
*.pdf
*.html s
.vscode
.ipynb_checkpoints
22 changes: 16 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
language: python
python:
- "2.7"
# Building 3.3 fails;
# ImportError: Tornado requires an up-to-date SSL module. This means Python 2.7.9+ or 3.4+ (although some distributions
# have backported the necessary changes to older versions).
# - "3.3"
- "3.4"
- "3.5"
- "3.6"
- "3.7"
- "3.8"
# does not have headers provided, please ask https://launchpad.net/~pypy/+archive/ppa
# maintainers to fix their pypy-dev package.
- "pypy"
# - "pypy"
# command to install dependencies
install:
# force six to install first as it's a dependicy of simplegeneric and simplegeneric is erroring.
- pip install six
- pip install jupyter
- pip install pdfkit
- pip install -e .
- pip install .
- sudo apt-get update
- sudo apt-get install texlive-xetex xvfb libfontconfig wkhtmltopdf pandoc texlive-fonts-recommended texlive-generic-recommended texlive-latex-extra texlive-generic-extra lmodern

# command to run tests
script:
- jupyter nbextension install --py hide_code --sys-prefix
- jupyter nbextension enable --py hide_code --sys-prefix
- jupyter serverextension enable --py hide_code --sys-prefix
- jupyter nbconvert "$(python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')/hide_code/test/utf-8 test.ipynb" --to hide_code_slides
# wkhtmltopdf has a bug in Ubuntu 16.04 apt package https://unix.stackexchange.com/questions/192642/wkhtmltopdf-qxcbconnection-could-not-connect-to-display
# - jupyter nbconvert "$(python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')/hide_code/test/utf-8 test.ipynb" --to hide_code_pdf
- jupyter nbconvert "$(python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')/hide_code/test/utf-8 test.ipynb" --to hide_code_html
- jupyter nbconvert "$(python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')/hide_code/test/utf-8 test.ipynb" --to hide_code_latex
- jupyter nbconvert "$(python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')/hide_code/test/utf-8 test.ipynb" --to hide_code_latexpdf
# - jupyter nbextension install --py hide_code --sys-prefix
# - jupyter nbextension enable --py hide_code --sys-prefix
# - jupyter serverextension enable --py hide_code --sys-prefix
181 changes: 181 additions & 0 deletions demo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
"## Hide_code: Jupyter code, input and output hiding\n",
"\n",
"To get started, select \"View\" -> \"Cell Toolbar\" -> \"Hide code\""
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Goodbye prompts\n"
]
}
],
"source": [
"print('Goodbye code')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Goodbye prompts\n"
]
}
],
"source": [
"print('Goodbye prompts')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"hideCode": false,
"hideOutput": true,
"hidePrompt": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Goodbye output\n"
]
}
],
"source": [
"print('Goodbye output')"
]
},
{
"cell_type": "markdown",
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
"Change cell toolbar to \"None\" to pretty up the notebook."
]
},
{
"cell_type": "markdown",
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
"Tryout the [keyboard shortcuts](https://github.com/kirbs-/hide_code/wiki/Keyboard-Shortcuts).\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Try hiding code by kitting the W key in command mode (blue bar highlight on this cell).\n"
]
}
],
"source": [
"print(\"Try hiding code by kitting the W key in command mode (blue bar highlight on this cell).\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Unhide code with SHIFT + W.\n"
]
}
],
"source": [
"print(\"Unhide code with SHIFT + W.\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"hideCode": false,
"hidePrompt": false
},
"source": [
"Try selecting the above two cells (SHIFT + click) and hiding/unhiding their code with the \"W\"/SHIFT + \"W\" keys.\n",
"\n",
"Want to hide everything or need to start over? Click on the '</>' icon above. This toggles hidind all code and inputs at once. \n",
"\n",
"When the notebook is formatted to your liking, click on the \"Hide Code\" menu bar to export into your desired format."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"celltoolbar": "Hide code",
"hide_code_all_hidden": false,
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Empty file added environment.yaml
Empty file.
19 changes: 10 additions & 9 deletions hide_code/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from .hide_code_html_exporter import HideCodeHTMLExporter
from .hide_code_pdf_exporter import HideCodePDFExporter
from .hide_code_preprocessor import HideCodePreprocessor
from .hide_code_latex_exporter import HideCodeLatexExporter
from .hide_code_slides_exporter import HideCodeSlidesExporter
from .hide_code import *
from .hide_code_config import HideCodeConfig
from .utils import Utils
from .hide_code import _jupyter_nbextension_paths, _jupyter_server_extension_paths
from hide_code.hide_code_html_exporter import HideCodeHTMLExporter
from hide_code.hide_code_pdf_exporter import HideCodePDFExporter
from hide_code.hide_code_latexpdf_exporter import HideCodeLatexPDFExporter
from hide_code.hide_code_preprocessor import HideCodePreprocessor
from hide_code.hide_code_latex_exporter import HideCodeLatexExporter
from hide_code.hide_code_slides_exporter import HideCodeSlidesExporter
from hide_code.hide_code import *
from hide_code.hide_code_config import HideCodeConfig
from hide_code.utils import Utils
from hide_code.hide_code import _jupyter_nbextension_paths, _jupyter_server_extension_paths
17 changes: 13 additions & 4 deletions hide_code/hide_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ function ($, celltoolbar, Jupyter){
}

function exportLink(path){
return window.location.origin + window.location.pathname + "/export/" + path;
// return window.location.origin + window.location.pathname + "/export/" + path;
return window.location.origin + '/notebooks/' + Jupyter.notebook.notebook_path + "/export/" + path;
}

var hideCodeCallback = ctb.utils.checkbox_ui_generator( 'Hide Code ', hideCodeSetter, hideCodeGetter);
Expand Down Expand Up @@ -326,11 +327,19 @@ function ($, celltoolbar, Jupyter){
});
console.log('hide_code setup complete');
}

// setup();


$(document).on('DOMSubtreeModified', '.filename', function(){
// var filename = $('.filename').text();

$('#hide_code_menu_list a').each(function(){
var path = $(this).attr('href').split('/').pop();
$(this).attr('href', exportLink(path));
});
});


return {
load_ipython_extension: load_ipython_extension
}
});

27 changes: 14 additions & 13 deletions hide_code/hide_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
from notebook.base.handlers import IPythonHandler
import nbformat
from traitlets.config import Config
from .hide_code_html_exporter import HideCodeHTMLExporter
from .hide_code_pdf_exporter import HideCodePDFExporter
from .hide_code_latex_exporter import HideCodeLatexExporter
from .hide_code_slides_exporter import HideCodeSlidesExporter
from hide_code.hide_code_html_exporter import HideCodeHTMLExporter
from hide_code.hide_code_pdf_exporter import HideCodePDFExporter
from hide_code.hide_code_latexpdf_exporter import HideCodeLatexPDFExporter
from hide_code.hide_code_latex_exporter import HideCodeLatexExporter
from hide_code.hide_code_slides_exporter import HideCodeSlidesExporter
import pdfkit
from notebook.services.config import ConfigManager
from .hide_code_config import HideCodeConfig as hc_config
from .utils import Utils
from hide_code.hide_code_config import HideCodeConfig as hc_config
from hide_code.utils import Utils


notebook_dir = []
Expand All @@ -40,13 +41,13 @@ def get(self, *args):
self.log.info("hide_code: Starting PDF export for {}".format(args[-1]))
with open(ipynb_file_name(args), encoding="utf-8") as f:
nb = nbformat.reads(f.read(), as_version=4)
exporter = HideCodeHTMLExporter()
exporter = HideCodePDFExporter()
output_html, resources = exporter.from_notebook_node(nb)
output = pdfkit.from_string(output_html, False)
# output = pdfkit.from_string(output_html, False)
self.set_header('Content-Type', 'application/pdf')
self.set_header('Content-Disposition', 'attachment; filename=' + notebook_name(args, 'pdf'))
self.flush()
self.write(output)
self.write(output_html)
self.log.info("hide_code: Finished PDF export for {}".format(args[-1]))
self.finish()

Expand All @@ -56,8 +57,8 @@ def get(self, *args):
self.log.info("hide_code: Starting Latex PDF export for {}".format(args[-1]))
with open(ipynb_file_name(args), encoding="utf-8") as f:
nb = nbformat.reads(f.read(), as_version=4)
exporter = HideCodePDFExporter()
output, resources = exporter.from_notebook_node(nb, resources={"metadata": {"name": notebook_name(args)}})
exporter = HideCodeLatexPDFExporter()
output, resources = exporter.from_notebook_node(nb, resources={"metadata": {"name": notebook_name(args, 'pdf')}})
self.set_header('Content-Disposition', 'attachment; filename=' + notebook_name(args, 'pdf'))
self.flush()
self.write(output)
Expand All @@ -71,7 +72,7 @@ def get(self, *args):
with open(ipynb_file_name(args), encoding="utf-8") as f:
nb = nbformat.reads(f.read(), as_version=4)
exporter = HideCodeLatexExporter()
output, resources = exporter.from_notebook_node(nb, resources={"metadata": {"name": notebook_name(args)}})
output, resources = exporter.from_notebook_node(nb, resources={"metadata": {"name": notebook_name(args, 'tex')}})
self.set_header('Content-Disposition', 'attachment; filename=' + notebook_name(args, 'tex'))
self.flush()
self.write(output)
Expand All @@ -85,7 +86,7 @@ def get(self, *args):
with open(ipynb_file_name(args), encoding="utf-8") as f:
nb = nbformat.reads(f.read(), as_version=4)
exporter = HideCodeSlidesExporter()
output, resources = exporter.from_notebook_node(nb, resources={"metadata": {"name": notebook_name(args)}})
output, resources = exporter.from_notebook_node(nb, resources={"metadata": {"name": notebook_name(args, 'html')}})
self.set_header('Content-Disposition', 'attachment; filename=' + notebook_name(args, 'html'))
self.flush()
self.write(output)
Expand Down
25 changes: 25 additions & 0 deletions hide_code/hide_code_latexpdf_exporter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
import os.path

from traitlets.config import Config
from nbconvert.exporters.pdf import PDFExporter
from traitlets.log import get_logger


class HideCodeLatexPDFExporter(PDFExporter):
def __init__(self, config=None, **kw):
# self.register_preprocessor('hide_code.HideCodePreprocessor', True)
super(HideCodeLatexPDFExporter, self).__init__(config, **kw)
self.preprocessors = ['hide_code.HideCodePreprocessor']
self._init_preprocessors()

def _template_file_default(self):
return 'hide_code_article'

@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(HideCodeLatexPDFExporter, self).template_path + [os.path.join(os.path.dirname(__file__), "Templates")]
Loading

0 comments on commit d94b1c5

Please sign in to comment.