python - 如何使用flask和wtform执行python脚本或从pythons脚本调用函数
问题描述
我对 Flask 和 Python 非常陌生,所以想了解/清除我的概念。我有一个使用烧瓶和 wtforms 创建的网页。Html 页面非常简单,只有一个字段和一个提交按钮。单击提交按钮时,我想调用python脚本(test.py)本身或python函数(pythonfunction())。另外有没有办法从网页中输入,无论我输入什么,我都可以作为属性传递给那个 python 脚本(test.py)?帮助表示赞赏
**app.py**
from flask import Flask , render_template,flash,redirect,url_for,session,logging,request
from wtforms import Form,StringField,TextAreaField,PasswordField,validators,SelectField,TextAreaField
from wtforms.widgets import TextArea
import subprocess
import test
app=Flask(__name__)
@app.route ('/')
def index():
return render_template('home.html')
class testpython(Form):
testenter=StringField('Enter something')
@app.route ('/testpage',methods=['GET','POST'])
def testpage():
form=testpython(request.form)
return render_template('testpage.html',form=form,python=testfunc(testenter))
if __name__ == '__main__':
app.run(debug=True)
**test.py**
def pythonfunctiontest (self):
print data #<something i can print here from that text field in webpage>
return "all good"
**testpage.html**
{% extends 'sec_layout.html'%}
{% block body %}
{% from "includes/_formhelpers.html" import render_field %}
<form method="POST" action ="">
<div class="form-group">
{{render_field(form.testenter,cols="1", rows="5",class_="form-control")}}
</div>
<div class="input-bar-item input-bar-item-btn">
<button class="btn btn-info">Submit</button>
</div>
</form>
{% endif %}
{% endblock%}
sec_layout.html
<!DOCTYPE <!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>MY PAGE-TEST</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
{% include 'includes/_navbar.html' %}
<div class= "container">
{% block body %}{% endblock%}
</div>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" </script>
</body>
</html>
解决方案
这个问题非常笼统,所以我会试着给你一个指导,也许你稍后会更清楚地重新审视这个问题。
Flask 请求服务器并呈现网页。即它在服务器上执行一些代码并将其传递给客户端网络浏览器。然后,客户端 Web 浏览器可以在用户浏览时执行客户端代码(即 Javascript),并且可以使用提交表单(到不同的 Flask 路由)或通过 JavaScript AJAX 请求(再次到其他 Flask 路由)将数据传递回服务器。因此,如果您想根据某些输入执行 python 脚本,您将需要一个单独的路由。
这是一个索引页面的简单示例和将执行其他操作的第二条路由:
@app.route('/index')
def index():
""" very basic template render """
return render_template('index.html')
@app.route('/data-submit', methods=["POST"])
def calc():
data = request.form.information.data
# do something with data..
x = data + data
return render_template('new_page.html', x)
========= (index.html)
<html>
<body>
<form action="{{ url_for('app.calc') }}" method="POST">
<input name="information" type='text'>
<button name="submit">
</form>
</body>
</html>
推荐阅读
- javascript - 选择时表单中的重复字段
- php - 这似乎是一个愚蠢的问题,但我对 (++) 及其在 php 中的位置感到困惑
- html - SVG在html标签中显示错误的颜色
- sql - 日期之间的 Postgresql
- reactjs - 如何从其他地方的组件更改正文的样式?
- reactjs - Chrome 扩展错误:gapi.auth2.ExternallyVisibleError: Invalid cookiePolicy using React and Webpack
- kubernetes - 生产环境中推荐的 Argo 工作流控制器副本数量是多少?
- html - 块的这个位置怎么做
- python - 如何从图像列表 Pytorch 加载图像数据集
- printing - 在 Ricoh 或 Keyoceria 或 Toshiba 打印机上装订 Postscript 文件