Attempted adding download

This commit is contained in:
Jesse Yang
2021-11-07 23:20:09 -05:00
parent 7672c79cd7
commit 54f2df9847
4 changed files with 64 additions and 1 deletions

Binary file not shown.

View File

@@ -1,5 +1,5 @@
import React, { useState, useEffect } from 'react'
import { makeStyles, Modal, Backdrop, Fade, LinearProgress } from '@material-ui/core'
import { makeStyles, Modal, Backdrop, Fade, LinearProgress, Button } from '@material-ui/core'
import axios from 'axios'
const useStyles = makeStyles((theme) => ({
@@ -24,6 +24,7 @@ export default function LookupModal(props) {
const [processed, setProcessed] = useState(false);
const [_, setLookupValid] = useState(true);
const [viewPath, setViewPath] = useState(null);
const [filePath, setFilePath] = useState('');
// const protectEmail = (email) => {
// let avg, splitted, p1, p2;
@@ -62,6 +63,13 @@ export default function LookupModal(props) {
} else if (res.data.status == 'SUCCESS') {
setProcessed(true)
setViewPath(`http://localhost:8000/view/${props.id}`)
axios
.get(`http://localhost:8000/api/download_loc/${props.id}/`)
.then((res) => {
console.log(res)
setFilePath(res)
})
.catch((err) => console.log(err))
} else if (res.data.status == 'FAILURE') {
setProcessed(true)
}
@@ -74,6 +82,15 @@ export default function LookupModal(props) {
}
}, [counter, props.id]);
const downloadFile = () => {
axios
.get(`http://localhost:8000/api/download/${props.id}/`)
.then((res) => {
console.log(res)
})
.catch((err) => console.log(err))
}
return (
<div className='LookupModal-Container'>
<Modal
@@ -96,6 +113,7 @@ export default function LookupModal(props) {
<p>Job id: {props.id}</p>
<p><em>The results of your prediction have been emailed.</em></p>
<p><em>View and analyze results <a href={viewPath}>here</a></em></p>
<p><Button variant='contained' onClick={downloadFile}><a href="" download={filePath}>Download</a></Button></p>
</div> :
<div className='LookupModal-Info'>
<p><em>Refreshing in {counter} seconds...</em></p>

View File

@@ -1,3 +1,4 @@
import csv
import itertools
import logging
import os
@@ -8,8 +9,10 @@ from pathlib import Path
import pandas as pd
from django.conf import settings
from django.core.files import File
from django.core.files.uploadedfile import UploadedFile
from django.http import HttpResponse
from django.utils.encoding import smart_str
from django.views.generic import View
from rest_framework import status
from rest_framework.decorators import api_view
@@ -213,3 +216,43 @@ def get_position(request, uuid):
logging.debug(f"Job {uuid} status {job_state}")
logging.info("# Sending response")
return Response({"id": uuid, "status": job_state})
@api_view(["GET"])
def get_download(request, uuid):
logging.info(f" # Getting Download for {uuid} ...")
job = Job.objects.get(pk=uuid)
result_path = job.result_fi
logging.debug(result_path)
with open(result_path, "r") as f:
logging.debug(f)
logging.debug("opened file successfully")
return HttpResponse(
f,
headers={
"Content-Type": "application/octet-stream",
"Content-Disposition": 'attachment; filename="test.tsv"',
},
)
response = HttpResponse(
content_type="text/csv",
headers={"Content-Disposition": f'attachment; filename="{uuid}.csv"'},
)
writer = csv.writer(response)
with open(result_path, "r") as f:
logging.debug("Opened file")
logging.debug("Trying to use lines")
for line in f:
logging.debug(line)
writer.writerow(line.strip().split("\t"))
return response
@api_view(["GET"])
def get_download_loc(request, uuid):
logging.info(f" # Getting Download Location for {uuid} ...")
job = Job.objects.get(pk=uuid)
result_path = job.result_fi
return result_path

View File

@@ -22,4 +22,6 @@ urlpatterns = [
re_path("(^(?!(api|admin|view)).*$)", views.FrontendAppView.as_view()),
path("api/predict/", views.predict),
path("api/position/<uuid:uuid>/", views.get_position),
path("api/download/<uuid:uuid>/", views.get_download),
path("api/download_loc/<uuid:uuid>", views.get_download_loc),
]