import json import datetime # Load JSON data from a file def load_json(file_path): with open(file_path, 'r', encoding='utf-8') as json_file: return json.load(json_file) def get_list_of_last_names(data): processed_data = [] for item in data: name_split = item.get('Name').split(" ") name_split_len = len(name_split) name = name_split[-1] if name not in processed_data and name_split_len > 1: processed_data.append(name) processed_data.sort() return processed_data def get_people_by_lastname(data, lastname): processed_data = [] for item in data: name_split = item.get('Name').split(" ") last_name = name_split[-1] name_split_len = len(name_split) if name_split_len == 1 and lastname == '[empty]': processed_data.append(item) if last_name == lastname: processed_data.append(item) return processed_data def generate_html(data, output_file): last_names = get_list_of_last_names(data) # last_names.insert(0, '[empty]') html_content = '' for lastname in last_names: html_content += '

'+ lastname +'

' people = get_people_by_lastname(data, lastname) for person in people: name_split = person.get('Name').split(" ") first_and_middle_names = " ".join(name_split[:-1]) last_name = name_split[-1] birthdate = datetime.datetime.strptime(person.get('Birthday'), "%Y-%m-%dT%H:%M:%S") dieddate = datetime.datetime.strptime(person.get('DiedDay'), "%Y-%m-%dT%H:%M:%S") if birthdate.year == 1 and dieddate.year == 1: html_content += '

' + last_name + ', ' + first_and_middle_names + '

' else: html_content += '

' + last_name + ', ' + first_and_middle_names + ' (' + (str(birthdate.year) if birthdate.year != 1 else '?') + ' - ' + (str(dieddate.year) if dieddate.year != 1 else '?') + ')

' html_content += '

This list contains ' + str(len(data)) + ' people

' html_content += '

Last updated ' + datetime.datetime.now().strftime('%d-%m-%Y') + '

' html_template = '' with open('templates/names_template.html', 'r') as file: html_template = file.read() html_template = html_template.replace('{{NAME_LIST}}', html_content) # Write the HTML content to a file with open(output_file, 'w', encoding='utf-8') as f: f.write(html_template) def main(): json_file_path = 'list.json' output_html_file = 'names.html' # Load JSON data directly into an array json_data = load_json(json_file_path) # Generate HTML from the loaded data generate_html(json_data, output_html_file) if __name__ == '__main__': main()