Snippets
We use snippets to reduce redundancy and improve maintainability. Snippets are references from the many Jupyter notebook files throughout our labs. They work by using Python's %load magic command.
Loaded Snippets
Jupyter notebooks will have Python code blocks similar to this one:
# %load ../../shared/snippets/openai-api-requests.py
import time
from openai import AzureOpenAI
...
What you see here is an executed load as the command is now commented out after the code has been loaded immediately below it.
While this is generally very maintable, the loading is static, unlike a function reference, for example. That means that code may be stale. When you are editing Jupyter notebook files, it is advisable that you execute the load commands fresh and check for git changes.
Updating a Loaded Snippet
- In a Jupyter Notebook's Python code cell, remove everything below the
%loadcommand in line 1. -
Uncomment the
%loadcommand. The cell should then look similar to this:%load ../../shared/snippets/openai-api-requests.py -
Press the Play button in the code cell to execute the
%loadcommand. - Repeate these steps for all code blocks with
%loadcommands in the current file. - Save the file.
- Check git for any changes to the file.
- Test the changes.
- Check in the changes.