【问题标题】:How to implement an external python code to a Django web server?如何在 Django Web 服务器上实现外部 python 代码?
【发布时间】:2021-12-02 04:00:39
【问题描述】:

正如标题所示,我正在尝试将外部 python 代码实现到 Django Web 服务器。
我对编程很陌生,所以任何提示肯定会有所帮助。

长话短说:我正在尝试设置一个表单,用户必须在其中插入氨基酸序列。这个序列应该传递给我的python脚本,它能够将它与数据库中已经存在的所有序列进行比较,并给出最相似的序列。我的问题是我不能让我的表单和我的脚本互相交谈。
我在https://docs.djangoproject.com/en/3.2/topics/forms/ 处关注了 Django 文档,但这并没有太大帮助。
还在线漫游并浏览这里已经提出的问题是徒劳的。
请在文件下方找到:
BLAST_page.html(已尝试,已评论和未评论)

{% extends "base_generic.html" %}

{% block content %}
<div class="container-fluid" style="text-align: center;" ></div>
    <form method="post" action= {% url 'BLAST-process' %}>
        {% csrf_token %}
        {{ blast }}
        <label for="sequence">Type or paste your sequence in the box below</label><br><br> 
        <input type="text" id="sequence" class="input_text" name="sequence" value="{{ sequence }}" style="width:600px; height:200px;"><br><br>
        <input type="submit" value="Submit">
    </form>
</div>
    {% endblock %}    
<!--    
    <div class="container-fluid" style="text-align: center;" >
    <form method="POST" action= {% url 'BLAST-process' %}>
        {% csrf_token %}
    <label for="sequence">Type or paste your sequence in the box below</label><br><br>  
    <input type="text" id="sequence" class="input_text" name="sequence" value="{{ sequence }}" style="width:600px; height:200px;"><br><br>  
    <input type="submit" value="Submit">
    </form>
</div>
-->

为了检查这个表单是否有效,我使用了这个简单的 .php 脚本。背后的原因是,如果表单正常工作,则应回显插入的数据。但这不会发生。

<html>
<body>

Sequence: <?php echo $_POST["sequence"]; ?><br>
<?php
    echo "<h2>Your Input:</h2>";
    echo $sequence;
    ?>
</body>
</html>

forms.py

from django import forms

class blast(forms.Form):
    sequence = forms.CharField(help_text="Enter a sequence", label='sequence')

blast.py 应该从表单接收数据的脚本

from Bio.Blast.Applications import NcbiblastpCommandline
from io import StringIO
from Bio.Blast import NCBIXML
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio import SeqIO
import numpy as np

# Create two sequence files

#taken from the textArea.
sequence = sequence
seq1 = SeqRecord(Seq(sequence), id="x")

SeqIO.write(seq1, "seq1.fasta", "fasta")
#SeqIO.write(seq2, "seq2.fasta", "fasta")

# Run BLAST and parse the output as XML
output = NcbiblastpCommandline(query="seq1.fasta", 
                               subject="/Users/santarpia/Documents/tutorial/codorenv/RepOdor/FASTA/db.fasta",
                               outfmt=5)()[0]

blast_result_record = NCBIXML.read(StringIO(output))

# Print some information on the result
for alignment in blast_result_record.alignments:
    for hsp in alignment.hsps:
        print('***Alignment****\n')
        print('Alignment title', alignment.title)
        print('Alignment Length:', alignment.length)
        print('E-value:', hsp.expect)
        print('Gaps:', hsp.gaps)
        print('Identities:', hsp.identities)
        print('Positives:', hsp.positives)
        print('Score:', hsp.score)
        print('Bits:', hsp.bits)
        print('Coverage:', hsp.align_length)
        print('% Identity:', np.round((hsp.identities / hsp.align_length) * 100, 2))
        print("\n")
        print (hsp.query[0:])
        print("\n")
        print (hsp.match[0:])
        print("\n")
        print (hsp.sbjct[0:])
        print('****************************\n\n\n')

如前所述,我们将不胜感激任何有关如何设置的评论。如果您需要更多文件或信息来回答我的问题,请随时索取。

【问题讨论】:

    标签: python php html django webserver


    【解决方案1】:

    因此,如果您在将序列提交到表单后正确遵循文档,则代码应“输入”视图的if request.method == 'POST' 部分。您可以通过在 if(或 import pdb; pdb.set_trace())下放置 print("hello world") 语句来验证这一点。在那里,您可以使用sequence = form.cleaned_data['sequence'] 从表单中获取序列。现在要将它传递给您的脚本,您需要您的脚本是一种可以接受输入(序列)的方法,因此将您的脚本包装在 def findMostSimilarSequence(sequence): 之类的东西中并删除第一行 sequence = sequence 然后在您的视图中您可以导入方法并使用表单中的序列变量调用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      相关资源
      最近更新 更多