requirements.txt
is a common name for a Python requirements file. This is a specially formatted list of packages that are required for software to run correctly.
Some of our notebooks, like Met AP.ipynb, define their requirements right in the notebook using Margo.
We can extract those values with a command line tool called margo-tool
. Since we can run shell commands from inside a notebook using !
, we'll demonstrate this approach inside a notebook. You can do this on a command line if you prefer.
# Display help for `margo-tool extract` subcommand
!margo-tool extract --help
usage: margo-tool extract [-h] -i NOTEBOOK_FILE [-f DECLARATION_FORMAT] [-p PROPERTY] optional arguments: -h, --help show this help message and exit -i NOTEBOOK_FILE, --input NOTEBOOK_FILE -f DECLARATION_FORMAT, --format DECLARATION_FORMAT -p PROPERTY, --property PROPERTY The property to extract
!echo "# Met API.ipynb" > requirements.txt
!margo-tool extract -i "Met API.ipynb" -p requirements.txt -f raw >> requirements.txt
We can look at the contents of the file with the cat command:
! cat requirements.txt
# Met API.ipynb matplotlib==3.3.4 requests==2.25.1
Now we can do this for the rest of the .ipynb
in this repository.
!echo "\n# Intro to Margo Loader.ipynb" >> requirements.txt
!margo-tool extract -i "Importing Notebooks.ipynb" -p requirements.txt -f raw >> requirements.txt
! cat requirements.txt
# Met API.ipynb matplotlib==3.3.4 requests==2.25.1 # Intro to Margo Loader.ipynb margo-loader
# Preving this notebook from being imported
# :: not-a-module ::
So far we've learned how to use Margo Loader to import notebooks; how to annotate notebooks with Margo notes so they are more usful to Margo Loader; and how to use Margo notes with a different application, margo-tool
, to integrate notebooks with pip.
The property we assigned in other notebooks and extract in this notebook was called requirements.txt
, but you can use any name and margo-tool
can extract it. You can even extract data stored as JSON or YAML.
To review the Margo "keywords" we've learned so far, checko out Margo keyword reference.ipynb.