• django文件的下载实现


    • 在后台通过脚本生成了想要的结果,需要提供一个下载链接返回给前端用户
      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})




  • 相关阅读:
    《软件测试》实验
    作业5
    之前看构建之法老段让我写的随笔
    作业2
    Bookstore系统测试缺陷报告
    实验七——Web应用测试(bookstore项目上完成)
    《构建之法》读后感
    (1)写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”); (2)编写单元测试进行测试; (3)用ElcEmma查看代码覆盖率,要求覆盖率达到100%。
    个人简介
    (1)把一个英语句子中的单词次序颠倒后输出。例如输入“how are you”,输出“you are how”; (2)编写单元测试进行测试; (3)用ElcEmma查看代码覆盖率,要求覆盖率达到100%。
  • 原文地址:https://www.cnblogs.com/raisok/p/15148998.html
Copyright © 2020-2023  润新知