Frodo Baggings
Frodo Baggings

Reputation: 57

How to import/incorporate a basic python script in Flask without calling it in a route?

Using Flask I understand how you can have different python functions doing various things according to the URL you put in.

However- I want to know how/if it's possible to incorporate a python script residing in a different folder into a flask framework?

Take this very basic flask app below...

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route("/")
def hello():
    return render_template('echo.html')

@app.route("/test_one")
def test():
    return("Hello world")

For example, how can I 'call' on a separate python script from a folder in the flask framework which prints some text- but which isn't just a basic function connected to one of the routes?

The idea is to apply this concept but to a much more complicated python script which performs a BLAST search.

Upvotes: 0

Views: 3129

Answers (2)

Jose Ricardo Bustos M.
Jose Ricardo Bustos M.

Reputation: 8174

To incorporate a python script residing in a different folder into a flask framework, example

/PATH_TO_FLASK/hello.py

from flask import Flask, Response
import sys
#You need to add the directory that contains script to PYTHONPATH
#There are other ways
#Modify this
sys.path.append('/OTHER_PATH/')
from blast import use_blast

app = Flask(__name__)

@app.route("/")
def hello():
    xml = use_blast()
    return Response(xml, mimetype='text/xml')

if __name__ == "__main__":
    app.run()

/OTHER_PATH/blast.py (Note: biopython required)

from Bio.Blast import NCBIWWW

seq="""AAAGGTTACCCCCACCGACTTTCGGGTGTTACAAACTCTTCGTGGTGTGACNGGGC
GGTGTGTACAAGGCCCCGGGAAACGTANTTCANCCGCGGCATGCTGATCCGCGAT
TACTAGCGATTCCAGCTTCATGTAGGCGAGTTGCAGCCTACAATCCGAACTGAGAA
CGGTTTTATGAGATTAGCTCCACCTCGCGGTCTTGCAGCTCTTTGTACCGTCCATT
GTAGCACGTGTGTAGCCCAGGTCATAAGGGGCATGATGATTTGACGTCATCCCCA
CCTTCCTCCGGTTTGTCACCGGCAGTCACCTTAGAGTGCCCAACTGAATGATGGCA
ACTAAGATCAAGGGTTGCGCTCGTTGCGGGACTTAACCCAACATCTCACGACACGA
GCTGACGACAACCATGCACCACCTGTCACTCTGCTCCCGAAGGAGAAGCCCTATCT
CTAGGGTTGTCAGAGGATGTCAAGACCTGGTAAGGTTCTTCGCGTTGCTTCGAATT"""

def use_blast():
    result_handle = NCBIWWW.qblast("blastn", "nt", seq, hitlist_size=1)
    xml = result_handle.read()
    return xml

Run it

python hello.py

you get,

<?xml version="1.0"?>
<!DOCTYPE BlastOutput PUBLIC "-//NCBI//NCBI BlastOutput/EN" "http://www.ncbi.nlm.nih.gov/dtd/NCBI_BlastOutput.dtd">
<BlastOutput>
  <BlastOutput_program>blastn</BlastOutput_program>
  <BlastOutput_version>BLASTN 2.6.1+</BlastOutput_version>
  <BlastOutput_reference>Stephen F. Altschul, Thomas L. Madden, Alejandro A. Sch&amp;auml;ffer, Jinghui Zhang, Zheng Zhang, Webb Miller, and David J. Lipman (1997), &quot;Gapped BLAST and PSI-BLAST: a new generation of protein database search programs&quot;, Nucleic Acids Res. 25:3389-3402.</BlastOutput_reference>
  <BlastOutput_db>nt</BlastOutput_db>
  <BlastOutput_query-ID>Query_13451</BlastOutput_query-ID>
  <BlastOutput_query-def>No definition line</BlastOutput_query-def>
  <BlastOutput_query-len>502</BlastOutput_query-len>
  <BlastOutput_param>
    <Parameters>
      <Parameters_expect>10</Parameters_expect>
      <Parameters_sc-match>2</Parameters_sc-match>
      <Parameters_sc-mismatch>-3</Parameters_sc-mismatch>
      <Parameters_gap-open>5</Parameters_gap-open>
      <Parameters_gap-extend>2</Parameters_gap-extend>
      <Parameters_filter>L;m;</Parameters_filter>
    </Parameters>
  </BlastOutput_param>
<BlastOutput_iterations>
<Iteration>
  <Iteration_iter-num>1</Iteration_iter-num>
  <Iteration_query-ID>Query_13451</Iteration_query-ID>
  <Iteration_query-def>No definition line</Iteration_query-def>
  <Iteration_query-len>502</Iteration_query-len>
