简体   繁体   中英

Uploading multiple files with Flask App Builder

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.

You can use this HTML form which will allow the user to select multiple files:

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

Then in your upload function you use getlist function from Flask.

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

I would recommend appending your do something function to accept a list of all of the files as an input. Then do something like

For file in uploaded_files:
    Process the files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM