{"id":179,"date":"2018-09-12T14:08:13","date_gmt":"2018-09-12T06:08:13","guid":{"rendered":"http:\/\/www.yinyubo.cn\/?p=179"},"modified":"2021-08-13T09:35:22","modified_gmt":"2021-08-13T01:35:22","slug":"jinja2%e4%b8%8d%e5%85%bc%e5%ae%b9pyinstaller%e6%89%93%e5%8c%85exe%e7%a8%8b%e5%ba%8f%e7%9a%84%e8%a7%a3%e5%86%b3%e5%8a%9e%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=179","title":{"rendered":"jinja2\u4e0d\u517c\u5bb9pyinstaller\u6253\u5305exe\u7a0b\u5e8f\u7684\u89e3\u51b3\u529e\u6cd5"},"content":{"rendered":"<p>jinja2\u662fpython\u4e0b\u7684\u4e00\u4e2a\u6839\u636ehtml\u6a21\u677f\uff0c\u4ea7\u751fhtml\u6587\u4ef6\u7684\u5e93\u3002\u5728jinja2\u5b98\u65b9\u6587\u6863\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528PackageLoader\u7684\u65b9\u5f0f\uff0c\u4ea7\u751fhtml\u6587\u4ef6\uff0c\u4f46\u662f\u8fd9\u4e2a\u65b9\u5f0f\u5374\u4e0epyinstaller\u51b2\u7a81\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u4fee\u6539\u6210FileSystemLoader\u7684\u65b9\u5f0f\u6765\u89e3\u51b3\u6253\u5305\u6587\u4ef6\u7684\u95ee\u9898\u3002<\/p>\n<h6>\u4e0d\u517c\u5bb9pyinstall\u7684\u8001\u4ee3\u7801\uff08PackageLoader\u4ea7\u751fHTML\u6587\u4ef6\uff09<\/h6>\n<pre class=\"\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">#\u4f7f\u7528PackageLoader\u4ea7\u751fhtml\u6587\u4ef6\ndef create_defect_html(defectlist, fileds, reportname=None):\n    \"\"\"\n    create html defects report in \".\/reports\" directory\n    :param defectlist:\u4f20\u5165\u95ee\u9898\u5355\u5217\u8868\n    :param fileds:\u4f20\u5165\u95ee\u9898\u5355\u5173\u952e\u5b57\u5217\u8868\n    :param reportname:\u4f20\u5165\u62a5\u544a\u6587\u4ef6\u540d\u79f0\uff0c\u82e5\u6709\u9700\u8981\u53ef\u4ee5\u6307\u5b9a\u751f\u6210\u7684html\u62a5\u544a\u540d\u79f0\uff1b\u82e5\u4e0d\u4f20\uff0c\u5219\u9ed8\u8ba4\u4e3aDefectList.html\n    :return:\u65e0\u8fd4\u56de\uff0c\u5728reports\u76ee\u5f55\u4e0b\u751f\u6210html\u62a5\u544a\n    \"\"\"\n    mycolumnlist = col_transform(fileds)\n    env = Environment(loader = PackageLoader('TAReport', 'templates'))\n    template = env.get_template('DefectTemplate.html')\n    if reportname:\n        filestr = '.\/reports\/' + reportname + '.html'\n    else:\n        filestr = '.\/reports\/DefectList.html'\n    with open(filestr, 'w+') as f:\n        f.write(str(template.render(defectlist=defectlist, columnlist=mycolumnlist)))\n<\/code><\/pre>\n<h6>\u4f7f\u7528FileSystemLoader\uff0c\u80fd\u591f\u6b63\u786e\u4f7f\u7528pyinstaller\u6253\u5305\u7684\u65b0\u4ee3\u7801<\/h6>\n<pre class=\"\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">from jinja2 import Environment, PackageLoader, FileSystemLoader\ndef create_defect_html(defectlist, fileds, reportname=None):\n    \"\"\"\n    create html defects report in \".\/reports\" directory\n    :param defectlist:\u4f20\u5165\u95ee\u9898\u5355\u5217\u8868\n    :param fileds:\u4f20\u5165\u95ee\u9898\u5355\u5173\u952e\u5b57\u5217\u8868\n    :param reportname:\u4f20\u5165\u62a5\u544a\u6587\u4ef6\u540d\u79f0\uff0c\u82e5\u6709\u9700\u8981\u53ef\u4ee5\u6307\u5b9a\u751f\u6210\u7684html\u62a5\u544a\u540d\u79f0\uff1b\u82e5\u4e0d\u4f20\uff0c\u5219\u9ed8\u8ba4\u4e3aDefectList.html\n    :return:\u65e0\u8fd4\u56de\uff0c\u5728reports\u76ee\u5f55\u4e0b\u751f\u6210html\u62a5\u544a\n    \"\"\"\n    mycolumnlist = col_transform(fileds)\n    template_file_name = \"DefectTemplate.html\"\n    template_file_path = resource_path('resource\/templates', template_file_name)\n    template_file_directory = os.path.dirname(template_file_path)\n    template_loader = FileSystemLoader(searchpath=template_file_directory)\n    env = Environment(loader=template_loader)\n    template = env.get_template(template_file_name)\n    if reportname:\n        filestr = os.path.abspath(os.getcwd() + \".\/resource\/reports\/\" + reportname + '.html')\n        # filestr = '.\/resource\/reports\/' + reportname + '.html'\n    else:\n        filestr = os.path.abspath(os.getcwd() + \".\/resource\/reports\/DefectList.html\")\n        # filestr = '.\/resource\/reports\/DefectList.html'\n    html = template.render(defectlist=defectlist, columnlist=mycolumnlist)\n    with open(filestr, 'w') as f:\n            f.write(html)\ndef resource_path(relative_path, file_name):\n    \"\"\" Get absolute path to resource, works for both in IDE and for PyInstaller \"\"\"\n    # PyInstaller creates a temp folder and stores path in sys._MEIPASS\n    # In IDE, the path is os.path.join(base_path, relative_path, file_name)\n    # Search in Dev path first, then MEIPASS\n    base_path = os.path.abspath(\".\")\n    dev_file_path = os.path.join(base_path, relative_path, file_name)\n    if os.path.exists(dev_file_path):\n        return dev_file_path\n    else:\n        base_path = sys._MEIPASS\n        file_path = os.path.join(base_path, file_name)\n        if not os.path.exists(file_path):\n            msg = \"\\nError finding resource in either {} or {}\".format(dev_file_path, file_path)\n            print(msg)\n            return None\n        return file_path<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>jinja2\u662fpython\u4e0b\u7684\u4e00\u4e2a\u6839\u636ehtml\u6a21\u677f\uff0c\u4ea7\u751fhtml\u6587\u4ef6\u7684\u5e93\u3002\u5728jinja2\u5b98\u65b9\u6587\u6863\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528P [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-179","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=179"}],"version-history":[{"count":1,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/179\/revisions"}],"predecessor-version":[{"id":948,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/179\/revisions\/948"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}