0

I'm trying to make a program that redirects the user to a HTML file after they have logged in. The redirecting part of the code works like this:

print("You have reached the end: Redirecting.")

url = ('test.html') # test.html is in the same folder as the program

webbrowser.open_new(url)

time.sleep(10)

Whenever I run the program, the HTML file opens, but when the program ends after ten seconds, the file exits. I've looked at the webbrowser documentation and tried many different variations, but I still can keep the file open after the program exits. Is there any command or anyway to keep the HTML file open?

(P.S. I'm using Visual Studio Code if that makes a difference.)

1
  • You need a way to block the thread. You can spawn a new process and have an input() statement at the end of the script as a way to do this in the background. Commented Jun 14, 2024 at 0:53

1 Answer 1

0

I'm on Mac, so I can only speak to this working on Mac, but I have included options for Windows and Linux.

One option is creating a subprocess to open the page independent of the script:

import subprocess
import time
import platform #You don't need this unless you also want to make this multi-platform

print("You have reached the end: Redirecting.")

url = 'test.html'

if platform.system() == 'Darwin': #Mac
    subprocess.Popen(['open', url])
elif platform.system() == 'Windows':
    subprocess.Popen(['start', url], shell=True)
elif platform.system() == 'Linux':
    subprocess.Popen(['xdg-open', url])

time.sleep(10)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.