save the print statement to a separate html file instead of stdout
sys.stdout = open(index_html_file, "w")
could be problematic down the line. Anything you import that uses print or otherwise writes to sys.stdout will instead write to that file. So you could end up with debugging messages in your HTML document. It's generally safer just to pass file=... to print each time, or to save keystrokes you can define a helper function like
html_file = open(index_html_file, "w")
def output(*args, **kwargs):
print(*args, file=html_file, **kwargs)