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

2

3 Answers 3

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))
Sign up to request clarification or add additional context in comments.

2 Comments

Simple and efficient. Thank you, exactly what I need.
Glad to help. Please feel free to tag it as the solution! ;-)
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

Comments

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.