python - 使用 html 运行 python

标签 python html

我很难做到这一点。 每次我运行“查看照片”按钮时,它显示的都是 python 脚本,而不是应用程序本身。

我正在尝试运行下面的 Python 脚本:

from os.path import join, dirname, exists
from pymt import *
current_dir = dirname(__file__)
fontname_title=join(current_dir,'fonts','7.ttf')
fontname_author_desc=join(current_dir,'fonts','author_desc.ttf')

css='''
.desktop-background,
.desktop-coverflow {
draw-background: 1;
bg-color: #000000;
}
.desktop-author,
.desktop-description {
color: #999999;
}
.desktop-title {
font-size: 60;
}
'''


css_add_sheet(css)

class Desktop(MTBoxLayout):
layout_def = '''
<MTBoxLayout orientation='"vertical"' cls='"desktop-background"'>
    <MTCoverFlow size_hint='(1, .7)' cls='"desktop-coverflow"'
        thumbnail_size='(256, 256)' cover_distance='150' id='"coverflow"'/>
    <MTAnchorLayout size_hint='(1, .3)'>
        <MTBoxLayout cls='"form"' padding='20' orientation='"vertical"'>
            <MTLabel id='"title"' label='"Unknown Title"' autosize='True'
                cls='"desktop-title"' anchor_x='"center"'/>
            <MTLabel id='"author"' label='"Unknown Author"' autosize='True'
                cls='"desktop-author"' anchor_x='"center"'/>
            <MTLabel id='"description"' label='"Unknown Description"' autosize='True'
                cls='"desktop-description"' anchor_x='"center"'/>
        </MTBoxLayout>
    </MTAnchorLayout>
</MTBoxLayout>
'''

def __init__(self, **kwargs):
    super(Desktop, self).__init__(**kwargs)
    self.xml = xml = XMLWidget(xml=Desktop.layout_def)
    self.xml.autoconnect(self)
    self.add_widget(self.xml.root)
    self.coverflow = xml.getById('coverflow')
    self.title = xml.getById('title')
    self.author = xml.getById('author')
    self.description = xml.getById('description')
    self.title.font_name=fontname_title
    self.author.font_name=fontname_author_desc
    self.description.font_name=fontname_author_desc
    self.populate()



def populate(self):
    # search plugins
    self.plugins = plugins = MTPlugins(plugin_paths=[
        join(current_dir, 'app')])
    plugins.search_plugins()

    # populate the coverflow with plugin list
    first_entry = None
    for key in plugins.list():
        plugin = plugins.get_plugin(key)
        infos = plugins.get_infos(plugin)

        icon = None
        for icon_filename in ('icon-large.png', 'icon-large.jpg',
                              infos['icon'], 'icon.png'):
            icon = join(infos['path'], icon_filename)
            if exists(icon):
                break
            icon = None

        # no icon ?
        if icon is None:
            print 'No icon found for', infos['title']
            continue

        # create an image button for every plugin
        button = MTImageButton(filename=icon)
        if first_entry is None:
            first_entry = button
        button.infos = infos
        button.plugin = plugin
        self.coverflow.add_widget(button)

    # display first entry
    if first_entry:
        self.show_plugin(first_entry)

def on_coverflow_change(self, widget):
    '''Called when the coverflow widget is changed
    '''
    self.show_plugin(widget)

def on_coverflow_select(self, widget):
    '''Called when the coverflow widget have a selection
    '''
    plugin = widget.plugin
    win = self.parent
    self.plugins.activate(plugin, self.parent)
    btn_close = MTImageButton(filename=join(current_dir,'icons','home.png'))
    btn_close.connect('on_release', curry(
            self.on_plugin_close, self.parent, plugin))
    self.parent.add_widget(btn_close)
    self.parent.remove_widget(self)

def on_plugin_close(self, win, plugin, *largs):
    '''Called when the close button is hitted
    '''
    self.plugins.deactivate(plugin, win)
    win.children.clear()
    win.add_widget(self)

def show_plugin(self, widget):
    '''Show information about a plugin in the container
    '''
    self.title.label = widget.infos['title']
    self.author.label = widget.infos['author']
    self.description.label = widget.infos['description']

if __name__ == '__main__':
runTouchApp(Desktop())`

使用下面的按钮

<html>
        <div class="art-blockcontent">
    <p style="text-align: center;"><img width="176" height="132" alt="" src="images/boat.jpg"></p>
<p style="text-align: center;">&nbsp;
<a href="Python\pictures\malacca.py" class="art-button">View Photo</a>&nbsp;<br></p></div>
</html>

最佳答案

由于您的 python 文件的 URL 中有反斜杠,我将猜测您做错了什么。

Python 不会在您的网络浏览器中运行。因此,如果您直接从硬盘加载 HTML 文件(即使用 file:// URL),那么浏览器将只显示您的源代码。

您需要设置一个 Web 服务器来运行 python。如何让服务器运行 python 代码取决于服务器。您可能想要重写您的 python 代码。它似乎是按照 CGI 标准编写的,而该标准实际上已被放弃多年。每次请求一个页面并“打印”HTML 时,CGI 都会运行一个程序——因为每次它都会启动一个新进程,速度非常慢。现代系统加载代码一次,然后在每次请求页面时调用代码中的方法,该方法返回 HTML,或者更一般地说,返回包含 HTML 和元数据的“响应对象”。

我建议查看Flaskcherrypy作为简单的起点。

关于python - 使用 html 运行 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15803871/

相关文章:

python - 如何从 python 程序生成多个 python 脚本?

html - 哪种 PHP 模板引擎语法最适合使用?

javascript - 如何修复错误 "reference to entity "orderby“必须以 ';' 分隔符结尾”?

javascript - HTML JS 在 DIV 中附加随机图像

python - 比较 R 中等效的键值对

python - 使用带有不正确标签的 Python ElementTree 解析 XML

python - 使用 scipy 的 SmoothSphereBivariateSpline 进行插值

javascript - 字符转义: from Python string literal to JSON and then to HTML

javascript - 使用touchmove触发悬停事件

python - 从 Weekday int 获取日期名称