Skip to content

Instantly share code, notes, and snippets.

Created April 24, 2017 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/b5734e6898cd00cf697ac8e1c0757d05 to your computer and use it in GitHub Desktop.
Save anonymous/b5734e6898cd00cf697ac8e1c0757d05 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import display, Javascript\n",
"ip = get_ipython()\n",
"\n",
"def test_success():\n",
" square = ip.user_ns.get('square')\n",
" if not square:\n",
" return False\n",
" try:\n",
" five_squared = square(5)\n",
" except Exception:\n",
" return False\n",
" else:\n",
" return five_squared == 25\n",
"\n",
"def add_section():\n",
" display(Javascript(r\"\"\"\n",
" var nb = Jupyter.notebook;\n",
" var idx = nb.get_selected_index();\n",
" var cell = nb.get_selected_cell();\n",
" if (!cell.get_text().match(/Section 2/)) {\n",
" var md_cell = nb.insert_cell_below('markdown');\n",
" md_cell.set_text(\"# Section 2\\n\\nSuccess! Moving on...\");\n",
" md_cell.render();\n",
" var code_cell = nb.insert_cell_below('code', idx + 1);\n",
" code_cell.set_text(\"def cube(x):...\");\n",
" nb.select(idx + 2);\n",
" code_cell.select();\n",
" }\n",
" \"\"\"))\n",
"\n",
"def add_section_if_correct():\n",
" if 'def square' not in ip.user_ns['In'][-1]:\n",
" return\n",
" if test_success():\n",
" add_section()\n",
" else:\n",
" print(\"Try again...\")\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"ip.events.register('post_run_cell', add_section_if_correct)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Try again...\n"
]
}
],
"source": [
"def square(x):\n",
" return x + x"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"application/javascript": [
"\n",
" var nb = Jupyter.notebook;\n",
" var idx = nb.get_selected_index();\n",
" var cell = nb.get_selected_cell();\n",
" if (!cell.get_text().match(/Section 2/)) {\n",
" var md_cell = nb.insert_cell_below('markdown');\n",
" md_cell.set_text(\"# Section 2\\n\\nSuccess! Moving on...\");\n",
" md_cell.render();\n",
" var code_cell = nb.insert_cell_below('code', idx + 1);\n",
" code_cell.set_text(\"def cube(x):...\");\n",
" nb.select(idx + 2);\n",
" code_cell.select();\n",
" }\n",
" "
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"def square(x):\n",
" return x * x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Section 2\n",
"\n",
"Success! Moving on..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def cube(x):..."
]
}
],
"metadata": {
"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.6.0"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {
"7aa59ce3600e4506a5156f8cf65bf0b4": {
"model_module": "jupyter-js-widgets",
"model_module_version": "*",
"model_name": "IntSliderModel",
"state": {
"_model_module_version": "*",
"_view_module_version": "*",
"layout": "IPY_MODEL_efa0d1f81cdc491e92030ed947987b0d",
"min": 10,
"style": "IPY_MODEL_8615b697f6264ecc83e16d4b70943a5a",
"value": 57
}
},
"8615b697f6264ecc83e16d4b70943a5a": {
"model_module": "jupyter-js-widgets",
"model_module_version": "*",
"model_name": "SliderStyleModel",
"state": {
"_model_module_version": "*",
"_view_module_version": "*"
}
},
"efa0d1f81cdc491e92030ed947987b0d": {
"model_module": "jupyter-js-widgets",
"model_module_version": "~2.1.4",
"model_name": "LayoutModel",
"state": {
"_model_module_version": "~2.1.4",
"_view_module_version": "~2.1.4"
}
}
},
"version_major": 1,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment