Reputation: 1
I don't know much about the CherryPy framework, but I would like to use it to give an easy way to use python scripts in my lab.
I would like to submit a file from a form so that python can read it and use it, but that doesn't work. Here is my code :
Python
env = Environment(loader=FileSystemLoader("templates"))
def render_html(html_file, **kwargs):
template = env.get_template(html_file)
return template.render(**kwargs)
def fasta_reader(df):
current_line= ""
while not current_line.startswith(">"):current_line=df.readline().strip()
id_seq=current_line
sequence= ""
while True:
current_line=df.readline().strip()
if current_line.startswith(">"):
yield(id_seq, sequence)
id_seq=current_line
sequence = ""
elif current_line!= "":sequence+=current_line
else:
yield(id_seq,sequence)
break
def protein_search(target, file):
result = ""
prot_not_found = True
for id_seq, seq in fasta_reader(df):
if target in id_seq:
prot_not_found = False
for txt in id_seq, seq:
result += txt + "\n"
if prot_not_found:
return f"La protéine {target} n'a pas été trouvée dans le fichier {file}.\n"
return result
class WebApp:
def __init__(self):
pass
@cherrypy.expose
def index(self):
return render_html('index.html')
@cherrypy.expose
def form_protein_search(self):
return render_html('prot_search_form.html')
@cherrypy.expose
def process_protein_search(self, prot_name, input_file):
result = protein_search(prot_name, open(input_file, 'r'))
*input_filename, format = input_file.split('.')
result_filename = f"'_'.join({prot_name.split()})_in_{'_'.join(input_filename)}.fasta"
return render_html('result.html', action=f'la recherche de la protéine {prot_name} dans le fichier {input_file}', result=result, filename=result_filename)
HTML
{% extends 'base.html' %}
{% block title %}Recherche de protéine{% endblock %}
{% block content %}
<h1>Recherche d'une protéine dans une banque au format fasta</h1>
<form action="/process_protein_search" method="post" enctype="multipart/form-data>
<label for="prot_name">Nom de la protéine:</label><br>
<input type="text" id="prot_name" name="prot_name"><br>
<label for="input_file">Fichier FASTA:</label><br>
<input type="file" id="input_file" name="input_file" accept=".fasta, .fa"><br>
<input type="submit" value="Rechercher">
</form>
{% endblock %}
Can someone help me ?
I tried to open the file in my protein_search function and in my process method. Moreover, the file is searched in my working directory, but I want to create a server so that many poeple can use it.
Upvotes: 0
Views: 18