1

Is there any convenient/standard way to generate html select menu using list variable? For example I have list variable elements=['aaa','zzz','sss'] And need to generate drop down select menu using this variable:

<select name="dropdown" >
<option value="aaa">aaa</option>
<option value="zzz"> zzz </option>
<option value="sss"> sss </option>
</select> <br />

In Perl for example I can use CGI module and just specify :

 popup_menu(-name=>'dropdown', -values=>['NULL',@elements])

Thank you in advance

Ruslan
  • 1,004
  • 12
  • 9
  • Maybe this would work http://stackoverflow.com/questions/1548474/python-html-generator – John Giotta Dec 28 '10 at 14:57
  • Lots of suggestions from this as well: http://stackoverflow.com/questions/521621/what-is-the-best-way-to-write-html-from-python It's mostly is suggestions for template engines, but it sounds like Genshi has a direct HTML generator. – mjhm Dec 28 '10 at 15:06

3 Answers3

1
def makeSelect(name,values):
    SEL = '<select name="{0}">\n{1}</select>\n'
    OPT = '<option value="{0}">{0}</option>\n'
    return SEL.format(name, ''.join(OPT.format(v) for v in values))
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
0

Expanding Hugh's answer a bit, someone might need a selected option:

def makeSelect(name, values, selectedValue=None):
  SEL = '<select name="{0}">\n{1}</select>\n'
  OPT = '<option value="{0}"{1}>{0}</option>\n'
  return SEL.format(name, ''.join(OPT.format(v, " SELECTED" if v==selectedValue else "") for v in values))
T .
  • 376
  • 3
  • 5
0

I'm not aware of a native markup generator, but this library looks promising.

markup.py

EDIT: It looks like it hasn't been worked on since 2007

John Giotta
  • 16,432
  • 7
  • 52
  • 82