Skip to content

Instantly share code, notes, and snippets.

@UlisesAlexanderAM
Created November 11, 2022 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UlisesAlexanderAM/1a295f4d428cf132b6206b76d90834f3 to your computer and use it in GitHub Desktop.
Save UlisesAlexanderAM/1a295f4d428cf132b6206b76d90834f3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from jinja2 import Environment, FileSystemLoader, select_autoescape, Template
import requests
def main():
list_catfacts_url: str = "https://catfact.ninja/facts?limit=10"
catfacts = get_facts(list_catfacts_url)
template = generate_template("templates", "catfacts.html")
print(render_template(catfacts, template))
def render_template(catfacts: list, template: Template) -> str:
return template.render(list=catfacts)
def generate_template(file_system: str, template: str) -> Template:
env = Environment(loader=FileSystemLoader(file_system), autoescape=select_autoescape())
template = env.get_template(template)
return template
def get_facts(url: str) -> list:
list_catfacts_url: str = url
request_catfacts = requests.get(url=list_catfacts_url)
catfacts_json = request_catfacts.json()
data_catfacts = catfacts_json["data"]
catfacts = []
for facts in data_catfacts:
catfacts.append(facts["fact"])
return catfacts
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment