python被html调用函数调用,调用一个html文件中的python函数

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

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

import cherrypy

class HelloWorld:

def index(self):

# Let's link to another method here.

return 'We have an important message 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结合其他几个模块和工具:

真子(模板库)

py2exe(转换成Windows可执行文件)

GccWinBinaries(结合使用py2exe)

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

你可能感兴趣的:(python被html调用函数调用,调用一个html文件中的python函数)