【问题标题】:Link a url in flask using python function使用python函数链接烧瓶中的url
【发布时间】:2023-03-31 00:30:01
【问题描述】:

我正在尝试在 Flask 中链接一个 url,但找不到问题的解决方案。我的想法是根据我网页中给出的参数在 UniProt 中搜索蛋白质。例子是:

@app.route('/http.html')
def http():
    return render_template('http.html')

@app.route('/http_results', methods=['POST'])
def http_results():
    protname_seq = request.form['protname_seq']
    specie_seq = request.form['specie_seq']
    input_text = seqtools.httplink(protname_seq,specie_seq)
    return render_template('http_results.html', **locals())

seqtools我有一个具有以下功能的python代码:

def httplink(prot,specie):
    a=prot
    b='_'+specie
    c=print('http://www.uniprot.org/uniprot/?query=',a+b,'&sort=score')
    return c

现在在http_results.html 中我尝试了多种方法,但最终结果是None 或错误,所以我真的不知道如何继续。 http_results.html 代码是:

{% extends "layout.html" %}
{% block body %}
<div>
<p>The link to the protein is:</p>
<p><a href={{input_text}}>{{ protname_seq }}</a></p>
<br>
</div>
{% endblock %}

只需要链接到 UniProt 网页的蛋白质名称。感谢您的帮助,对不起我的英语。

【问题讨论】:

    标签: python flask


    【解决方案1】:

    print 函数返回None,因此,您需要将print 和赋值操作分成两行:

    c='http://www.uniprot.org/uniprot/?query='+a+b+'&sort=score'
    print(c)
    return c
    

    另外,您需要在a 标签中的href 周围添加引号:

    <p><a href='{{input_text}}'>{{ protname_seq }}</a></p>
    

    【讨论】:

      【解决方案2】:

      在名为httplink 的函数中,您没有为c 分配任何东西。您应该在 print 语句中返回字符串文字。

      def httplink(prot, specie):
          url = 'http://www.uniprot.org/uniprot/?query=%s_%s&sort=score'
          return url % (prot, specie)
      

      这个函数应该可以解决问题。

      【讨论】:

      • 感谢您的帮助!很抱歉不能给你们两个投票。
      • 呵呵没关系。很高兴你想通了。
      猜你喜欢
      • 2018-02-22
      • 1970-01-01
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 2020-10-26
      • 1970-01-01
      • 2021-06-23
      • 2021-07-25
      相关资源
      最近更新 更多