有没有办法在 html 页面中单击某个链接时调用 python 函数?
谢谢
最佳答案
您需要使用网络框架将请求路由到 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)
templates/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
关于python - 在 html 文件中调用 python 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5615228/