Created
February 21, 2015 09:19
-
-
Save dufferzafar/4c1f414ce9ad31e82c5c to your computer and use it in GitHub Desktop.
Python function to view html file in a browser.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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