From Notebook to Script
A report you rebuild by hand every week is a chore. Using a food truck's weekly sales, turn notebook cells into functions, move them into a module your notebook imports, and write a script that finds the newest data and rebuilds the report, charts and all, with one command.
Updated
From Cells to a Module
- 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.
A Script That Rebuilds the Report
- Paths and Folders with pathlib (Apprentice): A Path from Python's pathlib module holds a file's or folder's address in parts, so a script can join paths with /, read a file's name and extension, list and find the files in a folder, and make folders, the same way on Windows and on a Mac.
- Command-Line Options with argparse (Journeyman): The words typed after a script's name arrive in sys.argv, and the argparse module turns them into a positional argument, options with defaults and flags, checks them, and writes a --help page, so one script works on any folder without editing its code.
- A Report Script That Finds the Newest Data (Journeyman): A report script runs with one command instead of a person running notebook cells, finds the newest week's file with sorted and glob (which works because the file names start with the year), stacks every week into one table with concat, and takes its folder as an option through a main(argv) function, so running it again next week builds next week's report.
- Writing the Report: Charts and Tables on a Web Page (Journeyman): A report someone else reads is a web page, so the script saves each chart as a PNG with savefig and closes it, turns tables into HTML with to_html, builds the page with an f-string, and writes it into a folder of its own for each week, ready to open in any browser.
Exercise: Food Truck Weekly Report
- Exercise: Food Truck Weekly Report (Journeyman): Write the six functions of a food truck's weekly report script, which find the newest week's sales file, load it, sum up its headline numbers, chart the items sold, write the report as a web page in a folder for that week, and run it all from one command with argparse options, and let pytest check the numbers and the files it writes.