使用HTML调用python函数
我在python中有一个函数,它显示名称列表使用HTML调用python函数,python,flask,jinja2,Python,Flask,Jinja2,我在python中有一个函数,它显示名称列表 def search(): with open('business_ten.json') as f: data=f.read() jsondata=json.loads(data) for row in jsondata['rows']: #print row['text'] a=str(row['name']) print a return a
def search():
with open('business_ten.json') as f:
data=f.read()
jsondata=json.loads(data)
for row in jsondata['rows']:
#print row['text']
a=str(row['name'])
print a
return a
search()
我正在尝试使用Flask在HTML文件中调用此函数
{% extends "layout.html" %}
{% block content %}
<div class="jumbo">
<h2>Welcome to the Rating app<h2>
<h3>This is the home page for the Rating app<h3>
</div>
<body>
<p>{{ search.a }}</p>
</body>
{% endblock %}
有很多方法可以做到这一点: 1-您可以注册一个新的Jinja2筛选器 2-您可以将函数作为Jinja2参数传递(这一个更容易) 对于方法2:
@app.route('/crawl')
def crawl():
return render_template('crawl.html', myfunction=search)
在模板调用中,参数具有一个函数
{% extends "layout.html" %}
{% block content %}
<div class="jumbo">
<h2>Welcome to the Rating app<h2>
<h3>This is the home page for the Rating app<h3>
</div>
<body>
<p>{{ myfunction() }}</p>
</body>
{% endblock %}
{%extends“layout.html”%}
{%block content%}
欢迎使用评级应用程序
这是评级应用程序的主页
{{myfunction()}}
{%endblock%}
有很多方法可以做到这一点:
1-您可以注册一个新的Jinja2筛选器
2-您可以将函数作为Jinja2参数传递(这一个更容易)
对于方法2:
@app.route('/crawl')
def crawl():
return render_template('crawl.html', myfunction=search)
在模板调用中,参数具有一个函数
{% extends "layout.html" %}
{% block content %}
<div class="jumbo">
<h2>Welcome to the Rating app<h2>
<h3>This is the home page for the Rating app<h3>
</div>
<body>
<p>{{ myfunction() }}</p>
</body>
{% endblock %}
{%extends“layout.html”%}
{%block content%}
欢迎使用评级应用程序
这是评级应用程序的主页
{{myfunction()}}
{%endblock%}
1。请修正你的缩进。2.这不是从HTML(可能涉及Ajax)调用的问题,而是从Jinja2模板调用的问题。当你这么做的时候,我也会这么做。请修正你的缩进。2.这不是从HTML(可能涉及Ajax)调用的问题,而是从Jinja2模板调用的问题。我试过你的第二种方法,但仍然不起作用。我不明白你的搜索。您的代码(标识?)有问题。这是一个成功的例子,您可以将函数引用传递给Jinja2,然后在模板上调用它。若你们想让你们的html从服务器上获取json,这是以一种完全不同的方式完成的。我尝试了你们的第二种方法,但仍然不起作用。我不明白你的搜索。您的代码(标识?)有问题。这是一个成功的例子,您可以将函数引用传递给Jinja2,然后在模板上调用它。如果您想让html从服务器获取json,这是以完全不同的方式完成的。