在html文件中调用python函数 [英] Call a python function within a html file

查看:1926
本文介绍了在html文件中调用python函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



感谢

有没有办法在html页面中点击某个链接时调用python函数?您需要使用Web框架将请求路由到Python,因为您无法仅使用HTML来完成此任务。 Flask 是一个简单的框架:



server.py

strong>:


  from flask import Flask,render_template 
app = Flask(__ name__)

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

@ app.route('/ my-link /')
def my_link():
print'I got clicked!'

return'Click。'

if __name__ =='__main__ ':
app.run(debug = True)

模板/模板。 html

 <!doctype html> 

< title>测试< / title>
< meta charset = utf-8>

< a href =/ my-link />点击我< / a>

使用 python server.py 运行它,然后然后导航至 http:// localhost:5000 / 。开发服务器不安全,因此为了部署您的应用程序,请查看 http ://flask.pocoo.org/docs/0.10/quickstart/#deploying-to-a-web-server


Is there a way to call a python function when a certain link is clicked within a html page?

Thanks

解决方案

You'll need to use a web framework to route the requests to Python, as you can't do that with just HTML. Flask is one simple framework:

server.py:

from flask import Flask, render_template
app = Flask(__name__)

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

@app.route('/my-link/')
def my_link():
  print 'I got clicked!'

  return 'Click.'

if __name__ == '__main__':
  app.run(debug=True)

templates/template.html:

<!doctype html>

<title>Test</title> 
<meta charset=utf-8> 

<a href="/my-link/">Click me</a>

Run it with python server.py and then navigate to http://localhost:5000/. The development server isn't secure, so for deploying your application, look at http://flask.pocoo.org/docs/0.10/quickstart/#deploying-to-a-web-server

这篇关于在html文件中调用python函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