【问题标题】:Call a python function within a html file在 html 文件中调用 python 函数
【发布时间】:2011-04-10 22:47:30
【问题描述】:

有没有办法在html页面中点击某个链接时调用python函数?

谢谢

【问题讨论】:

    标签: python html


    【解决方案1】:

    您需要使用 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)
    

    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

    【讨论】:

    • 你能用瓶子做这个吗?
    • @Temere:语法应该完全相同。我认为render_template 只是名称不同。
    • 只创建这两个文件是行不通的。还有更多工作要做吗(创建项目)?
    • @PraveshJain:把app.run()改成app.run(debug=True)应该更清楚了。
    • @PraveshJain:哦,如果我不清楚,抱歉。您需要将template.html 放入templates 文件夹中。
    【解决方案2】:

    是的,但不是直接的;您可以设置onclick 处理程序来调用一个JavaScript 函数,该函数将构造一个XMLHttpRequest 对象并向您服务器上的页面发送请求。反过来,您服务器上的该页面可以使用 Python 实现并执行它需要执行的任何操作。

    【讨论】:

      【解决方案3】:

      是的。如果链接指向您的 Web 服务器,那么您可以设置您的 Web 服务器以在单击该链接时运行任何类型的代码,并将该代码的结果返回给用户的浏览器。有很多方法可以编写这样的 Web 服务器。例如,请参阅Django。您可能还想使用 AJAX。

      如果您想在用户的浏览器中运行代码,请使用 Javascript。

      【讨论】:

        【解决方案4】:

        有几种方法可以做到这一点,但最适合我的一种是使用 CherryPy。 CherryPy 是一个极简的 Python Web 框架,允许您在任何计算机上运行小型服务器。 stackoverflow - Using the browser for desktop UI 上有一个与您非常相似的问题。

        下面的代码会做你想做的事。它来自 CherryPy 教程的示例 2。

        import cherrypy
        
        class HelloWorld:
        
            def index(self):
                # Let's link to another method here.
                return 'We have an <a href="showMessage">important message</a> for you!'
            index.exposed = True
        
            def showMessage(self):
                # Here's the important message!
                return "Hello world!"
            showMessage.exposed = True
        
        import os.path
        tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf')
        
        if __name__ == '__main__':
            # CherryPy always starts with app.root when trying to map request URIs
            # to objects, so we need to mount a request handler root. A request
            # to '/' will be mapped to HelloWorld().index().
            cherrypy.quickstart(HelloWorld(), config=tutconf)
        else:
            # This branch is for the test suite; you can ignore it.
            cherrypy.tree.mount(HelloWorld(), config=tutconf)
        

        我个人将 CherryPy 与其他几个模块和工具结合使用:

        • Mako(模板库)
        • py2exe(转换成Windows可执行文件)
        • GccWinBinaries(与 py2exe 结合使用)

        我写了一篇关于Browser as Desktop UI with CherryPy 的文章,介绍了使用的模块和工具以及一些可能有帮助的进一步链接。

        【讨论】:

          【解决方案5】:

          除了在服务器上运行 Python 脚本外,您还可以使用 Skulpt 在客户端运行 Python 脚本。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-01-09
            • 2013-12-25
            • 1970-01-01
            • 1970-01-01
            • 2020-04-04
            • 2019-04-08
            • 2018-01-01
            相关资源
            最近更新 更多