Created
November 11, 2022 22:05
-
-
Save UlisesAlexanderAM/1a295f4d428cf132b6206b76d90834f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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