1

I have searched all over for a simple solution to this problem, but have yet to find anything. Please let me know how you would approach this.

I have a Django project and an app in which of the views for the app calls a function that takes a significant amount of time (upwards of 5-10 min) to run, so I would like to display a loading screen temporarily, and then have the results of the function display.

I've looked into using JavaScript, IFrames, and other solutions, but nothing has yet to work.

Please keep in mind that I'm a beginner/intermediate programmer, but I would like to know if there's a simple solution to what I thought was a simple and common problem, or if I need something more complex.

Ian Moore
  • 35
  • 1
  • 8
  • 1
    You should first check your view function if its taking 5-10min to render the response. Using ajax or using django template ? – Subhanshu Jul 11 '18 at 05:36
  • Using JavaScript, you could implement an AJAX call to your Django backend to retrieve the data in json format and then append the data to your page. [jQuery](https://jquery.com/) has a quite convenient [AJAX wrapper](https://api.jquery.com/jQuery.ajax/), but there might be other js libraries out there that do the same – shmee Jul 11 '18 at 05:36
  • Your view shouldn't take that long to respond! Can you share your views.py file here? – Lucifer Jul 11 '18 at 05:38
  • 2
    Usually such heavy tasks are run in the background (may even on specialized machines) by a task queue like for example celery. The user starts then task in one request and the website shows when it's done via Ajax or just on the next request. – Klaus D. Jul 11 '18 at 05:45
  • See [here](https://stackoverflow.com/questions/8317219/django-show-loading-message-during-long-processing) – Chiheb Nexus Jul 11 '18 at 05:46
  • This is all that is in the view: `def index(request): datalist = interface.last_data() return render(request, 'impliedvolatilities/index.html', {'datalist': datalist})` but `interface.last_data()` takes a while to respond – Ian Moore Jul 11 '18 at 05:46
  • `I've looked into using JavaScript, IFrames, and other solutions`: Care to post a few of those? What have you tried so far? – Selcuk Jul 11 '18 at 05:46
  • Selcuk, I've tried writing an automatically updating IFrame with Python changing a "static" file, but decided that it wouldn't be suitable for production and it doesn't really work, because the function still has to complete before the return. I also tried queries to databases, but again the same problem is that the function returns only once. – Ian Moore Jul 11 '18 at 05:49
  • shmee and Klaus D., thank you! That's exactly what I was looking for. Chiheb, thank you as well, I'll check into those solutions (sorry, I guess my StackOverflow searches weren't very good). – Ian Moore Jul 11 '18 at 05:51
  • what's the code in `interface.last_data()`? If it's a database query, there might be a way to optmize it? Totally different question though. – Ashish Acharya Jul 11 '18 at 05:52
  • Ashish Acharya, yes that is a different question, but to answer it it goes and pulls data from an API and then analyzes it. Due to the amount of requests and the analytics, it takes several minutes. I wouldn't think there's much I could do to optimize it. – Ian Moore Jul 11 '18 at 06:32

0 Answers0