小能豆

Python flask for 循环没有打印所有输出?

py

我这里有一个像这样的数据集:

df=    
    A            B            C
    house        50           50
    money        80           50
    bed          20           60 

这是我的代码:

@app.route('/index', methods=['POST'])
def index():
    if request.method == 'POST'
        inpt = request.form['inpt']

        for text in df['A']:
            if text == inpt:
                give_text = df[df['A'] == inpt]
                give_text = give_text.drop(['A'], axis =1)

        for column in give_text:
            column_edit = give_text[column]
            column_edit = column_edit * 5

            return render_template('index.html', numbers = column_edit)

如果这是我的代码,情况会如何变化:

@app.route('/index', methods=['POST'])
def index():
    if request.method == 'POST'
        inpt = request.form['inpt']

        for text in df['A']:
            if text == inpt:
                give_text = df[df['A'] == inpt]
                give_text = give_text.drop(['A'], axis =1)

        for column in give_text:
            column_edit = give_text[column]
            column_edit = column_edit * 5

            if ".." in column_edit:     #nvm which symbol
                column_edit = column_edit + "meters"
                return render_template('index.html', numbers = column_edit)

我的 HTML 文件中的代码如下:

<form> action ='/index' method = 'post'>
    <p>< input type = 'text' name='inpt'/> </p>
    <p>< input type = 'submit' name='submit'/> </p>


    <h3>{{numbers}}</h3>
</form>

但是为什么他总是在我的网站上只打印我的循环的第一个答案?

如果我在输入中写‘house ‘ ,我想像这样在我的网站上打印整个计算数据集:

output:
    250
    250



阅读 20

收藏
2024-12-13

共1个答案

小能豆

如果numbers是可迭代对象,则可能无法像之前那样将其输出到 html 中。您可以这样做:

Python-Flask

@app.route('/index', methods=['POST'])
def index():
    if request.method == 'POST'
        inpt = request.form['inpt']

        for text in df['A']:
            if text == inpt:
                give_text = df[df['A'] == inpt]
                give_text = give_text.drop(['A'], axis =1)
        my_data = []
        for column in give_text:
            column_edit = give_text[column]
            column_edit = column_edit * 5
            my_data.append(column_edit)

        length = list(range(0, len(my_data))) #create a list for length of items in  column_edit, this list will server as indicies for iteration through elements of column_edit
     return render_template('index.html', numbers = my_data, length=length)

HTML

<form> action ='/index' method = 'post'>
<p>< input type = 'text' name='inpt'/> </p>
<p>< input type = 'submit' name='submit'/> </p>
{% for i in blenr %}
<h3>{{numbers[i]}}</h3>
{% endfor %}
2024-12-13