Skip to content

Instantly share code, notes, and snippets.

@szapp
Created July 9, 2018 15:30
Show Gist options
  • Save szapp/fca3ea6f46fc6f61d674d691e1d03d47 to your computer and use it in GitHub Desktop.
Save szapp/fca3ea6f46fc6f61d674d691e1d03d47 to your computer and use it in GitHub Desktop.
Jupyter HTML template for "html_with_lenvs" for local viewing
#!/bin/bash
# Create report from juptyer notebook for local viewing (no internet)
# with support for the nbextensions
# - latex_envs
# - hide_input
#
# Download the following javascript projects and place them
# into the destination directory with macthing directory names.
# - font-awesome-4.7.0
# - jquery-2.0.3
# - jquery-ui-1.11.4
# - jquery-ui-1.9.1
# - jquery-ui-themes-1.12.1
# - latex_envs
# - marked-0.3.5
# - MathJax-2.7.4
# - nbextensions
# - requirejs-2.1.10
if [[ "$1" ]]; then
destpath=$1
else
destpath="./"
fi
destname=$(date +%y-%m-%d)_report.html
dest=${destpath}${destname}
mkdir -pv "${destpath}"
jupyter nbconvert "$1" --to html_with_lenvs \
--no-prompt \
--template ./jupyter_report.tpl \
--output "${dest}"
# --HTMLExporter.exclude_input=True
# Replace internet links with local ones
sed -i -e 's/https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/require.js\/2.1.10\/require.min.js/.\/requirejs-2.1.10\/require.js/g' "${dest}"
sed -i -e 's/https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/jquery\/2.0.3\/jquery.min.js/.\/jquery-2.0.3\/jquery.min.js/g' "${dest}"
sed -i -e 's/https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/mathjax\/2.7.1\/MathJax.js?config=TeX-AMS_HTML/.\/MathJax-2.7.4\/MathJax.js\?config=TeX-AMS_HTML/g' "${dest}"
{%- extends 'full.tpl' -%}
<!-- Copied from nbextensions.tpl (extending did not work) --->
{% block input_group -%}
{%- if cell.metadata.hide_input or nb.metadata.hide_input -%}
{%- else -%}
{{ super() }}
{%- endif -%}
{% endblock input_group %}
{% block output_group -%}
{%- if cell.metadata.hide_output -%}
{%- else -%}
{{ super() }}
{%- endif -%}
{% endblock output_group %}
{% block output_area_prompt %}
{%- if cell.metadata.hide_input or nb.metadata.hide_input -%}
<div class="prompt"> </div>
{%- else -%}
{{ super() }}
{%- endif -%}
{% endblock output_area_prompt %}
{%- block html_head -%}
<meta charset="utf-8"/>
<!-- javascript from CDN for conversion -->
<script src="./marked-0.3.5/marked.min.js"></script>
{% if nb['metadata']['latex_envs']['eqLabelWithNumbers'] == True %}
<script type="text/x-mathjax-config">
// make sure that equations numbers are enabled
MathJax.Hub.Config({ TeX: { equationNumbers: {
autoNumber: "AMS", // All AMS equations are numbered
useLabelIds: true, // labels as ids
// format the equation number - uses an offset eqNumInitial (default 0)
formatNumber: function (n) {return String(Number(n)+Number({{nb['metadata']['latex_envs']['eqNumInitial']}})-1)}
} }
});
</script>
{% else %}
<script type="text/x-mathjax-config">
// make sure that equations numbers are enabled
MathJax.Hub.Config({ TeX: { equationNumbers: {
autoNumber: "none", // All AMS equations are numbered
useLabelIds: true, // labels as ids
} }
});
</script>
{% endif %}
{{ super() }}
<link rel="stylesheet" href="./jquery-ui-themes-1.12.1/jquery-ui.css">
<script type="text/javascript" src="./jquery-ui-1.9.1/ui/jquery-ui.js"></script>
<!-- stylesheet from CDN -->
<link rel="stylesheet" type="text/css" href="./font-awesome-4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="./latex_envs/latex_envs.css">
<!-- Custom stylesheet, it must be in the same directory as the html file -->
<link rel="stylesheet" href="custom.css">
<!-- Load mathjax
<script src="https://rawgit.com/ipython-contrib/jupyter_contrib_nbextensions/master/src/jupyter_contrib_nbextensions/nbextensions/latex_envs/thmsInNb4.js"></script>
-->
<script type="text/javascript" src="./latex_envs/thmsInNb4.js"> </script>
<!-- for toc2 -->
<link rel="stylesheet" type="text/css" href="./nbextensions/toc2/main.css">
<link rel="stylesheet" type="text/css" href="./font-awesome-4.7.0/css/font-awesome.min.css">
<script src="./nbextensions/toc2/toc2.js"></script>
<script>
$( document ).ready(function(){
//Value of configuration variables, some taken from the notebook's metada.
eqNum = 0; // begins equation numbering at eqNum+1
eqLabelWithNumbers = "{{nb['metadata']['latex_envs']['eqLabelWithNumbers']}}"=="True" ? true : false; //if true, label equations with equation numbers;
//otherwise using the tag specified by \label
conversion_to_html = false;
current_cit=1;
cite_by='key'; //only number and key are supported
//var document={}
document.bibliography={};
// Read environment map config
initmap();
// Read user envs config, if specified
{% if nb['metadata']['latex_envs']['user_envs_cfg'] == True %}
var data = {{ include_userenvs_cfg() }}
environmentMap = $.extend(true,{}, environmentInitialMap,data)
{% else %}
environmentMap = $.extend(true,{}, environmentInitialMap)
{% endif %}
// fire the main function with these parameters
var html_to_analyse = $('body').html()
var html_converted = thmsInNbConv(marked,html_to_analyse);
html_converted = html_converted.replace(/%[\S\t ]*<\/p>/gm,"</p>")
$('body').html(html_converted)
// Show/hide anchors
var labels_anchors = "{{nb['metadata']['latex_envs']['labels_anchors']}}"=="True" ? true : false;
$('.latex_label_anchor').toggle(labels_anchors)
// Number all environments
report_style_numbering = "{{nb['metadata']['latex_envs']['report_style_numbering']}}"=="True" ? true : false;
reset_counters();
renumberAllEnvs();
// toc2
var cfg = {{ nb.get('metadata', {}).get('toc', {})|tojson|safe }};
// fire the main function with these parameters
require(['nbextensions/toc2/toc2'], function (toc2) {
toc2.table_of_contents(cfg);
});
});
</script>
<!-- Larger figures -->
<style type="text/css">
div.output_subarea {
max-width: 100%!important;
}
</style>
<!-- Remove right margin for lists -->
<style>
.rendered_html ol {
margin-right: 0!important;
}
</style>
{%- endblock html_head -%}
{% block body %}
{% if nb['metadata']['latex_envs']['latex_user_defs'] == True %}
<div id='latex_user_defs'>
{{ include_latexdefs('latexdefs.tex') }}
</div>
{% endif %}
{{ super() }}
{%- endblock body %}
{% block footer %}
</html>
{% endblock footer %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment