Launch HTML code in browser (that is generated by BeautifulSoup) straight from Python
Using webbrowser.open
:
import os
import webbrowser
html = '<html> ... generated html string ...</html>'
path = os.path.abspath('temp.html')
url = 'file://' + path
with open(path, 'w') as f:
f.write(html)
webbrowser.open(url)
Alternative using NamedTemporaryFile
(to make the file eventually deleted by OS):
import tempfile
import webbrowser
html = '<html> ... generated html string ...</html>'
with tempfile.NamedTemporaryFile('w', delete=False, suffix='.html') as f:
url = 'file://' + f.name
f.write(html)
webbrowser.open(url)