【问题标题】:HTML select options from a python list从 Python 列表中选择 HTML 选项
【发布时间】:2016-10-19 20:31:39
【问题描述】:

我正在编写一个 python cgi 脚本来设置一个 Hadoop 集群。 我想创建一个 HTML 选择下拉列表,其中的选项来自 python 列表。这可能吗?? 我环顾四周。找不到任何合适的答案。

这是我目前在另一个线程上发现的...

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

我真的需要一些帮助。请。谢谢。

【问题讨论】:

    标签: python html cgi


    【解决方案1】:

    您需要生成一个“选项”列表并将它们传递给您的 javascript 以制作列表

    values = {"A": "One", "B": "Two", "C": "Three"}
    options = []
    
    for value in sorted(values.keys()):
        options.append("<option value='" + value + "'>" + values[value] + "</option>")
    

    然后将“选项”注入到您的 html 中。说,在你的“template.html”中有一行:

    var options = $python_list;
    

    然后在你的 python 脚本结束时:

    ####open the html template file, read it into 'content'
    html_file = "template.html"
    f = open(html_file, 'r')
    content = f.read()
    f.close()
    
    ####replace the place holder with your python-generated list
    content = content.replace("$python_list", json.dumps(options))
    
    ####write content into the final html
    output_html_file = "index.html"
    f = open(output_html_file, 'w')
    f.write(temp_content)
    f.close()
    

    在您的“index.html”中,您应该在“var options = ...”之后有一行,用于获取列表并生成下拉列表。

    $('#my_dropdown').append(options.join("")).selectmenu();
    

    或者,我建议你使用你的 python 生成一个 json 文件(使用 json.dumps()),一个名为“config.json”的文件可能。并且您的 html javascript 文件应读取此 json 文件以呈现最终页面。所以在你的 json 文件中,应该有这样的内容:

    { ...“选项”:[“一”,“二”,“三”] ...}

    在您的 html &lt;script&gt; 部分,您可以读取选项值

    d3.json("config.json", function(data)) {
     var options = [];
     for (var i= 0; i < data.options.length; i++)
        {
          options.push("<option value='" + data.options[i] + "'>" + data.options[i] + "</option>");
    
        }
     $('#my_dropdown').append(options.join("")).selectmenu();
    }
    

    【讨论】:

    • 哇。好的。我需要一段时间才能理解这一切。我不知道 json 是如何工作的,所以我认为这对我来说不是一个可行的选择。但是非常感谢。
    • 嗯,为什么值被当作字典?
    • 我不确定这应该如何工作。因为到目前为止,我只有一个 .cgi 脚本,其中包含我所有的 html 代码以及所需的 python 代码。所以我不明白你为什么要打开一个文件然后把输出放到另一个文件中。对不起。我是 python 新手。
    • 你和你的cgi代码有什么样的交互? cgi 脚本应该在某个地方有它的 html,或者本身是硬编码的,或者它在某个地方获取一个模板并修改它并返回它。无论哪种方式,您都需要访问该 html 部分并对其进行调整,以便您的 python 代码可以交出选项。
    • cgi 代码和 html 都在一个文件中。我正在使用 print """ html """ 打印 html 部分。
    【解决方案2】:

    这是一种更简单的方法。

    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>
       {}
    </select>
    </body>
    </html>
    """.format(html_list)
    print(html)
    

    更多详情:http://python-forum.io/Thread-string-format-and-string-expressions

    【讨论】:

      【解决方案3】:

      这很可能是最简单的方法。只需在 HTML 中使用 Python 使用 FOR 循环更新您的选项...例如:

      这是一个选择列表,我用一个名为 myOptions 的数组填充它,我将它传递给表单。我还添加了一个 If 语句,用于检查列表中的某个值是否 = 到 myValue 并预先选择了该值。这完全是可选的。

       <select>
              {% for opt in myOptions %}
                  <option value={{ opt }} 
                     {% if myValue==opt %} 
                         selected="true"
                     {% endif %}>{{ opt }}
                  </option>
              {% endfor %}
       </select>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-02
        • 1970-01-01
        • 1970-01-01
        • 2021-03-14
        • 2014-06-20
        • 1970-01-01
        • 2020-03-21
        • 1970-01-01
        相关资源
        最近更新 更多