0

I've been in the process of creating a Flask application through the past week or so but nothing seems to work. The goal is to create a Pandas Dataframe based on a set of parameters that are meant to be submitted in the form.

On the front-end, everything seems to work fine, at least for the main page. However, it seems that an error comes up whenever I try to 1) actually create the dataframe based on the parameters (this is meant to be done with my_function, and 2) whenever I try to go to the /downloads part of the webpage.

All of the code I have is included below, including for both of the templates. The main purpose here of the /downloads page is simply to print a message saying that the function, ideally created in the main page, "/", did its job in creating the new DataFrame.

I've tried many things already, including using a more basic approach with if request.methods=='POST", but nothing seems to be working. Any help would be greatly appreciated. Thank you!

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 %}
stevene97
  • 95
  • 1
  • 4

1 Answers1

0

See this tutorial: https://www.tutorialspoint.com/flask/flask_file_uploading.htm

Make sure enctype = "multipart/form-data" is set.

Rick
  • 138
  • 1
  • 10