Skip to content

Instantly share code, notes, and snippets.

@zed
Last active December 31, 2015 13:59
Show Gist options
  • Save zed/7996895 to your computer and use it in GitHub Desktop.
Save zed/7996895 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2.6
"""Fill textarea.
http://stackoverflow.com/q/20621751
"""
import cgi
text = u'the text <that> you want & "to" put into textarea \u263a'
html_safe_text = cgi.escape(text, quote=True) # < > & " -> &lt; &gt; &amp; &quot;
textarea_html = u'<textarea placeholder="{0}"></textarea>'.format(
html_safe_text)
textarea_html_filled = u'<textarea>{0}</textarea>'.format(
html_safe_text)
import tempfile
import webbrowser
import time
def open_in_browser(html):
"""like lxml.html.open_in_browser() but `html` is a bytestring."""
with tempfile.NamedTemporaryFile("wb", 0, suffix='.html') as file:
file.write(html)
webbrowser.open(file.name)
time.sleep(60) # give the browser a minute to open before
# deleting the file
open_in_browser((u'''<!doctype html>
<meta charset="utf-8">
<title>Fill textarea</title>
''' + textarea_html + textarea_html_filled).encode('utf-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment