From Cells to a Module
From Cells to a Module: From Notebook Cells to Functions, Your Own Modules and Packages, Using Your Module in a Notebook: autoreload.
Updated
- From Notebook Cells to Functions (Journeyman): A notebook that answers this week's questions has to be edited by hand for next week's file, so the cells you run again and again become functions whose parameters are what changes, such as the file and how many top items to show, and which return their results instead of printing them.
- Your Own Modules and Packages (Apprentice): Any .py file is a module another file can import, so a growing program is split into files by job, a folder of them with __init__.py becomes a package, and the __name__ check keeps a file's own test lines from running when it's imported.
- Using Your Module in a Notebook: autoreload (Journeyman): A notebook can import functions from your own .py file, but the kernel keeps the version it imported first, so after editing the file you restart the kernel and run every cell, or turn on %load_ext autoreload and %autoreload 2 to load each saved change before every cell runs.