Explorer pHAT - Scripting Help!

I’ve got an explorer pHAT which I’m using to teach some basic programming and linux to my step daughter and her cousins. The ultimate goal is to show them how they can turn lights on and off using a web page. So far I’ve only managed to work out that you can execute a script with the explorerhat library imported, however the lights do not stay on when the script terminates itself and cleans up.

I’d like to write something whereby i can control the lights on and off on the web browser and manage my own cleanups with a delay (e.g. 5 mins of no interaction then clean up and turn everything off)

I’m currently using a python CGI webserver with a separate CGI-Bin directory where i have 4 scripts calling the explorerhat.output.one/two/three/four.toggle() with a time.sleep(5) set afterwards. I’d like to have a script which runs as soon as the web page loads whereby when i press buttons on the page it triggers the lights on and off, and if its inactive for 5 minutes it closes the script down and shows a page saying please refresh to continue. Can anyone please assist as i’m quite stuck in getting the scripts to behave as I’d like and I know the way i’m doing it currently is not ideal.