public
Last active

Demo of hideable hints and scrubbing solutions from IPython notebooks

  • Download Gist
SWCDemo.ipynb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
{
"metadata": {
"name": "SWCDemo"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Initial cell has buttons to show/hide all hint cells"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<script>\n",
" toggle_md_cell = function(divid) {\n",
" $(\"#\" + divid).parent().parent().toggle();\n",
" };\n",
" hide_all_hints = function() {\n",
" $(\"div.hint\").parent().parent().hide();\n",
" };\n",
" show_all_hints = function() {\n",
" $(\"div.hint\").parent().parent().show();\n",
" };\n",
"</script>\n",
"\n",
"<button onclick=\"show_all_hints();\">show hints</button> <button onclick=\"hide_all_hints();\">hide hints</button>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# this code cell isn't marked, so it survives\n",
"print \"Welcome to Software Carpentry!\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Welcome to Software Carpentry!\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Problem 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Click this button to reveal a hint: <button onclick=\"toggle_md_cell('hint1');\">hint</button>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## This is Hint # 1\n",
"\n",
"We mark each hint cell with an empty div:\n",
"\n",
" <div id=\"hint2\" class=\"hint\"></div>\n",
"\n",
"<div id=\"hint1\" class=\"hint\"></div>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Solution 1\n",
"def solution_one():\n",
" print 'hi'\n",
"solution_one()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"hi\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Problem 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Click this button to reveal the next hint: <button onclick=\"toggle_md_cell('hint2');\">hint</button>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### This is Hint #2\n",
"\n",
"Here is how we are toggling hints:\n",
"\n",
" <button onclick=\"toggle_md_cell('hint2');\">toggle hint</button>\n",
"\n",
"<div id=\"hint2\" class=\"hint\"></div>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Solution 2\n",
"print \"hello\", \"world\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"hello world\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*footer that hides all hints on load*\n",
"\n",
"Alternatively, one could add individual hides inside each hint cell,\n",
"but this is the simplest way to hide all of them on page load.\n",
"\n",
"<script>\n",
" hide_all_hints();\n",
"</script>"
]
}
],
"metadata": {}
}
]
}
SWCDemo_scrub.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
{
"metadata": {
"name": "SWCDemo"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Initial cell has buttons to show/hide all hint cells"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<script>\n",
" toggle_md_cell = function(divid) {\n",
" $(\"#\" + divid).parent().parent().toggle();\n",
" };\n",
" hide_all_hints = function() {\n",
" $(\"div.hint\").parent().parent().hide();\n",
" };\n",
" show_all_hints = function() {\n",
" $(\"div.hint\").parent().parent().show();\n",
" };\n",
"</script>\n",
"\n",
"<button onclick=\"show_all_hints();\">show hints</button> <button onclick=\"hide_all_hints();\">hide hints</button>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# this code cell isn't marked, so it survives\n",
"print \"Welcome to Software Carpentry!\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Welcome to Software Carpentry!\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Problem 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Click this button to reveal a hint: <button onclick=\"toggle_md_cell('hint1');\">hint</button>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## This is Hint # 1\n",
"\n",
"We mark each hint cell with an empty div:\n",
"\n",
" <div id=\"hint2\" class=\"hint\"></div>\n",
"\n",
"<div id=\"hint1\" class=\"hint\"></div>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Solution goes here"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Problem 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Click this button to reveal the next hint: <button onclick=\"toggle_md_cell('hint2');\">hint</button>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### This is Hint #2\n",
"\n",
"Here is how we are toggling hints:\n",
"\n",
" <button onclick=\"toggle_md_cell('hint2');\">toggle hint</button>\n",
"\n",
"<div id=\"hint2\" class=\"hint\"></div>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Solution goes here"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*footer that hides all hints on load*\n",
"\n",
"Alternatively, one could add individual hides inside each hint cell,\n",
"but this is the simplest way to hide all of them on page load.\n",
"\n",
"<script>\n",
" hide_all_hints();\n",
"</script>"
]
}
],
"metadata": {}
}
]
}
scrub_code.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#!/usr/bin/env python
"""
simple example script for scrubping solution code cells from IPython notebooks
 
Usage: `scrub_code.py foo.ipynb [bar.ipynb [...]]`
 
Marked code cells are scrubbed from the notebook
"""
 
import io
import os
import sys
 
from IPython.nbformat.current import read, write
 
def scrub_code_cells(nb):
scrubbed = 0
cells = 0
for ws in nb.worksheets:
for cell in ws.cells:
if cell.cell_type != 'code':
continue
cells += 1
# scrub cells marked with initial '# Solution' comment
# any other marker will do, or it could be unconditional
if cell.input.startswith("# Solution"):
cell.input = u'# Solution goes here'
scrubbed += 1
cell.outputs = []
 
print
print "scrubbed %i/%i code cells from notebook %s" % (scrubbed, cells, nb.metadata.name)
 
if __name__ == '__main__':
for ipynb in sys.argv[1:]:
print "scrubbing %s" % ipynb
with io.open(ipynb, encoding='utf8') as f:
nb = read(f, 'json')
scrub_code_cells(nb)
base, ext = os.path.splitext(ipynb)
new_ipynb = "%s_scrub%s" % (base, ext)
with io.open(new_ipynb, 'w', encoding='utf8') as f:
write(nb, f, 'json')
print "wrote %s" % new_ipynb

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.