Python Forum
HTML select options from python list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HTML select options from python list
#1
I'm writing a python-cgi script to setup a Hadoop cluster.
I want to create an HTML select dropdown where the options are taken from a python list. Is this possible??
I've looked around a lot. Couldn't find any proper answer to this.
So far, I'm only using a single .cgi file which has both the html code as well as the python code in it.

I really need to find an answer to this. I'd appreciate any help at all.

Thanks.

The options i need are IP addresses. I have a function which stores the IP addresses into a python list.
Reply
#2
(Oct-20-2016, 12:07 AM)adi.6194 Wrote: I want to create an HTML select dropdown where the options are taken from a python list. Is this possible??
import cgitb
cgitb.enable()

import cgi
form = cgi.FieldStorage()
lister = ['a','b','c']

html_list = ''
for value in lister:
   html_list += '<option value={0}>{0}</option>'.format(value)

html = """Content-type: text/html\n

<html>
<head>
</head>
<body>
<select>
   {OPTIONS}
</select>
</body>
</html>
""".format(
       OPTIONS=html_list,
       )
print(html)
and of course instead of +=, you can append the option string to a list, and then join them at the end.
Recommended Tutorials:
Reply
#3
This is giving me an error OPTIONS undefined on the " OPTIONS=html_list " line

This is the error. (I'm using optionlist instead of html_list)

OPTIONS undefined, optionlist = '<option value=192.168.100.103>192.168.100.103</option>'

<type 'exceptions.KeyError'>: ' \n\t\t\tbackground'
args = (' \n\t\t\tbackground',)
message = ' \n\t\t\tbackground'
Reply
#4
(Oct-20-2016, 09:08 AM)adi.6194 Wrote: This is giving me an error OPTIONS undefined on the " OPTIONS=html_list " line
Too tired at the moment. But you dont really need the OPTIONS at all. This is by keyword, and you can use by position. http://python-forum.io/Thread-string-for...xpressions

'<select>
{}
</select>'.format(optionlist)
Recommended Tutorials:
Reply
#5
Wow. Thank you so much. This is amazing.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Populating list items to html code and create individualized html code files ChainyDaisy 0 1,560 Sep-21-2022, 07:18 PM
Last Post: ChainyDaisy
  Using yt-dlp options in Python Pavel_47 8 14,212 Jun-29-2022, 12:52 PM
Last Post: Pavel_47
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,529 Mar-14-2021, 12:23 PM
Last Post: rfeyer
  Pandas tuple list returning html string shansaran 0 1,664 Mar-23-2020, 08:44 PM
Last Post: shansaran
  Python3 + BeautifulSoup4 + lxml (HTML -> CSV) - How to loop to next HTML/new CSV Row BrandonKastning 0 2,328 Mar-22-2020, 06:10 AM
Last Post: BrandonKastning
  getting options from a html form pgoosen 5 3,193 Jul-03-2019, 06:07 PM
Last Post: nilamo
  How do I fetch values from db to Select Options using Flask? progShubham 2 17,637 Jul-25-2017, 05:52 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020