0

Creating a simple front end with Flask where I can select multiple files and runs some calculations on them.

Currently I am using the code below, but it is only good for 1 file, #do something is where the conversion happens;

class Sources(SimpleFormView):
    form = MyForm
    form_title = 'This is my first form view'
    message = 'My form submitted'

    def form_get(self, form):
        form.field1.data = 'This was prefilled'

    def form_post(self, form):
        x = #do something
        return self.render_template('test.html', table = x ,name='TEST')

The form basically lets me type in the path as shown below:

from wtforms import Form, StringField
from wtforms.validators import DataRequired
from flask.ext.appbuilder.fieldwidgets import BS3TextFieldWidget
from flask.ext.appbuilder.forms import DynamicForm


class MyForm(DynamicForm):
    Path = StringField(('Field1'),
        description=('Your field number one!'),
        validators = [DataRequired()], widget=BS3TextFieldWidget())

I am trying to select multiple files from my local machine and then process them together. Much like how we attach files using Gmail;

  1. Option to select file path
  2. Open file browser
  3. Store file path
  4. Process 1 and 3 repeats till hit threshold or submitted.

I am currently using Flask App Builder to get my front end right.

4

1 回答 1

0

您可以使用此 HTML 表单,它允许用户选择多个文件:

<form method="POST" enctype="multipart/form-data" action="/upload">
  <input type="file" name="file[]" multiple="">
  <input type="submit" value="Upload Files">
</form>

然后在您的上传功能中,您使用 Flask 中的 getlist 功能。

@app.route("/upload", methods=["POST"])
def upload():
    uploaded_files = flask.request.files.getlist("file[]")
    print uploaded_files
    return ""

我建议附加你的做某事功能以接受所有文件的列表作为输入。然后做类似的事情

For file in uploaded_files:
    Process the files
于 2016-08-06T12:32:04.980 回答