diff --git a/OrganizeCode/OrganizeCode.ipynb b/OrganizeCode/OrganizeCode.ipynb index 8166f1e..b070445 100644 --- a/OrganizeCode/OrganizeCode.ipynb +++ b/OrganizeCode/OrganizeCode.ipynb @@ -1,175 +1,175 @@ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ - "# How to organize code in your notebooks?\n", - "It is important to have your notebooks as free as possible from Python code that is not providing any useful information for your students, or that can add unecessary complexity to your notebook. Sometimes, hidding some code in an external file is a nice solution.\n", + "# How to organize your code in your notebooks?\n", + "It is important to have your notebooks as free as possible from Python code that is not providing any useful information for your students, or that add unecessary complexity to your notebook. Sometimes, hidding some code in an external file is a nice solution.\n", "\n", "You can also use external Python files to put classes or functions in common, and reuse them in several notebooks. Having the same code in a single place really helps with the maintenance work." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using the magic \"%run\"\n", "\n", "Using the magic ```%run filename``` will execute the provided Python file, just like if the Python code was in the cell. This is useful to load Python resources from an external file." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%run libs/mylib.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Every time your run the above cell, the python code from the file is executed. So if you change the Python file, you just have to play the cell again." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m = MyLib(\"%load\")\n", "m.greet()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that you have to use UNIX-style path to reference your files (```libs/mylib.py```, with ```/```s in the path)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using python's ```import``` [recommended]\n", "By using the standard ```import``` mechanism from Python, you can also load external Python files." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from libs.mylib import MyLib" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the path to the file is now using Python's nomenclature (using ```.``` as separator), which will work on all systems." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m = MyLib(\"import\")\n", "m.greet()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What if you change the code in the Python file? Running the ```import``` line again will not help: imports are only executed once for your current kernel.\n", "While this is not an issue for anyone just running the notebook, it might be inconvenient for you during the time you are working on the external file and testing it. To take the new Python code into account, you have to:\n", "\n", "1. restart your kernel and execute the import again\n", "1. force the reloading\n", "\n", "How do you force Python to actually import your external files? You can use the ```%reload_ext autoreload``` magic for that!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%reload_ext autoreload" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%autoreload 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once the two cells above have been executed, all imports in the notebook will systematically import the " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from libs.mylib import MyLib" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m = MyLib(\"import reloaded\")\n", "m.greet()" ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], - "source": [] + "source": [ + "## EOF" + ] } ], "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.8" } }, "nbformat": 4, "nbformat_minor": 2 }