Convert loop into function to be used for basic flask web application

Hello! This is my first time on the forum and I am pretty new to python. After learning all of the basics, I have created a program that helps give me a visual countdown of my school periods. I want to make this into a basic web application using flask, but I do not know how I should go about doing that. While looking up flask tutorials it seems that I have to make my program into a function somehow so that I can see the countdown on the web application. How can I do this? Help would be much appreciated!

My current program:

import time

normal_school_time_table = (59, 5, 59, 7, 59, 5, 59, 30, 5, 59, 5, 59)
thursday_school_time_table = (47, 5, 47, 7, 47, 5, 47, 30, 5, 47, 5, 47)
normal_days = ["Monday", "Tuesday", "Wednesday", "Friday"]
short_day = "Thursday"

try:
    while True:
        week_day = time.strftime("%A", time.localtime())
        current_time = time.strftime("%H:%M", time.localtime())
        if week_day in normal_days and current_time == "8:30":
            print("It is a normal week day!")
            for period in normal_school_time_table:
                if period < 10:
                    print(f'0{period}:00')
                else:
                    print(f'{period}:00')
                time.sleep(1)
                for minute in range(period - 1, -1, -1):
                    for second in range(59, -1, -1):
                        if second <= 9 < minute:
                            print(f'{minute}:0{second}')
                        elif second <= 9 and minute <= 9:
                            print(f'0{minute}:0{second}')
                        elif second > 9 >= minute:
                            print(f'0{minute}:{second}')
                        elif second > 9 and minute > 9:
                            print(f'{minute}:{second}')
                        time.sleep(1)
        elif week_day == short_day and current_time == "8:30":
            print("It is Thursday, a short day!")
            for period in thursday_school_time_table:
                if period < 10:
                    print(f'0{period}:00')
                else:
                    print(f'{period}:00')
                time.sleep(1)
                for minute in range(period - 1, -1, -1):
                    for second in range(59, -1, -1):
                        if second <= 9 < minute:
                            print(f'{minute}:0{second}')
                        elif second <= 9 and minute <= 9:
                            print(f'0{minute}:0{second}')
                        elif second > 9 >= minute:
                            print(f'0{minute}:{second}')
                        elif second > 9 and minute > 9:
                            print(f'{minute}:{second}')
                        time.sleep(1)
except KeyboardInterrupt:
    print("Program stopped manually")

There is quite a lot of refactoring that you can do here. For one, you can replace all of this

with just

print(f"{minute:02d}:{second:02d}")

which will automatically format it to 2 digits with 0 in front

second, you can take the remaning loop and put it in it’s own function like this

def countdown(minutes):
    for minute in range(minutes, -1, -1):
        for second in range(59, -1, -1):
            time.sleep(1)
            yield minute, second

which will return you a minute and a second in a tuple and it will decrement every time you use it. Once you do this, you can remove your main loop into another function and add changes just made to it.

After that, you will need to wrap it in flask template.

Also, you probably want to do something like

if not short day:
    timetable = normal_time_table
else:
    timetable = short_time_table

and just write the rest of the code using timetable variable instead of writing some long branches. This will also remove repeated logic.

1 Like