如何链接独立的python代码以在烧瓶中使用。
例如
import sys
# By design, the patterns come in one per line piped in from STDIN
for line in sys.stdin.readlines():
line = line.strip()
# 1) Split the pattern into clauses.
# 2) Translate each clause into regex syntax
# 3) reassemble the full regex pattern
clauses = line.split("-")
regex_clause_array = []
for clause in clauses:
# re_clause: the incremental build-up of the clause into regex syntax
re_clause=None
# Convert the prosite negation into a regex inverted character class
if clause.startswith("{"):
neg_pieces = clause.split("}")
# neg_pieces[0][1:] is the character set for the negation
# neg_pieces[1] is the optional quantification
re_clause = "[^%s]%s" % (neg_pieces[0][1:], neg_pieces[1])
else:
re_clause = clause
# change the quantification parenthesis into regex curly-braces
re_clause = re_clause.replace(")","}")
re_clause = re_clause.replace("(","{")
# change wildcards from 'x' to '.'
re_clause = re_clause.replace("x",".")
# save the regex-syntax clause to the regex clause array
regex_clause_array.append(re_clause)
# add the leading and trailing slashes and concatenate all regex clauses
# together to form the full regex pattern
print ("/%s/" % ("".join(regex_clause_array)))
上面的代码单独工作,它需要一个序列,例如Px(2)-GESG(2) - [AS]并转换为python正则表达式P. {2} GESG {2} [AS]。
我无法弄清楚的是,我正在尝试使用flask将其链接到webtool。我现在拥有的是一个简单的网页,它有一个文本框和一个提交按钮,但无法将上述代码链接到路由应用程序。
答案 0 :(得分:2)
将代码放在不同的python文件中并命名。现在,您可以将python文件用作模块。将代码放在一个函数中。你可以在flask中导入python文件。
from filename import function
您可以在
等视图中使用它@app.route('/link' , methods = ['POST'])
def view_function():
call = function()
现在在提交按钮中,您可以执行以下操作:
<form action="/link" method = 'post'>
<input type="submit" value="submit" />
</form>