<Iteration_hits>
<Hit>
  <Hit_num>1</Hit_num>
  <Hit_id>gi|725611268|gb|KM660696.1|</Hit_id>
  <Hit_def>Bacillus cereus strain MGH118 16S ribosomal RNA gene, partial sequence</Hit_def>
  <Hit_accession>KM660696</Hit_accession>
  <Hit_len>1447</Hit_len>
  <Hit_hsps>
    <Hsp>
      <Hsp_num>1</Hsp_num>
      <Hsp_bit-score>874.117</Hsp_bit-score>
      <Hsp_score>968</Hsp_score>
      <Hsp_evalue>0</Hsp_evalue>
      <Hsp_query-from>1</Hsp_query-from>
      <Hsp_query-to>502</Hsp_query-to>
      <Hsp_hit-from>1447</Hsp_hit-from>
      <Hsp_hit-to>948</Hsp_hit-to>
      <Hsp_query-frame>1</Hsp_query-frame>
      <Hsp_hit-frame>-1</Hsp_hit-frame>
      <Hsp_identity>496</Hsp_identity>
      <Hsp_positive>496</Hsp_positive>
      <Hsp_gaps>2</Hsp_gaps>
      <Hsp_align-len>502</Hsp_align-len>
         <Hsp_qseq>AAAGGTTACCCCCACCGACTTTCGGGTGTTACAAACTCTTCGTGGTGTGACNGGGCGGTGTGTACAAGGCCCCGGGAAACGTANTTCANCCGCGGCATGCTGATCCGCGATTACTAGCGATTCCAGCTTCATGTAGGCGAGTTGCAGCCTACAATCCGAACTGAGAACGGTTTTATGAGATTAGCTCCACCTCGCGGTCTTGCAGCTCTTTGTACCGTCCATTGTAGCACGTGTGTAGCCCAGGTCATAAGGGGCATGATGATTTGACGTCATCCCCACCTTCCTCCGGTTTGTCACCGGCAGTCACCTTAGAGTGCCCAACTGAATGATGGCAACTAAGATCAAGGGTTGCGCTCGTTGCGGGACTTAACCCAACATCTCACGACACGAGCTGACGACAACCATGCACCACCTGTCACTCTGCTCCCGAAGGAGAAGCCCTATCTCTAGGGTTGTCAGAGGATGTCAAGACCTGGTAAGGTTCTTCGCGTTGCTTCGAATT</Hsp_qseq>
         <Hsp_hseq>AAAGGTTACCCCCACCGACTTTCGGGTGTTACAAACTCTTCGTGGTGTGACGGGCCGGTGTGTACAAGGCCCCGGGAA-CGTA-TTCACCCGCGGCATGCTGATCCGCGATTACTAGCGATTCCAGCTTCATGTAGGCGAGTTGCAGCCTACAATCCGAACTGAGAACGGTTTTATGAGATTAGCTCCACCTCGCGGTCTTGCAGCTCTTTGTACCGTCCATTGTAGCACGTGTGTAGCCCAGGTCATAAGGGGCATGATGATTTGACGTCATCCCCACCTTCCTCCGGTTTGTCACCGGCAGTCACCTTAGAGTGCCCAACTTAATGATGGCAACTAAGATCAAGGGTTGCGCTCGTTGCGGGACTTAACCCAACATCTCACGACACGAGCTGACGACAACCATGCACCACCTGTCACTCTGCTCCCGAAGGAGAAGCCCTATCTCTAGGGTTGTCAGAGGATGTCAAGACCTGGTAAGGTTCTTCGCGTTGCTTCGAATT</Hsp_hseq>
      <Hsp_midline>||||||||||||||||||||||||||||||||||||||||||||||||||| || ||||||||||||||||||||||| |||| |||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||</Hsp_midline>
    </Hsp>
  </Hit_hsps>
</Hit>
</Iteration_hits>
  <Iteration_stat>
    <Statistics>
      <Statistics_db-num>41176984</Statistics_db-num>
      <Statistics_db-len>113854703</Statistics_db-len>
      <Statistics_hsp-len>0</Statistics_hsp-len>
      <Statistics_eff-space>0</Statistics_eff-space>
      <Statistics_kappa>0.41</Statistics_kappa>
      <Statistics_lambda>0.625</Statistics_lambda>
      <Statistics_entropy>0.78</Statistics_entropy>
    </Statistics>
  </Iteration_stat>
</Iteration>
</BlastOutput_iterations>
</BlastOutput>

Upvotes: 1

argyris
argyris

Reputation: 23

You can import it as module. Let's say you have another file, functions.py located in the same folder. That file contains many functions, including printdummy(), which simply prints the Hello world! statement.
Your new code should look like:

from flask import Flask, render_template, request
from functions import printdummy
app = Flask(__name__)

@app.route("/")
def hello():
    return render_template('echo.html')

@app.route("/test_one")
def test():
    return(printdummy())

Upvotes: 1

Related Questions