Skip to content

Instantly share code, notes, and snippets.

@dufferzafar
Created February 21, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dufferzafar/4c1f414ce9ad31e82c5c to your computer and use it in GitHub Desktop.
Save dufferzafar/4c1f414ce9ad31e82c5c to your computer and use it in GitHub Desktop.
Python function to view html file in a browser.
def view(html, rm=False):
"""View HTML in a browser."""
# So many imports :O
import os
import webbrowser
import string
import random
# Remove preview random files
if rm:
for f in os.listdir('/tmp'):
if f.startswith('py_view'):
os.remove(os.path.join('/tmp', f))
# Generate a new file
rand = ''.join(random.choice(string.lowercase) for i in range(10))
temp = '/tmp/py_view.%s.html' % rand
with open(temp, 'w') as t:
t.write(html)
# View
webbrowser.open('file://' + temp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment