Flask - render template asynchronously - python

I'm making a Flask app and I was wondering if I could render a template for a route, but redirect the user after a function is complete. Currently using Python 2.7 Here is my example
#app.route('/loading/matched')
def match():
time_match()
return render_template('matched.html')
def time_match():
# match two users based on time
sleep(3) # pretend to be doing
return redirect('/loading/generation')
I don't know where to begin. Is there a library I should use?

This sounds more like a client sided thing to me? Do you want something like a loading bar?
You could provide an ajax route which initiates heavy workload on the server side - while the client does show some progress. Once the workload finished you render a template which than gets loaded via ajax.
For asycn workload you could look into Celery, which is a great library for that. It even can do work on a seperate server...
More sources Integration in Flask

Related

Flask redirect alternative in Seldon core

I have a kibana dashboard where there are some links provided for the user to click on. The link calls a flask service, which does some processing and redirects an URL using flask's redirect API, so that Kibana dashboard shows the processed values. Now, the flask is replaced with seldon core for predictions. Is there any way to redirect an URL, like it can be done in flask?
Thanks in advance.
Have you completely removed the flask app? If not you can use the flask to handle all interactions with the frond end and just use Seldon for the predictions.
This is a micro-services approach. You can receive the request as you used to in flask and then from within flask call the Seldon micro-service, get the prediction and then redirect to the results page with the new results from within flask.
This is good because if you change your prediction logic or tools in the future you wont have to redo all this work. You ll just change the function that flask calls to get the results. Also you frond end and back-end will be agnostic of the implementation details of the prediction method.

Flask: Redirect after function/loading complete?

I am using Python 3.6.1 with Flask, with the goal being to load a webpage, display live loading information via websockets, then once the loading is complete redirect or otherwise allow the user to do something.
I have the first two steps working- I can load the page fine, and the websocket interface (running on a separate thread using SocketIO) updates properly with the needed data. But how can I make something happen once the functions that need to load are finished? To my understanding once I return a webpage in Flask it is simply static, and there's no easy way to change it.
Specific code examples aren't necessary, I'm just looking for ideas or resources. Thanks.

Long time request in Flask with React

I am building an application in Flask API and React.
The first page of the app presents the user with an upload file form. The user selects a file (700 MB) and click uploads.
Once this is done, the backend:
Takes the file, unzip it
Run some ML model
Returns a JSON containing the right data
When this is over, react gets the JSON and renders a new page.
These three steps takes more than 10 minutes therefore I get an error 500 which I believe is due to the long time request timeout.
I would like to know if there is a way to make timeout=None.
I looked for some answers and they suggest to use Celery. However, I am not sure if this is the right approach for my task.
I second with #TheIncorrigible suggestion to solve with some kind of event driven architecture what you are doing is Web Worker Architecture. Ref
Your problem reminds me one of the AWS service called control tower where launching landing zone of that service takes more than >10min and AWS gracefully handles that. When you try to launch it gives me a banner saying it is progress and would take 1 hour. In console log I noticed they were using Promise(Not exactly sure how they are achieving and how long it can handle).
May be you could try using Promises in react for asynchronous computations. I am not expert but it looks like you can achieve this using that. You may watch this short video for basic understanding.
There is also signalr that allows server code to send asynchronous notifications to client-side web applications. You can check if that can be applied in your case signalr in python dicussion

Can Django do further process after AJAX response?

I'm working on a Django application processing AJAX requests. The code is like this:
def handler(request):
# save the data to the database
return HttpResponse(# some json)
The problem is that I want to further process the data that the user has submitted, but after responding the user with the success information. So is there a way to make the response without using return ? Or is there any other way to do that?
In general, you should use Celery for this type of task: it's the standard for asynchronous processing.
You can learn more about it here:
Django with celery

How can I have a python module run asynchronously and recieve calls from other modules?

So I'm currently working on adding a recommendation engine to a Django project and need to do some heavy processing (in an external module) for one of my view functions. This significantly slows down page load time because I have to load in some data, transform it, perform my calculations based on parameters sent by the request, and then return the suggestions to the view. This has to be done every time the view is loaded.
I was wondering if there was some way I could have the recommender module load and transform the data in memory, and then wait for parameters to be sent from the view, have calculations run on those parameters and then send it back to the view.
Any help would be greatly appreciated.
Celery is a task queue that reeally excels at this sort of thing.
It would allow you to do something like:
user makes request to view
view starts an async task that does the heavy lifting, then returns to the user immediately
you can poll from javascript to see if your task is done and load the results when it is
Might not quite be the flow you're looking for but celery is definitetly worth checking out
Celery has a great django package too, extremely easy to use
Rereading your question, i think it would also be possible to create a local webservice around your recommendation engine. On startup it can load all the data into memory, then you can just make requests to it from your django app?

Categories

Resources