{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "If you see below, this notebook is not trusted.\n", "As a consequence Autorun cannot work.\n", "Run \"from notebook_autorun import Autorun; Autorun.info()\" for more info.\n" ] }, { "data": { "application/javascript": [ "\n", "\"use strict\";\n", "\n", "console.log('start main');\n", "\n", "require([\n", "\t'jquery',\n", "\t'base/js/dialog',\n", "\t'base/js/events',\n", "\t'base/js/namespace',\n", "\t'notebook/js/celltoolbar',\n", "\t'notebook/js/codecell',\n", "\t'base/js/promises'\n", "], function (\n", "\t$,\n", "\tdialog,\n", "\tevents,\n", "\tJupyter,\n", "\tcelltoolbar,\n", "\tcodecell,\n", "\tpromises\n", ") {\n", "\t\tevents.on('kernel_ready.Kernel', function () {\n", "\t\t\tconsole.log('start autorun');\n", "\t\t\tif (Jupyter.notebook.trusted) {\n", "\t\t\t\t// notebook is trusted js can run\n", "\t\t\t\tconsole.log('notebook is trusted');\n", "\t\t\t\tlet nbCells = Jupyter.notebook.get_cells(),\n", "\t\t\t\t\tarrCells;\n", "\t\t\t\twindow.debugNbCells = nbCells;\n", "\n", "\t\t\t\t\n", "\n", "\t\t\t\t\n", "\n", "\t\t\t\t\n", "\t\t\t\t\n", "\n", "let getCellsFromComment = function (nbCells, commentFlag) {\n", "\n", "\tlet arrCell = [];\n", "\tfor (let [i, nbCell] of nbCells.entries()) {\n", "\n", "\t\tlet text = nbCell.get_text();\n", "\t\t\n", "\t\tif (isFlagInComment(text, commentFlag)) {\n", "\t\t\tarrCell.push(i);\n", "\t\t}\n", "\t}\n", "\treturn arrCell;\n", "};\n", "\n", "let isFlagInComment = function (text, flag) {\n", "\tlet arrText = text.split('\\n');\n", "\tlet isFlag = false;\n", "\tfor (let line of arrText) {\n", "\t\tline = line.trim();\n", "\t\tif (line.startsWith('#')) {\n", "\t\t\tlet found = line.indexOf(flag) > -1;\n", "\t\t\tif (found) {\n", "\t\t\t\tisFlag = true;\n", "\t\t\t}\n", "\t\t}\n", "\t}\n", "\treturn isFlag;\n", "};\n", "\t\t\t\tarrCells = getCellsFromComment(nbCells, 'AUTORUN');\n", "\t\t\t\t\n", "\n", "\t\t\t\tconsole.log(arrCells);\n", "\t\t\t\tfor (let i of arrCells) {\n", "\t\t\t\t\tlet nbCell = nbCells[i];\n", "\t\t\t\t\tif (nbCell) {\n", "\t\t\t\t\t\tconsole.log('execute cell ' + i);\n", "\t\t\t\t\t\tnbCell.execute();\n", "\t\t\t\t\t}\n", "\t\t\t\t\telse {\n", "\t\t\t\t\t\tconsole.log('cell ' + i + ' does not exist');\n", "\t\t\t\t\t}\n", "\t\t\t\t}\n", "\n", "\t\t\t\t\n", "\t\t\t\t\n", "\n", "let getCellToFocus = function(nbCells, focus) {\n", "\n", "\tfocus = parseInt(focus);\n", "\tfocus = focus >= 0 ? focus : transformNegativeIndex(focus, nbCells);\n", "\treturn focus;\n", "};\n", "\n", "let transformNegativeIndex = function(negIndex, nbCells) {\n", "\treturn nbCells.length + negIndex;\n", "};\n", "\t\t\t\tlet f = getCellToFocus(nbCells, '-1');\n", "\t\t\t\tlet nbCell = nbCells[f];\n", "\t\t\t\tif (nbCell) {\n", "\t\t\t\t\tconsole.log('focus on cell ' + f);\n", "\t\t\t\t\tnbCell.focus_cell();\n", "\t\t\t\t}\n", "\t\t\t\telse {\n", "\t\t\t\t\tconsole.log('cell ' + f + ' does not exist');\n", "\t\t\t\t}\n", "\t\t\t\t\n", "\n", "\t\t\t}\n", "\t\t\telse {\n", "\t\t\t\t// notebook is not trusted let the user know about it\n", "\t\t\t\t// by definition this should never happen as\n", "\t\t\t\t// js cannot run if notebook not trustee\n", "\t\t\t\tconsole.log('notebook is not trusted');\n", "\t\t\t\tconsole.log('nothing is done');\n", "\t\t\t}\n", "\t\t});\n", "\n", "\n", "\n", "\n", "\t});\n", "\n", "console.log('end main');" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "This output cell contains **notebook-autorun** settings: \n", "```json\n", " {\"comment\": true, \"comment_flag\": \"AUTORUN\", \"focus\": -1} \n", "```" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from notebook_autorun import Autorun\n", "cells = '3,-1'\n", "Autorun(cells=None, metadata=False, comment=True, comment_flag='AUTORUN', focus=-1, verbose=True).add_js()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# manually executed after previous cell failure and indication\n", "# from notebook_autorun import Autorun; Autorun.info()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "autorun": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11\n" ] } ], "source": [ "# AUTORUN\n", "i = 11; print(i); i += 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "autorun": true }, "outputs": [], "source": [ "print(i); i += 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(i); i += 1" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\n" ] } ], "source": [ "# AUTORUN\n", "print(i); i += 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(i); i += 1" ] } ], "metadata": { "celltoolbar": "Edit 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.3" } }, "nbformat": 4, "nbformat_minor": 2 }