Python Forum
HTML select options from python list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: HTML select options from python list (/thread-566.html)



HTML select options from python list - adi.6194 - Oct-20-2016

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.


RE: HTML select options from python list - metulburr - Oct-20-2016

(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.


RE: HTML select options from python list - adi.6194 - Oct-20-2016

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'


RE: HTML select options from python list - metulburr - Oct-20-2016

(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-format-and-string-expressions

'<select>
{}
</select>'.format(optionlist)



RE: HTML select options from python list - adi.6194 - Oct-20-2016

Wow. Thank you so much. This is amazing.