'Sending CSV to client over SSE on file generation
I'm learning Django on the fly so sorry if this is a stupid question, I think the answer is no.
I'm wanting to push a generated CSV file to the client and download it automatically after it's finished. I was originally going to create a view for this, but I'm unable to call a view function from an external function. Views passes form data to an external function to process.
Views.py:
def viewsFunc(request):
if request.method == "POST":
csvUpload = pd.read_csv(request.FILES['filename'])
userName = request.POST['Username']
password = request.POST['Password']
x = threading.Thread(target=externalScript.externalFunc, args=[csvUpload, userName, password])
x.setDaemon(False)
x.start()
return render(request, 'app/htmlPage.html')
externalScript.py:
def externalFunc(csvUpload, userName, password):
'do stuff & sends data to client, create CSV'
send_event('test', 'message', {'text': data})
As you can see, I'm unable to return response from here outside of views.py so I'm not sure if it's possible or what best practice would be. Best case scenario is I pass the CSV to the client without saving to the server.
How terrible would it be to do in views.py? It feels "hacky":
x = threading.Thread(target=externalScript.externalFunc, args=[csvUpload, userName, password])
if x.isAlive() == False:
csvDownloadViewFunc()
def csvDownloadViewFunc():
response = HttpResponse(
content_type='text/csv',
headers={'Content-Disposition': 'attachment; filename="testForm.csv"'},
)
return response
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
