用python脚本硬编码html代码的正确方法?

问题描述 投票:1回答:1

我已经开发了一个基于Web的工具,目前正在尝试使其可通过python启动。我认为使用CEFpython可能是这样做的方法。我按照教程here编写了以下代码:

from cefpython3 import cefpython as cef
import base64
import platform
import sys
import threading
import os

HTML_code = """
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <link href="static/css/main.css" rel="stylesheet" />
    </head>
    <body>
        <div id="UI">
        </div>
        <div id="container"></div>

        <script src="static/main.js"></script>
        <script type="text/javascript">
            function defineData(datainput){

                console.log("start")
                data = datainput;
                var loc = window.location.pathname;
                var dir = loc.substring(0, loc.lastIndexOf('/'));
                console.log(loc);
                console.log(dir);
                Main();
            }
        </script>

    </body>
</html>

"""
def html_to_data_uri(html):
    html = html.encode("utf-8", "replace")
    b64 = base64.b64encode(html).decode("utf-8", "replace")
    ret = "data:text/html;base64,{data}".format(data=b64)
    return ret


def main(config):

    sys.excepthook = cef.ExceptHook  # To shutdown all CEF processes on error
    settings = {}
    cef.Initialize(settings=settings)

    browser = cef.CreateBrowserSync(url=html_to_data_uri(HTML_code),window_title="Test")

    browser.SetClientHandler(LoadHandler(config))
    cef.MessageLoop()
    cef.Shutdown()
    return 

class LoadHandler(object):

    def __init__(self, config):
        self.config = config
    def OnLoadingStateChange(self, browser, is_loading, **_):
        """Called when the loading state has changed."""
        if not is_loading:
            # Loading is complete. DOM is ready.
            browser.ExecuteFunction("defineData", self.config)

不幸的是,与本教程不同,我的工具必须加载定义了主要功能的本地.js文件(),而且如果我以这种方式编写html文件,似乎我的工作目录实际上不是我调用脚本的目录,但是有些奇怪的地方

这些行的输出是:

var loc = window.location.pathname;
var dir = loc.substring(0, loc.lastIndexOf('/'));
console.log(loc);
console.log(dir);

输出:

text/html;base64,CjwhRE9DVFlQRSBodG1sPgo8aHRtbCBsYW5nPSJlbiI+Cgk8aGVhZD4KCQk8bWV0YSBjaGFyc2V0PSJ1dGYtOCI+CgkJPG1ldGEgbmFtZT0idmlld3BvcnQiIGNvbnRlbnQ9IndpZHRoPWRldmljZS13aWR0aCwgdXNlci1zY2FsYWJsZT1ubywgbWluaW11bS1zY2FsZT0xLjAsIG1heGltdW0tc2NhbGU9MS4wIj4KCQk8bGluayBocmVmPSJzdGF0aWMvY3NzL21haW4uY3NzIiByZWw9InN0eWxlc2hlZXQiIC8+CgkJPHN0eWxlIHR5cGU9InRleHQvY3NzIj4KCQkJKiB7CgkJCQkuYm9yZGV....

text

[您能帮我找到在python中用正确路径对html代码进行硬编码的正确方法吗?也许我需要以某种方式设置路径?

PS:我确实尝试将html代码包含在单独的.html文件中,并且该代码在Windows计算机上可以运行,但是MacOS似乎不喜欢它。由于本教程确实可以在MAC上运行,因此我尝试将html部分硬编码为python脚本,并希望它在Windows和Mac上均可使用

python html data-uri cefpython
1个回答
0
投票

[好,HTML文档已由data URI转换为html_to_data_uri的正文,因此文档的U [niversal] R [esource] L [ocator](window.location)不是位置在服务器上,但是数据URI本身(您提到的“奇怪的地方”)。

请记住,URL是URI的子集,您通过以下方式将URI作为URL传递给CEF:

    browser = cef.CreateBrowserSync(url=html_to_data_uri(HTML_code),window_title="Test")

因此,只要您使用的是数据URI / URL,我认为window.location不会有帮助。相反,您可以将HTML代码提取到单独的.html文件中,并将该行更改为:

    browser = cef.CreateBrowserSync(url="/path/to/that_html_file.html", window_title="Test")
© www.soinside.com 2019 - 2024. All rights reserved.