Skip to content

Instantly share code, notes, and snippets.

@jcb91
Last active January 26, 2017 12:44
Show Gist options
  • Select an option

  • Save jcb91/286ae54c8d631501d543c495b2ad1e81 to your computer and use it in GitHub Desktop.

Select an option

Save jcb91/286ae54c8d631501d543c495b2ad1e81 to your computer and use it in GitHub Desktop.
Jupyter notebook to test arbitrary numbers of headings
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%javascript\n",
"\n",
"var lipsum = (\n",
"'Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor '+\n",
"'incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam quis'+\n",
"'nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat '+\n",
"'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore '+\n",
"'eu fugiat nulla pariatur Excepteur sint occaecat cupidatat non proident sunt '+\n",
"'in culpa qui officia deserunt mollit anim id est laborum'\n",
").split(' ');\n",
"\n",
"function lipsentence (maxwords) {\n",
" maxwords = Math.ceil((maxwords || 20) * Math.random());\n",
" var sentence = [];\n",
" while (sentence.length < maxwords) {\n",
" sentence.push(lipsum[Math.floor(Math.random() * (lipsum.length - 1))]);\n",
" }\n",
" sentence = sentence.join(' ');\n",
" return sentence.substring(0, 1).toUpperCase() + sentence.substring(1) + '.';\n",
"}\n",
"\n",
"function liptitle (level) {\n",
" var title = '# ' + lipsentence(10);\n",
" for (var ii = level; ii > 0; ii--) { title = '#' + title; }\n",
" return title;\n",
"}\n",
"\n",
"// delete existing cells\n",
"function delete_all_cells (starting_ind) {\n",
" starting_ind = starting_ind || 1;\n",
" var inds = [];\n",
" for (var ii = Jupyter.notebook.ncells() - 1; ii > starting_ind - 1; ii--) {\n",
" inds.push(ii);\n",
" }\n",
" Jupyter.notebook.delete_cells(inds);\n",
"}\n",
"\n",
"// add new headings\n",
"function add_new_sections (num_sections) {\n",
" num_sections = num_sections || 1\n",
" for (var ii=0; ii < num_sections; ii++) {\n",
" var hcell = Jupyter.notebook.insert_cell_at_bottom('markdown');\n",
" var hlevel = Math.floor(Math.random() * 6);\n",
" var htext = liptitle(hlevel);\n",
" hcell.set_text(htext);\n",
" hcell.render();\n",
" for (var jj=0; jj < Math.floor(Math.random() * hlevel); jj++) {\n",
" Jupyter.notebook.insert_cell_at_bottom('code').set_text(lipsentence());\n",
" }\n",
" }\n",
"}\n",
"\n",
"// stops this happening on notebook load\n",
"if (window._jcb91_add_sections === undefined) {\n",
" $(element).append($('<p>').text('not adding cells - you need to set window._jcb91_add_sections'));\n",
"}\n",
"else {\n",
" var cells = Jupyter.notebook.get_cells();\n",
" var nheadings = 0;\n",
" for (var ii = 1; ii < cells.length; ii++) {\n",
" if (cells[ii].type === 'markdown' && cells[ii].get_text().substring(0, 1) == '#') {\n",
" nheadings++;\n",
" }\n",
" }\n",
" var to_add = window._jcb91_add_sections - nheadings;\n",
" if (to_add < 1) {\n",
" $(element).append($('<p>').text('not adding cells - there are already ' + nheadings + '.'));\n",
" }\n",
" else {\n",
" add_new_sections(to_add);\n",
" }\n",
"}"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [default]",
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment