- 在后台通过脚本生成了想要的结果,需要提供一个下载链接返回给前端用户
html代码如下:
<div class="col-md-12">
<button onclick="window.location.href='/download/?id={{ expfile }} ' ">差异表达基因的结果</button>
<br>
<br>
<button onclick="window.location.href='/download/?id={{ difffile }}' ">基因在不同样品中的表达量结果</button>
<h1>表格显示的结果是查询的差异表达基因在不同数据库的差异倍数,pvalue,qvalue结果</h1>
</div>
在views.py里面传递一个文件的路径即可,我这里是将文件的路径存入得了数据库,然后再读出来的,这样可以随时获取差异基因和表达量文件所在的位置,同时进行后续的画图操作
def showtable(request):
gse_direct = ["Liver_cancer", "Myocardial_Infarction", "Macrophage", "VIH", "Renal_fibrosis"]
if request.method == 'POST':
# print (request.POST)
#获取从表单以POST方式提交过来的数据 query_gene_list = request.POST.get('input_gene_list')
disease_type = request.POST.get('disease_type')
species_type = request.POST.get('spcies_type')
tissue_type = request.POST.get('tissue_type')
#######
if len(query_gene_list) == 0:
return render(request, 'error01.html', {'errorlog': "您输入的基因列表为空"})
elif query_gene_list.replace("
", "").replace("
", "").encode('UTF-8').isalnum():
genes = getgenelist(query_gene_list)
genes = map(lambda x: x.upper(), genes)
genes = tuple(genes)
else:
return render(request, 'error01.html', {'errorlog': "您输入的基因列表包含非法字符串,请检查你的基因列表"})
if disease_type in gse_direct and len(genes) !=0:
datapath,rpath=createpath()
gsepath = datapath + "/" + disease_type
degfilelist, expfilelist = fetch_gene.get_exp_list(gsepath)
difftable,degfilename,expfilename = fetch_gene.get_result(genes, degfilelist, expfilelist, gsepath, rpath)
# return HttpResponse("成功生成了你想要的文件")
downexp = rpath + "/" + degfilename
downdiff = rpath + "/" + expfilename
inputgenenum = len(genes)
Resultpath.objects.all().delete()
Resultpath.objects.create(resultpath=rpath, expfilepath=downexp, difffilepath=downdiff,
inputgenenum=inputgenenum,
picpath=rpath)
tablepre = DEGs.objects.all()
resultdata = Resultpath.objects.all()
# print (resultdata)
#获取表头 hlist = ["Database_type", "gene_id", "log2FoldChange", "pvalue", "padj"]
tablenum = int(resultdata.values('inputgenenum')[0]['inputgenenum'])+1
resultpath = resultdata.values('resultpath')[0]['resultpath']
downexp = resultdata.values('expfilepath')[0]['expfilepath']
downdiff = resultdata.values('difffilepath')[0]['difffilepath']
picpath = resultdata.values('picpath')[0]['picpath']
paginator = Paginator(tablepre, tablenum)
page = request.GET.get('page')
if page: # 判断:获取当前页码的数据集,这样在模版就可以针对当前的数据集进行展示
data_list = paginator.page(page).object_list
else:
data_list = paginator.page(1).object_list
try: # 实现分页对象,分别判断当页码存在/不存在的情况,返回当前页码对象
page_object = paginator.page(page)
except PageNotAnInteger:
page_object = paginator.page(1)
except EmptyPage:
page_object = paginator.page(paginator.num_pages)
return render(request, 'show_table2.html', {
'page_object': page_object,
'data_list': data_list,
'myList': hlist,
'resultpath': resultpath,
'expfile': downexp,
'difffile': downdiff,
'img': "output.pdf"
})
else:
return HttpResponse("其他的数据库还未开放,目前只能查询Liver_cancer数据库." )
else:
# 获取表格数据
tablepre = DEGs.objects.all()
resultdata = Resultpath.objects.all()
#获取表头
hlist = ["Database_type", "gene_id", "log2FoldChange", "pvalue", "padj"]
print (Resultpath.objects.values('inputgenenum')[0]['inputgenenum'])
tablenum = int(resultdata.values('inputgenenum')[0]['inputgenenum'])+1
resultpath = resultdata.values('resultpath')[0]['resultpath']
downexp = resultdata.values('expfilepath')[0]['expfilepath']
downdiff = resultdata.values('difffilepath')[0]['difffilepath']
print (downdiff)
print (downexp)
picpath = resultdata.values('picpath')[0]['picpath']
paginator = Paginator(tablepre, tablenum)
page = request.GET.get('page')
if page: # 判断:获取当前页码的数据集,这样在模版就可以针对当前的数据集进行展示
data_list = paginator.page(page).object_list
else:
data_list = paginator.page(1).object_list
try: # 实现分页对象,分别判断当页码存在/不存在的情况,返回当前页码对象
page_object = paginator.page(page)
except PageNotAnInteger:
page_object = paginator.page(1)
except EmptyPage:
page_object = paginator.page(paginator.num_pages)
# 分页功能
return render(request, 'show_table2.html', {
'page_object': page_object,
'data_list': data_list,
'myList': hlist,
'resultpath': resultpath,
'expfile': downexp,
'difffile': downdiff,
'img': "output.pdf"
})
# return render(request, "show_table2.html")
# return HttpResponse("ok") # return render(request,'download.html',{'resultpath':resultpath,'expfile':downexp,'difffile':downdiff})