This post was inspired by Blog your projects! of Laura Langdon.

The cat facts list project

This project comes from an exercise I did for a technical test for a Python developer job opening. The expected outcome of the exercise was to print to console the HTML rendered from a template. The HTML will consist of an unordered list of cat facts obtained from the following API: Cat Fact API.

If you want to see the finished(?) product, you can go to my repo: GitHub - UlisesAlexanderAM/catfacts-jinja2.

My process

The exercise came with sub-tasks to be completed, so I decided to approach the problem in that way. The first task was to import the jinja2 module. For this task, I consulted the official documentation. I got this part of my code:

from jinja import Environment, FileSystemLoader, select_autoescape, Template

Before passing to the next step, I continue reading the documentation to familiarize myself with jinja. The next step was easier, I only did a simple import of the requests module.

Now I had to request the list of facts from the API, for this part I check the following resources: Python Requests Tutorial - GeeksforGeeks, Quickstart — Requests 2.28.1 documentation. To get the list of facts I use the method get from the module. Here is an example of how I did it originally:

list_catfacts = requests.get("https://catfact.ninja/facts")

With a list of cat facts, I move to learn how to make a template in jinja and check the jinja2 tutorial - Python Tutorial. Here is a snippet of the template:

Let's do a checklist of what I've done at this point.

  • [x] Import module requests and jinja2
  • [x] Get a list of cat facts
  • [x] Make a template that displays a list of cat facts
  • [ ] Pass the list of cat facts to the template
  • [ ] Print to console the HTML generated

With only two things on the checklist, I decided to refactor my code. These are the two primary reasons:

  1. What I get from the request is more than just cat facts
  2. To follow a functional programming approach

To tackle this, I reduced the number of facts obtained from the request. I code a function to get the cat facts, one to generate the template, and another to render the template. While I was doing the refactoring, I passed the list of cat facts to the template.

Now I just need to print to the console, this was solved by calling the print function. Once done this last step, my checklist was complete.

  • [x] Import module requests and jinja2
  • [x] Get a list of cat facts
  • [x] Make a template that displays a list of cat facts
  • [x] Pass the list of cat facts to the template
  • [x] Print to console the HTML generated

Here is the final product of my catfacts-jinja2 module:

What's next

My plan is to modify the project to stylize it with Tailwinds CSS, render the template in a file, and host it in Vercel or GH pages.

References

API — Jinja Documentation (3.1.x)

Python Requests Tutorial - GeeksforGeeks

Quickstart — Requests 2.28.1 documentation

Template Designer Documentation — Jinja Documentation (3.1.x)

jinja2 tutorial - Python Tutorial