在过去一周左右的时间里,我一直在创建Flask应用程序,但似乎没有任何效果。目标是基于一组要以表单形式提交的参数来创建Pandas数据框。
在前端,一切似乎都正常,至少对于主页而言。但是,每当我尝试转到/ downloads部分时,似乎每当我尝试1)实际基于参数创建数据框(这将使用my_function
和2)时都会出现错误。网页。
下面包含了我拥有的所有代码,包括两个模板的代码。 /downloads
页面的主要目的只是打印一条消息,指出理想情况下在主页"/"
中创建的函数在创建新DataFrame时已完成工作。
我已经尝试了许多事情,包括对if request.methods=='POST"
使用更基本的方法,但是似乎没有任何效果。任何帮助将不胜感激。谢谢!
from flask import Flask, render_template, Response, request, redirect, url_for
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms import FileField, StringField, IntegerField, SubmitField
from wtforms.validators import DataRequired
from my_script import my_function
app = Flask(__name__)
bootstrap = Bootstrap(app)
### setting configuration for WTF extension
app.config['SECRET_KEY'] = 'my secret key'
class FileUploadForm(FlaskForm):
file_uploaded = FileField('Upload a file here:', validators=[DataRequired()])
int_param = IntegerField('Enter number:', default=12, validators=[DataRequired()])
param1 = StringField('Enter param1:', validators=[DataRequired()])
param2 = StringField('Enter param2:', validators)
submit = SubmitField('Download File')
### main page: contains the fields we need
@app.route('/', methods=['GET', 'POST'])
def index():
form = FileUploadForm()
if form.validate_on_submit():
my_function(form.file_uploaded.data, form.int_param.data, form.param1.data,
form.param2.data)
return render_template('download.html')
return render_template('index_sys.html', form=form)
@app.route('/downloads', methods=['GET', 'POST'])
def downloads():
return render_template('download.html')
index_sys.html:
{% extends "base_sys.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}Title{% endblock %}
{% block page_content %}
Fill out the fields below.
<br><br>
{{ wtf.quick_form(form, action='/downloads') }}
{% endblock %}
download.html:
{% extends "base_sys.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}Title{% endblock %}
{% block page_content %}
<h2>File downloaded successfully</h2>
{% endblock %}
答案 0 :(得分:0)
请参阅本教程:https://www.tutorialspoint.com/flask/flask_file_uploading.htm
确保设置了enctype = "multipart/form-data"
。