{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# %load_ext autoreload\n", "# %autoreload 2" ] }, { "cell_type": "code", "execution_count": 8, "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\t// promises.app_initialized.then(function (appname) {\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", "\t\t\t\t\n", "\n", "function getCellsFromMetadata(nbCells) {\n", "\n", "\tlet arrCell = [];\n", "\tfor (let [i, nbCell] of nbCells.entries()) {\n", "\t\tif (nbCell.metadata.autorun) {\n", "\t\t\tarrCell.push(i);\n", "\t\t}\n", "\t}\n", "\n", "\treturn arrCell;\n", "}\n", "\t\t\t\tarrCells = getCellsFromMetadata(nbCells);\n", "\t\t\t\t\n", "\n", "\t\t\t\t\n", "\n", "\t\t\t\tconsole.log(arrCells);\n", "\t\t\t\t// console.log(nbCells);\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", "\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\tconsole.log('notebook is not trusted');\n", "\t\t\t\tconsole.log('nothing is done');\n", "\t\t\t}\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", " {\"str_cells\": null, \"metadata\": true, \"comment\": false, \"comment_flag\": \"# AUTORUN\"} \n", "```" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from notebook_autorun import Autorun\n", "cells = '3,-1'\n", "Autorun(cells=cells, metadata=False, comment=False, focus=None, verbose=True).add_js()\n", "\n", "# in nbviewer.org the Javascript object (code) and Markdown object (status msg) are displayed\n", "# as there is no security mechanism disabling their execution - as opposed to the real notebook\n", "# So you should see the following, instead of the actual status message:\n", "#\n", "#\n", "#\n", "#" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "### notebook-autorun\n", "\n", "This module enables auto-run of certains cells at notebook kernel start. \n", "It will work **only** for **trusted** notebooks. \n", "\n", "#### 1 - Settings\n", "\n", "The cells to autorun can be determined by one of the 3 args below. \n", "Only one of them must be specified: \n", "+ `cells`: List of 0-indexed cells or String representing a list of cells. [Python list slices](https://docs.python.org/2.3/whatsnew/section-slices.html) are allowed. Default is `None`. `cells` is stringified to `str_cells` before passing to javascript. Invalid cell numbers (e.g. greater than the notebook number of cells) are ignored. Examples: [2, 3], '2,3', '2:', ':-2', '4:8', '::-1' \n", "+ `metadata`: Boolean (default is `False`). If `True`, all cells with medatada `\"autorun\": true` are concerned. \n", "+ `comment`: Boolean (default is `False`). If `True`, all cells containing a comment `comment_flag` (default is `# AUTORUN`) are concerned. \n", "\n", "\n", "#### 2 - Security\n", "\n", "Because a notebook is designed to allow the user to write arbitrary code, it has full access to many resources. \n", "\n", "The typical risks are the following:\n", "+ A notebook has access to your file system and can therefore potentially read/modify/delete any of your files or send them to an attacker, or write a new file (virus). \n", "+ A notebook may contain javascript in output cells which can read you cookies and local storage and potentially send them to an attacker. \n", "\n", "See the [Security in notebook documents](https://jupyter-notebook.readthedocs.io/en/stable/security.html#security-in-notebook-documents) section of the official [Jupyter Notebook documentation](https://jupyter-notebook.readthedocs.io/en/stable/index.html) for more info. \n", "\n", "Therefore you **should review** and **must trust** the notebook before you can use **notebook-autorun**." ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# manually executed after previous cell failure and indication\n", "from notebook_autorun import Autorun; Autorun.info()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "autorun": true }, "outputs": [], "source": [ "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": null, "metadata": {}, "outputs": [], "source": [ "print(i); i += 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(i); i += 1" ] } ], "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.1" } }, "nbformat": 4, "nbformat_minor": 2 }