如何在同一页面上的文本字段上发布结果?

时间:2019-05-26 02:07:48

标签: python python-3.x flask flask-wtforms

我从左侧文本字段框中获取输入值并执行操作。我得到的结果将仅打印在终端上。因此,结果将打印在同一页的右侧文本框上。 我是flask / html的新手。我可以在tkinter上做到这一点。帮助我如何实现这一目标。

我包括了我编写的代码,您还可以运行该代码,查看页面和结果的外观以及我要获取的内容。

from flask import Flask, render_template, request, flash
from flask_wtf import Form
from wtforms import RadioField, SubmitField, StringField, TextAreaField
import os, re

app = Flask(__name__)
app.config.from_object(__name__)
app.secret_key = 'password'

def print_1():
     form=SimpleForm()
     return str(form.content.data)
     ##inputdata1 = form.content.data

def buffer_data():
    var1 = print_1()
    with open("Bufferfile.txt", 'w', encoding='utf-8') as infile:
        for line in var1:
            textdata1 = infile.write(line.strip('\n'))
            #print(textdata1)

def ping_1():
    form = SimpleForm()
    var2 = buffer_data()
    with open("Bufferfile.txt", 'r', encoding='utf-8') as infile1:
        for row in infile1.read().split():
            print(row)
            match1 = re.search("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:/\d{1,2}|)", row)
            if match1:
                ip = match1.group(0)
                x = os.system(form.example1.data + ip)
                if x == 0:
                    return (row, "Active")
                    print(row, "Active" + "\n")
                else:
                    print(row, "Inactive" + "\n")
    print("Read!")

class SimpleForm(Form):
    example1 = RadioField('Label', choices=[('ping -n 1 ', 'Ping'), ('nslookup ', 'DNSResolve')])
    content = TextAreaField('content')
    content1 = TextAreaField('content1')

@app.route('/', methods=['post', 'get'])
def hello_world():
    form = SimpleForm()
    if form.validate_on_submit():
        ping_1()
    else:
        print(form.errors)
    return render_template('form.html', form=form)


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

及以下是我的html的wtforms代码:

<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
<div class="container">
    <form method="post" action="">
        {{ form.hidden_tag() }}
        {{ form.example1 }}
        <div class="text_field">
            {{ form.content(class="textarea") }}
            {{ form.content1(class="textarea") }}
            <input type="submit" , value="Submit">
        </div>
    </form>
</div>

1

0 个答案:

没有答案