Skip to content

Instantly share code, notes, and snippets.

@sohang3112
Last active November 1, 2023 10:34
Show Gist options
  • Save sohang3112/adcc6daf7c3eb858c09a54573e965e11 to your computer and use it in GitHub Desktop.
Save sohang3112/adcc6daf7c3eb858c09a54573e965e11 to your computer and use it in GitHub Desktop.
Render an HTML string in the default web browser
import webbrowser
from tempfile import NamedTemporaryFile
def view_html(html: bytes, **kwargs) -> bool:
"""
Open HTML input string as a webpage in default browser
@return - True if page opened successfully, False otherwise
"""
with NamedTemporaryFile(suffix='.html', delete=False, **kwargs) as f:
f.write(html)
return webbrowser.open(f.name)
@sohang3112
Copy link
Author

Note: In a Jupyter Notebook, HTML can be displayed inline directly using IPython.display:

from IPython.display import display, HTML
display(HTML(an_html_string))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment