{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Generate requirements.txt\n", "\n", "`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.\n", "\n", "Some of our notebooks, like [Met AP.ipynb](./Met%20API.ipynb), define their requirements right in the notebook using Margo.\n", "\n", "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.\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "usage: margo-tool extract [-h] -i NOTEBOOK_FILE [-f DECLARATION_FORMAT]\r\n", " [-p PROPERTY]\r\n", "\r\n", "optional arguments:\r\n", " -h, --help show this help message and exit\r\n", " -i NOTEBOOK_FILE, --input NOTEBOOK_FILE\r\n", " -f DECLARATION_FORMAT, --format DECLARATION_FORMAT\r\n", " -p PROPERTY, --property PROPERTY\r\n", " The property to extract\r\n" ] } ], "source": [ "# Display help for `margo-tool extract` subcommand\n", "\n", "!margo-tool extract --help" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "!echo \"# Met API.ipynb\" > requirements.txt\n", "!margo-tool extract -i \"Met API.ipynb\" -p requirements.txt -f raw >> requirements.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can look at the contents of the file with the cat command:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# Met API.ipynb\r\n", "matplotlib==3.3.4\r\n", "requests==2.25.1\r\n" ] } ], "source": [ "! cat requirements.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can do this for the rest of the `.ipynb` in this repository." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "!echo \"\\n# Intro to Margo Loader.ipynb\" >> requirements.txt\n", "!margo-tool extract -i \"Importing Notebooks.ipynb\" -p requirements.txt -f raw >> requirements.txt" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# Met API.ipynb\r\n", "matplotlib==3.3.4\r\n", "requests==2.25.1\r\n", "\r\n", "# Intro to Margo Loader.ipynb\r\n", "margo-loader\r\n" ] } ], "source": [ "! cat requirements.txt" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "# Preving this notebook from being imported\n", "# :: not-a-module ::" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Next lesson\n", "\n", "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.\n", "\n", "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.\n", "\n", "To review the Margo \"keywords\" we've learned so far, checko out [Margo keyword reference.ipynb](./Margo%20keyword%20reference.ipynb)." ] } ], "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.7.9" } }, "nbformat": 4, "nbformat_minor": 4 }