Open a web browser with Python's webbrowser module


Python tip:

Python's standard library includes a webbrowser module that allows you to open a web-based document in a browser.

👇

import webbrowser
import tempfile

html_page="""
<html>
<head>
<title>Testdriven.io</title>
</head>
<body>
I'm going to open in a browser.
</body>
</html>
"""

with tempfile.NamedTemporaryFile('w', delete=False, suffix='.html') as f:
    url = 'file://' + f.name
    f.write(html_page)

webbrowser.open(url)