-
Notifications
You must be signed in to change notification settings - Fork 0
/
animals_web_generator.py
74 lines (56 loc) · 2.08 KB
/
animals_web_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import json
import data_fetcher
def load_data(file_path):
'''Loads json file'''
with open(file_path, "r") as handle:
return json.load(handle)
def load_html(file_path):
'''Load html file'''
with open(file_path, "r") as handler:
html_data = handler.read()
return html_data
def save_data(filename, data):
'''save data to new file'''
with open(filename, "w") as handler:
handler.write(data)
def get_animal_facts(data):
'''Print data from animal dictionary'''
animal_data_string = ""
# Iterate animal dict extract data add with html tags to animal_data_string
for animal in data:
animal_data_string += '<li class="cards__item">\n'
try:
animal_data_string += f' <div class="card__title">{animal["name"]}</div>\n'
except KeyError:
pass
animal_data_string += ' <p class="card__text">\n'
try:
animal_data_string += f" <strong>Location:</strong> {animal['locations'][0]}<br/>\n"
except KeyError:
pass
try:
animal_data_string += f" <strong>Type:</strong> {animal['characteristics']['type']}<br/>\n"
except KeyError:
pass
try:
animal_data_string += f" <strong>Diet:</strong> {animal['characteristics']['diet']}<br/>\n"
except KeyError:
pass
animal_data_string += ' </p>\n'
animal_data_string += ' </li>\n'
animal_data_string += '</li>\n'
return animal_data_string
def main():
# Load all the data
#animals_json = load_data('animals_data.json')
animal_name = input("Please enter an animal: ")
data = data_fetcher.fetch_data(animal_name)
print(data)
animal_facts = get_animal_facts(data)
animals_html_file = load_html('animals_template.html')
# Replace somethin in animals_html_file
animals_html_file = animals_html_file.replace("__REPLACE_ANIMALS_INFO__", animal_facts)
# Save the file
save_data("animals.html", animals_html_file)
if __name__ == "__main__":
main()