-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsite_generator.py
More file actions
89 lines (69 loc) · 2.63 KB
/
website_generator.py
File metadata and controls
89 lines (69 loc) · 2.63 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from moviestorage import movie_storage_sql as storage
import subprocess
import threading
import os
import platform
import webbrowser
TITLE = "My Movie App"
MOVIES_PER_ROW = 5
output_path = os.path.join("static", "index.html")
template_path = os.path.join("static", "index_template.html")
def open_browser_non_blocking(path):
"""Opens a URL in the browser without blocking the main thread"""
def open_task():
try:
if platform.system() == 'Windows':
os.startfile(path)
elif platform.system() == 'Darwin': # macOS
subprocess.Popen(['open', path])
else: # Linux and others
subprocess.Popen(['xdg-open', path])
print("Opening in browser...")
except Exception as e:
print(f"Couldn't open browser: {e}")
# Start the browser in a separate thread
threading.Thread(target=open_task, daemon=True).start()
def write_html():
"""Writes the final HTML file"""
try:
with open(template_path, "r", encoding="utf-8") as obj:
template = obj.read()
html_content = template.replace("__TEMPLATE_TITLE__", TITLE)
html_content = html_content.replace(
"__TEMPLATE_MOVIE_GRID__",
movie_data_to_html()
)
with open(output_path, "w", encoding="utf-8") as obj:
obj.write(html_content)
print("Website successfully created")
# Get the absolute path to the HTML file
abs_path = os.path.abspath(output_path)
# Open in browser without blocking
open_browser_non_blocking(abs_path)
except Exception as e:
print(f"{e}\nCouldn't create html file")
def movie_data_to_html():
movies = storage.list_movies()
movie_info = ""
current_row = []
for i, (title, moviedetails) in enumerate(movies.items(), 1):
current_row.append(serialize_movies(title, moviedetails))
# start new row, if MOVIES_PER_ROW is reached
if i % MOVIES_PER_ROW == 0 or i == len(movies):
movie_info += (
'<div class="movie-row">' + ''.join(current_row) + '</div>'
)
current_row = []
return movie_info
def serialize_movies(title, moviedetails):
"""Creates HTML for each movie with fixed height containers"""
return (
'<div class="movie-container">'
f'<img class="movie-poster" src='
f'{moviedetails["url"]}" alt="{title}" loading="lazy">'
'<div class="movie-info">'
f'<div class="movie-title" title="{title}">{title}</div>'
f'<div class="movie-year">{moviedetails["year"]}</div>'
'</div>'
'</div>'
)