Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit a Form in a Single Page Application using Flask Without Reloading

I am working on a single page Flask application in python. To change the content of the page, I simply call a javascript function that hides one div and reveals another.

One of these divs contains a form where a user may upload a file, which I run through a python script that parses data from the file.

The problem is that after executing the code, Flask seems to want me to redirect to a new page. I would prefer to return a trigger to simply run my javascript function.

The HTML form:

<form class='upload-form' id='upload-form' action="/upload" method='POST' enctype="multipart/form-data" onsubmit="return show_and_hide('load', 'upload')">
                            <input type="file" name='file' value="Browse...">
                            <input type="submit" value='Upload'>
                        </form>

The Javascript function:

var callFunction;
    function show_and_hide(div_in, div_out){
        var shower = document.getElementById(div_in);
        var hider = document.getElementById(div_out);

        var hider_items = hider.children;
        var i;
        for(i = 0; i < hider_items.length; i++){
            hider_items[i].style.animation = "hide 1s";
        }

        callFunction = setTimeout(change_div, 1000, div_in, div_out);

        var shower_items = shower.children;
        var n;
        for(n = 0; n < shower_items.length; n++){
            shower_items[n].style.animation = "show 1s";
        }
        return true;
    }

And the flask application (I do not actually have it returning ???):

@app.route('/upload', methods=['POST', 'GET'])
def upload_file():
        f = request.files['file']
        new_file_name = str(uuid.uuid1()) + '.zip'

        f.save(os.path.join('uploads/', new_file_name))
        path = 'uploads/' + new_file_name
        return ???

I appreciate any advice! Thank you!

like image 674
eric Avatar asked Sep 06 '19 03:09

eric


People also ask

How can I submit a form without refreshing the page?

Use jQuery's submit event to handle the form submit, add return false; at the end of the submit handle function to prevent the page to reload.

How do you handle form submit in Flask?

In this example, you will ask for the create page using a GET request. The Create page will have a web form with input fields and a Submit button. When a user fills in the web form and clicks the Submit button, a POST request gets sent to the /create route.

How do I make a single page application in Flask?

Flask can be used to serve Single-Page Applications (SPA) by placing static files produced by your frontend framework in a subfolder inside of your project. You will also need to create a catch-all endpoint that routes all requests to your SPA.


1 Answers

You will need to use JavaScript to POST the files to Flask, since submitting a HTML form will always redirect you. Here's an example using AJAX How can I upload files asynchronously?

Flask needs all view functions (routes) to return a response to send to the client. You can reuturn an empty string and a 200 status to let Flask know you processed the request successfully.

@app.route('/upload', methods=['POST', 'GET'])
def upload_file():
        f = request.files['file']
        new_file_name = str(uuid.uuid1()) + '.zip'

        f.save(os.path.join('uploads/', new_file_name))
        path = 'uploads/' + new_file_name
        return '', 200
like image 103
Gabriel Cappelli Avatar answered Oct 12 '22 11:10

Gabriel Cappelli