+-

有没有办法在html页面中单击某个链接时调用 python函数?
谢谢
最佳答案
您需要使用Web框架将请求路由到 Python,因为您不能仅使用HTML. Flask是一个简单的框架:
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)
模板/ template.html:
<!doctype html>
<title>Test</title>
<meta charset=utf-8>
<a href="/my-link/">Click me</a>
使用python server.py运行它,然后导航到http://localhost:5000/.开发服务器不安全,因此要部署应用程序,请查看http://flask.pocoo.org/docs/0.10/quickstart/#deploying-to-a-web-server
点击查看更多相关文章
转载注明原文:在html文件中调用python函数 - 乐贴网