“Notebook Translation” Exercise
Prompt
You have been writing some code in Google Colab to investigate your ability to fetch and display data from an API. Now your goal is to translate the notebook content into a local development repository.
Instructions
Review the following notebooks:
Open them in Google Colab, complete the setup instructions, and demonstrate your ability to run them.
To run either of the first two notebooks, you’ll first need to obtain an API Key from the AlphaVantage API.
To run the email sending notebook, you’ll first need to setup an account with either SendGrid or Mailgun, and obtain corresponding API keys. If you want to focus on the first two notebooks and return to the email notebook later, that would be fine.
Walk-through
Part I - Financial Reports
These challenges pertain to translating the Unemployment Report and Stocks Report notebooks.
Commit 1: Setup a Repo
- Create a new repo on GitHub. Include a “README.md” file and a python-flavored “.gitignore” file.
- Clone the repo locally, navigate there from the command line, and open the repo with a text editor.
Commit 2: Run an Example Python Script
- Create a new subdirectory in the repo called “app” and place inside a file called “example.py”.
- In the “example.py” file, write an example print statement, and save.
- Using the Anaconda base environment, run the “example.py” file from the command-line, to see that it works. Add the corresponding run command to the “README.md” file.
Commit 3 and Commit 4: Translate the Unemployment Report Notebook
- Create a new file in the “app” directory called “unemployment_report.py”, and copy the code from the Unemployment Report Notebook into the “unemployment_report.py” file.
- Translate notebook secrets to environment variables, setup a “.env” file, store the environment variables in the “.env” file, and update the “README.md” file with instructions on how to obtain these credentials and how to setup and update the “.env” file.
- Virtual Environment and Package Installations:
- Notice we need to install some packages, so add those to a new “requirements.txt” file in the repo’s root directory, and save.
- Create and activate a new Anaconda virtual environment. Add the corresponding virtual environment setup commands into the “README.md” file.
- After activating the virtual environment, install required packages. Add the corresponding package installation command to the “README.md” file.
- Run the “unemployment_report.py” file to see it works.
Commit 5: Translate the Stocks Report Notebook
- Create a new file in the “app” directory called “stocks_report.py”, and copy the code from the Stocks Report Notebook into the “stocks_report.py” file.
- Update the code relating to the API Key to use environment variables from the “.env” file, instead of asking for notebook secrets.
- Run the “stocks_report.py” file to see it works.
Commit 6: Refactor Duplicate Code
- Look for any code duplicated across the stocks and unemployment file. Observe the code relating to reading the environment variable is duplicated.
- Move the environment variable reading code to its own file, and import that functionality back into the other files.
Part II - Email Reports
These challenges pertain to translating the Email Sending notebook.
Send Example Email
- Choose one of the email sending services (SendGrid or Mailgun) - whichever one works for you and you were able to get set up.
- Create a new file in the “app” directory called “email_service.py”, and copy the code from the Email Sending Notebook pertaining to your chosen email sending service.
- Translate notebook secrets to environment variables, store the environment variables in the “.env” file, and update the “README.md” file with instructions on how to obtain these credentials and how to update the “.env” file.
- Run the “email_service.py” file to see it works.
Send Reports via Email
- Refactor the “email_service.py” file such that the email sending functionality exists within a custom function that accepts parameters for the sender address, recipient address, subject, and email contents. Also use a “main conditional” in this file to allow us to import code from it. Run the refactored file to see it stills sends the example email.
- Import the email sending function from the email service into the “unemployment_report.py” file. Use the unemployment data to compile some content to send in an email. Run the unemployment report to send it via email.
- Import the email sending function from the email service into the “stocks_report.py” file. Use the stock data to compile some content to send in an email. Run the stocks report to send it via email.