0

I have an sql database where every row contains paragraphs of text. I want to display each row of text in it's own html textarea. I'm using python to generate the html page. I've tried the following but the body of the textarea is always empty. Does anyone know what I can do to fix this?

# didn't work
# dict = {'example':results[i]["Teaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;"), 'arg':results[i]["GImageID"]}
# s = "<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra{arg}\">maybe1 - {example}</textarea>".format(**dict)

# gives an error on the d even though a tutorial said I should do it this way
# s = "<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra{arg:d}\">maybe2 - {example:d}</textarea>".format(**dict)

#extra += s

#------------------------------------------------------
teaser = results[i]["Teaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;")


# didn't work - empty
# extra += "<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra{arg}\">maybe3 - {example}</textarea>".format(example=teaser, arg=results[i]["GImageID"])

# didn't work - empty
# extra += "<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra{arg}\">maybe3.5 - {example}</textarea>".format(example=results[i]["GImageTeaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;"), arg=results[i]["GImageID"])

# didn't work - empty
# t = Template("<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra$arg\">maybe4 - $example</textarea>")
# s = t.substitute({ 'example': results[i]["Teaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;"), 'arg': results[i]["GImageID"]})
# extra += s

# didn't work - empty
# html = Template("<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra$arg\">maybe5 - $example</textarea>")
# result = html.safe_substitute(example=teaser,arg='Vishnu')

# result = html.safe_substitute(example=results[i]["Teaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;"),arg='Vishnu')
# extra += result

# didn't work - empty
# extra += """<br><textarea class=\"\" rows=\"2\" cols=\"20\" id=\"extra%d\">maybe6 - %s</textarea>""" % (results[i]["GImageID"], results[i]["Teaser"].replace("\"", "\\\"").replace("<", "&lt;").replace(">", "&gt;"))

The text above is in a for loop

for i in range(len(results)):

and I've outputted the text results[i]["Teaser"] to verify that's it's not empty. The GImageID prints out fine, but the Teaser text that contains the paragraphs is always empty.

If you need any additional information please let me know. I've been stuck for hours, and I can't find any working solution. Thanks!

josealvarado111
  • 565
  • 1
  • 9
  • 24
  • Have you tried to write `textarea` by hand and see whether it works? [How can I “pre-fill” the value of a textarea in an HTML form?](http://stackoverflow.com/q/2231936/4279) – jfs Dec 16 '13 at 22:34
  • Yeah, I went to w3schools.com made a text area and filled in the body of the text with the output. – josealvarado111 Dec 16 '13 at 23:20

1 Answers1

0
import cgi

text = results[i]["Teaser"] # the text that you want to put into textarea
html_safe_text = cgi.escape(text) # < > & -> &lt; &gt; &amp;
textarea_html = u"<textarea>{}</textarea>".format(html_safe_text)

You can open it in a browser for debugging:

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).encode('utf-8'))

Here's a standalone fill-textarea.py script that you can try.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • I modified one line of code textarea_html = u"".format(html_safe_text) since I'm using 2.6 but the text areas being generated are still empty. Is there simply no way of doing this? – josealvarado111 Dec 16 '13 at 23:25
  • @user2492566: it means the error is in different place e.g., `len(results)` could be zero. – jfs Dec 16 '13 at 23:27
  • @user2492566: I've added [complete code example. Try it.](https://gist.github.com/zed/7996895) – jfs Dec 16 '13 at 23:33
  • Thanks for the example. Your previous code was correct. I simply uploaded the wrong version of my code to the server. You just saved me a lot of time. Thanks a lot! – josealvarado111 Dec 16 '13 at 23:56