首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将值从flask html获取到python

将值从flask html获取到python
EN

Stack Overflow用户
提问于 2018-07-04 20:53:14
回答 1查看 807关注 0票数 0

下面给出了我使用python创建的flask服务器的html代码。在这里,我只使用checkboxSelectCombo,在选中flask UI中的复选框之后,我想在单击任何flask按钮时用python打印这些值。如何在python中获取和打印这些值?如果有人知道答案,请帮帮我。

谢谢

代码语言:javascript
复制
    <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>OPTIMIZATION</title>

       <title></title>
    <!-- Ignite UI Required Combined CSS Files -->
    <link href="http://cdn-na.infragistics.com/igniteui/2018.1/latest/css/themes/infragistics/infragistics.theme.css" rel="stylesheet" />
    <link href="http://cdn-na.infragistics.com/igniteui/2018.1/latest/css/structure/infragistics.css" rel="stylesheet" />

    <script src="http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.8.3.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="http://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>

    <!-- Ignite UI Required Combined JavaScript Files -->
    <script src="http://cdn-na.infragistics.com/igniteui/2018.1/latest/js/infragistics.core.js"></script>
    <script src="http://cdn-na.infragistics.com/igniteui/2018.1/latest/js/infragistics.lob.js"></script>
</head>

<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    background-color: #dddddd;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 1px;
}
</style>


<body  bgcolor="#a9d284">

<img src="/static/images/logo.gif" alt="Related Book 1" width="300" height="80" align="right">
<script src="/static/scripts.js" charset="utf-8"></script>

    <h1>PORTFOLIO OPTIMIZATION</h1>

    <style>
        .combo-label {margin-bottom:.5em;}
    </style>


<form method="POST" action="">

        <h4 class="combo-label">SELECT THE STOCK OPTIONS</h4>
    <div id="checkboxSelectCombo"></div>

    <br><br>

    <h4>SELECT THE INPUT DATE</h4>
        <input type="date" name="date1" placeholder="Start date">
        <input type="date" name="date2" placeholder="End date">

    <br><br>

<h4>ENTER CONSTRAINTS CRITERIA</h4>
<table>
  <tr>
    <th>    </th>
    <th>Cheap</th>
    <th>Moderate</th>
    <th>Expensive</th>
    <th>PUT</th>
    <th>CALL</th>
    <th>Not Recommended</th>
    <th>Highly Liquid</th>
    <th>Moderately Liquid</th>
    <th>Less Liquid</th>
    <th>MPO</th>
    <th>PO</th>
    <th>LPO</th>
  </tr>

  <tr>
    <td>Group_Min</td>
     <td class="td"><input type="text" name="min1"/></td>
        <td class="td"><input type="text" name="min2" /></td>
        <td class="td"><input type="text" name="min3"/></td>
        <td class="td"><input type="text" name="min4"/></td>
        <td class="td"><input type="text" name="min5"/></td>
        <td class="td"><input type="text" name="min6"/></td>
        <td class="td"><input type="text" name="min7"/></td>
        <td class="td"><input type="text" name="min8"/></td>
        <td class="td"><input type="text" name="min9"/></td>
        <td class="td"><input type="text" name="min10"/></td>
        <td class="td"><input type="text" name="min11"/></td>
        <td class="td"><input type="text" name="min12"/></td>
  </tr>

  <tr>
    <td>Group_Max</td>
      <td class="td"><input type="text" name="max1"/></td>
        <td class="td"><input type="text" name="max2"/></td>
        <td class="td"><input type="text" name="max3"/></td>
        <td class="td"><input type="text" name="max4"/></td>
        <td class="td"><input type="text" name="max5"/></td>
        <td class="td"><input type="text" name="max6"/></td>
        <td class="td"><input type="text" name="max7"/></td>
        <td class="td"><input type="text" name="max8"/></td>
        <td class="td"><input type="text" name="max9"/></td>
        <td class="td"><input type="text" name="max10"/></td>
        <td class="td"><input type="text" name="max11"/></td>
        <td class="td"><input type="text" name="max12"/></td>
  </tr>
</table>

    <br><br>
     <input type="submit" value="GET RESULTS">
</form>


    <script>

        var colors = [
            { Name: "FP FP Equity" },
            { Name: "SAN FP Equity" },
            { Name: "NOK1V FH Equity" },
            { Name: "SAN SQ Equity" },
            { Name: "EOA GY Equity" },
            { Name: "SIE GY Equity" },
            { Name: "ENI IM Equity" },
            { Name: "UC IM Equity" },
            { Name: "INGA NA Equity" },
            { Name: "TEF SQ Equity" },
            { Name: "BNP FP Equity" },
            { Name: "BBVA SQ Equity" },
            { Name: "ALV GY Equity" }

        ];

        $(function () {

            $("#singleSelectCombo").igCombo({
                width: 300,
                dataSource: colors,
                textKey: "Name",
                valueKey: "Name",
                dropDownOnFocus: true,
                dropDownOrientation: "bottom"
            });

            $("#multiSelectCombo").igCombo({
                width: 300,
                dataSource: colors,
                textKey: "Name",
                valueKey: "Name",
                multiSelection: {
                    enabled: true
                },
                dropDownOrientation: "bottom"
            });

            $("#checkboxSelectCombo").igCombo({
                width: 300,
                dataSource: colors,
                textKey: "Name",
                valueKey: "Name",
                multiSelection: {
                    enabled: true,
                    showCheckboxes: true
                },
                dropDownOrientation: "bottom"
            });

        });

    </script>

</body>
</html>



<!doctype html>
<title>Simple tables</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
<div class=page>
  <h2>OPTIMIZED VALUES</h2>
  {% for table in tables %}
    <h3>{{titles[loop.index]}}</h3>
    {{ table|safe }}
  {% endfor %}
</div>

Python后端代码

代码语言:javascript
复制
from flask import Flask, render_template, request
from optimization_excel.app import rough_bkend as r1

app = Flask(__name__)


@app.route('/opt')
def index():
    return render_template('rough_portopt.html')


@app.route('/opt', methods=['POST'])
def opt():

    val = request.form.getlist('checkboxSelectCombo')
    print(val)

    res1, res2, res3 = r1.get_res1()
    return render_template('rough_portopt.html', tables=[res1.to_html(), res2.to_html()],
                           titles=['na', 'Test Results', 'Constraint values'])

if __name__ == "__main__":
    app.run(debug=True)
EN

回答 1

Stack Overflow用户

发布于 2018-07-06 04:23:42

因此,我将import pdb; pdb.set_trace()放在print(val)代码之前,并发现

代码语言:javascript
复制
-> print(val)
(Pdb) p request.form
ImmutableMultiDict([('date1', ''), ('date2', ''), ('min1', ''), ('min2', ''),
('min3', ''), ('min4', ''), ('min5', ''), ('min6', ''), ('min7', ''), ('min8', ''),
('min9', ''), ('min10', ''), ('min11', ''), ('min12', ''), ('max1', ''), ('max2', ''),
('max3', ''), ('max4', ''), ('max5', ''),('max6', ''), ('max7', ''), ('max8', ''),
('max9', ''), ('max10', ''), ('max11', ''), ('max12', '')])

所以我们看到checkboxSelectCombo不是表单的一部分。右键单击下拉框并选择Inspect,检查生成的HTML并找到

代码语言:javascript
复制
<input type="hidden" class="ui-igcombo-hidden-field" value="SAN FP Equity">

啊,这个元素没有名字。您的问题似乎出在您的javascript中,而不是flask。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51174199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档