If there is a better way to accomplish this, please let me know. I need to synchronously process data outside of a view, then only after data is processed in an external function redirect or open a new view.
- File is uploaded from an HTML page, which routes the file to views.
- Threading in views begins processing data in script outside of views.
- Views returns a view to display the data as it’s being processed using SSE.
- External script sends data using
send_event('test', 'message',{'text': stuff}
Here’s where I’m stuck: I’ve tried using the Javascript event listener to watch for certain text that then does a window.open('newview')
, as I have a view that downloads the CSV the script creates. I’m having a problem going from one view (with an active SSE connection) to another while waiting on the thread that is processing data to finish. I’ve tried a second thread in views.py
that returns another response when thread x is finished.
I have to return render
from the first view so that the live data shows to the client.
Html for upload:
<form action="/viewFunction/" method="POST" enctype="multipart/form-data" form target="_blank">
{% csrf_token %}
<input type="file" id="fileform" name="filename">
<input type="submit" id="fileform">
views.py:
def viewFunction(request):
csv = pd.read_csv(request.FILES['filename'])
file is uploaded from an html form
x = threading.Thread(target=external.script, args=[csv])
x.setDaemon(False)
x.start()
return render(request, 'html_that_displays_SSE_output.html')
external script.py:
iterate over CSV, making API calls
build CSV based on server responses
send_event('test', 'message', {'text': data}