Created
October 29, 2019 10:48
-
-
Save psychemedia/8df4d41720422b36bd1d01a85d7398cb to your computer and use it in GitHub Desktop.
nbconvert template for generating ThebeLab enabled HTML page from ipynb notebook
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
<!-- | |
Template to geneate a ThebeLab enhanced HTML page. | |
The Binder / Jupyyer kernel target needs setting explicitly in the template. | |
Run using: jupyter nbconvert --to html 'my notebook.ipynb' --template=thebelab.tpl | |
--> | |
{% extends 'full.tpl'%} | |
{% block header %} | |
{{ super() }} | |
<script type="text/x-thebe-config"> | |
{ | |
requestKernel: true, | |
binderOptions: { | |
repo: "binder-examples/requirements", | |
}, | |
} | |
</script> | |
<script src="https://unpkg.com/[email protected]/lib/index.js"></script> | |
{% endblock header %} | |
{%- block body %} | |
<button id="activateButton" style="width: 150px; height: 75px; font-size: 1.5em;">Activate</button> | |
<script> | |
var bootstrapThebe = function() { | |
thebelab.bootstrap(); | |
} | |
document.querySelector("#activateButton").addEventListener('click', bootstrapThebe) | |
</script> | |
{{ super() }} | |
<script> | |
//via https://github.com/jupyter/jupyter-book/blob/c37959a8ae8923fafecb64c46e95c026b3739cac/jupyter_book/book_template/_includes/js/thebelab.html#L44 | |
// Find all code cells, replace with Thebelab interactive code cells | |
const codeCells = document.querySelectorAll('.input_area pre') | |
codeCells.forEach((codeCell, index) => { | |
codeCell.setAttribute('data-executable', 'true') | |
// Figure out the language it uses and add this too | |
var parentDiv = codeCell.parentElement.parentElement; | |
var arrayLength = parentDiv.classList.length; | |
for (var ii = 0; ii < arrayLength; ii++) { | |
var parts = parentDiv.classList[ii].split('language-'); | |
if (parts.length === 2) { | |
// If found, assign dataLanguage and break the loop | |
var dataLanguage = parts[1]; | |
break; | |
} | |
} | |
codeCell.setAttribute('data-language', dataLanguage) | |
// If the code cell is hidden, show it | |
var inputCheckbox = document.querySelector(`input#hidebtn${codeCell.id}`); | |
if (inputCheckbox !== null) { | |
setCodeCellVisibility(inputCheckbox, 'visible'); | |
} | |
}); | |
</script> | |
{%- endblock body %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment