Home > front end >  send variables or messages to page while script is doing tasks (django 4)
send variables or messages to page while script is doing tasks (django 4)

Time:10-20

I'm doing some python script with django, and I when the user submit a file, I process the file and do tons of tasks, it can last a few minutes. While the user is waiting, I got a loading screen in js, and I print in console what's happening. But I would like to print it in the page, on the loading screen, is there a simple way to do this?

my model looks like that

models.py
def main():
    #all the long stuff & logic

and my view :

views.py
def upload(request):
    #some other stuff
    MyModel.main()
    return render(request,
        'frontend/upload.html',{
            'form':form,
        }
    )

It can be frustrating for the user (and for me) to wait and not know what's going on, I'd like to use my page as I use the console with print, because I'm not over with the stuff to do and I guess it could last some long minutes.

CodePudding user response:

You are trying to get information about the status of your running tasks.

I would do a database table containing a token, a task name and a progression column.

This table should be fed with informations from your main function.

You can provide a token to your user when he uploads a file, then periodically call a django view from your JS.

This view will return the current task and status based on the token you provided.

In your upload function :

def upload():
    #some other stuff
    token = uuid4.randomUUID()
    MyModel.main(token)
    return render(request,
        'frontend/upload.html',{
            'form':form,
            'token':token,
        }
    )

and in your main function :

def main(token):
    current_task = Task.objects.get(id=token)
    try:
        # some task
        current_task.name = "next_operation"
        current_task.status = "running"
    except:
        current_task.status = "failed"
    finally:
        current_task.save()
        

This would also provide logs.

Then you have to create a view returning a task and its status when you give it a token, and display whatever it returns.

The view providing the task details :

def upload_status_view(request):
    token = request.GET.get("token")
    if token is None:
        return HttpResponse(status=404)
    task = Task.objects.get(id=token)
    json = '{"task": "'   task.name   '", "status": "'   task.status   '"}'
    return HttpResponse(status=200, content=json)

And in your front-end you could use some javascript to call the upload status view and update your page :

while(condition) {
    url = "https://myserver.com/task/status?token=MY_TOKEN";
    fetch(url).then((response) => {
        if(response.status === 200) {
            response.json().then((data) => {
                document.getElementById("myDiv").innerHTML = data["task"]   " : "   data["status"];
            });
        }
        else if (response.status === 404) {
            document.getElementById("myDiv").innerHTML = "task not found";
        }
    }
}
  • Related