Running a Python script on HTML Button Click

I am fairly new to Django. Basically, I have a python script which translates text from a language to English. The file containing the text will be .csv or .tsv and user can upload that file for which I have a form.
Now when the user uploads the file and presses the “Translate” button, I want Python (not JavaScript/JQuery/AJAX template) to handle it.
Now I am aware of views and rendering and redirecting etc. But basically I’m not aware of options to connect a Python script/view with an HTML button click.

The folder structure for the project is as under:

The contains the module responsible for translating the text/data. I call the concerned function in a view but I don’t know how I can trigger the view once the user will click the HTML button (whose type is “submit”).

Also, I would like that once the file has been uploaded and translate button has been pressed, some spinner or progress bar is displayed whose width (or percentage) values are updated continuously depending upon how much the file has been translated.
Lastly, I want that translated file to be available for the user to download.

The HTML right now is:

<<!DOCTYPE html>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Translate data to English</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
        <script src="" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>

        <div class="col-md-6 offset-md-3 mt-5">
             <form accept-charset="UTF-8" method="POST" enctype="multipart/form-data" target="_blank">
                {% csrf_token %} 
                <div class="form-group">
                 <label for="exampleInputName">Full Name</label>
                 <input type="text" name="fullname" class="form-control" id="exampleInputName" placeholder="Enter your name and surname" required="required">
               <div class="form-group">
                 <label for="exampleInputEmail1" required="required">Email address</label>
                 <input type="email" name="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
               <div class="form-group">
                 <label for="exampleFormControlSelect1">Favourite Platform</label>
                 <select class="form-control" id="exampleFormControlSelect1" name="platform" required="required">
               <div class="form-group mt-3">
                 <label class="mr-2"> <h6> Upload the file which you want to get translated: </h6> </label>
                 <input type="file" name="file">
               <button type="submit" class="btn btn-primary">Translate</button>

When a submit button is pressed, that causes a request to be sent to the server.

In the case of Django, the server is going to direct that request to a view.

That view can run whatever python code you want it to run. Yes, the tutorials and examples all show that in the context of only building the page to be returned, but you are not limited by what’s shown in those examples.

Also, virtually all the examples show the view returning an HttpResponse, but again, you’re not limited to that. Your view could return a FileResponse.

This is going to require some JavaScript to be used for this, along with some “coordinating communication” with the server. This may end up needing to get something like websockets involved.

There are two basic possibilities here:

  • The translation is going to happen quickly enough that the progress bar is superfluous.
  • The translation takes long enough that keeping the connection open is an issue.

With the first case, I’d strongly discourage attempting the progress bar at all.

In the second case, I wouldn’t tie up the user’s session by making them sit and wait for the task to complete. I’d spawn off the translation task to something like Celery and let it complete in the background. You can then provide for a “message” or a “notification” to be sent to the user to let them know that the task has finished.

You also want to consider how you’re going to store and manage these files.

  • How long are you going to keep them?
  • How long should they be available for download?
  • Do you want to store them as files, or store them as data within the database?