{"id":204474,"date":"2025-05-29T13:34:08","date_gmt":"2025-05-29T05:34:08","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204474\/"},"modified":"2025-05-29T13:34:08","modified_gmt":"2025-05-29T05:34:08","slug":"pytracetoix-%e5%a6%82%e4%bd%95%e5%9c%a8%e4%b8%8d%e7%a0%b4%e5%9d%8f%e8%ae%be%e8%ae%a1%e6%88%96%e4%bb%a3%e7%a0%81%e6%9b%b4%e6%94%b9%e7%9a%84%e6%83%85%e5%86%b5%e4%b8%8b%e8%b0%83%e8%af%95-jinjaemplates","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204474\/","title":{"rendered":"PyTraceToIX &#8211; \u5982\u4f55\u5728\u4e0d\u7834\u574f\u8bbe\u8ba1\u6216\u4ee3\u7801\u66f4\u6539\u7684\u60c5\u51b5\u4e0b\u8c03\u8bd5 Jinjaemplates\u3001Flask Web \u5e94\u7528\u7a0b\u5e8f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>PyTraceToIX &#8211; \u5982\u4f55\u5728\u4e0d\u7834\u574f\u8bbe\u8ba1\u6216\u4ee3\u7801\u66f4\u6539\u7684\u60c5\u51b5\u4e0b\u8c03\u8bd5 Jinjaemplates\u3001Flask Web \u5e94\u7528\u7a0b\u5e8f<\/h1>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300aPyTraceToIX &#8211; \u5982\u4f55\u5728\u4e0d\u7834\u574f\u8bbe\u8ba1\u6216\u4ee3\u7801\u66f4\u6539\u7684\u60c5\u51b5\u4e0b\u8c03\u8bd5 Jinjaemplates\u3001Flask Web \u5e94\u7528\u7a0b\u5e8f\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241108\/1731062386672dea7241c80.jpg\" class=\"aligncenter\"><\/p>\n<p>pytracetoix \u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\u8ddf\u8e2a\u5668\uff0c\u8bbe\u8ba1\u7528\u4e8e\u8c03\u8bd5 jinja2 \u6a21\u677f\u3001flask web \u5e94\u7528\u7a0b\u5e8f\u3001lambda\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u65b9\u6cd5\u94fe\u548c\u4e00\u822c\u8868\u8fbe\u5f0f\u3002<\/p>\n<p>\u4ee3\u7801\u7f16\u8f91\u5668\u901a\u5e38\u65e0\u6cd5\u5728\u6b64\u7c7b\u8868\u8fbe\u5f0f\u4e2d\u8bbe\u7f6e\u65ad\u70b9\uff0c\u8fd9\u9700\u8981\u5927\u91cf\u4ee3\u7801\u4fee\u6539\u624d\u80fd\u6709\u6548\u8c03\u8bd5\u3002<\/p>\n<p>\u5bf9\u4e8e jinja2 \u6a21\u677f\uff0c\u53ef\u4ee5\u4f7f\u7528\u8c03\u8bd5\u6269\u5c55\uff0c\u4f46\u5b83\u901a\u5e38\u4f1a\u8f6c\u50a8\u6574\u4e2a\u4e0a\u4e0b\u6587\uff0c\u4ece\u800c\u5f88\u96be\u9694\u79bb\u7279\u5b9a\u95ee\u9898\u3002 pytracetoix \u901a\u8fc7\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u8ddf\u8e2a\u7279\u5b9a\u6570\u636e\u5e76\u5c06\u5176\u76f4\u63a5\u5199\u5165 sys.stdout \u6216\u6d41\u6765\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u800c\u65e0\u9700\u66f4\u6539\u8bbe\u8ba1\u6216\u5bf9 web \u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u4efb\u4f55\u66f4\u6539\u3002<\/p>\n<p>\u6b64\u5916\uff0cpytracetoix \u53ef\u4ee5\u6355\u83b7\u591a\u4e2a\u8f93\u5165\u53ca\u5176\u7ed3\u679c\uff0c\u5c06\u5b83\u4eec\u5168\u90e8\u663e\u793a\u5728\u4e00\u884c\u4e2d\uff0c\u4ece\u800c\u66f4\u8f7b\u677e\u5730\u67e5\u770b\u805a\u5408\u6570\u636e\u548c\u8ddf\u8e2a\u503c\u6d41\u3002<\/p>\n<p>pytracetoix \u4e3a\u8fd9\u4e9b\u6311\u6218\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u7b80\u5316\u4e86\u8c03\u8bd5\uff0c\u540c\u65f6\u4fdd\u7559\u4e86\u539f\u59cb\u4ee3\u7801\u5e93\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<p>\u5b83\u7684\u8bbe\u8ba1\u5f88\u7b80\u5355\uff0c\u5177\u6709\u6613\u4e8e\u8bc6\u522b\u7684\u529f\u80fd\uff0c\u4e00\u65e6\u53d1\u73b0\u9519\u8bef\u5c31\u53ef\u4ee5\u5c06\u5176\u5220\u9664\u3002<\/p>\n<p>pytracetoix \u6709 2 \u4e2a\u4e3b\u8981\u529f\u80fd\uff1a<\/p>\n<ul>\n<li> c__ \u6355\u83b7\u8868\u8fbe\u5f0f\u8f93\u5165\u7684\u8f93\u5165\u3002\u4f8b\u5982\uff1ac__(x) <\/li>\n<li> d__ \u663e\u793a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u548c\u6240\u6709\u6355\u83b7\u7684\u8f93\u5165\u3002\u4f8b\u5982\uff1a d__(c__(x) + c__(y)) <\/li>\n<\/ul>\n<p>\u8fd8\u6709 2 \u4e2a\u53ef\u9009\u529f\u80fd\uff1a<\/p>\n<ul>\n<li> init__ \u521d\u59cb\u5316\u663e\u793a\u683c\u5f0f\u3001\u8f93\u51fa\u6d41\u548c\u591a\u7ebf\u7a0b\u3002<\/li>\n<li> t__ \u5b9a\u4e49\u5f53\u524d\u7ebf\u7a0b\u7684\u540d\u79f0\u3002<\/li>\n<\/ul>\n<pre>pip install pytracetoix\n<\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff1a<\/p>\n<ul>\n<li>flask web \u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528 jinja2 \u6a21\u677f<\/li>\n<li>\u5b83\u751f\u6210\u4e00\u4e2a\u8d2d\u7269\u5361 html \u8868\uff0c\u5176\u4e2d\u5305\u542b\u4ea7\u54c1\u3001\u6570\u91cf\u548c\u6700\u7ec8\u4ef7\u683c<\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<th>product<\/th>\n<th>qty<\/th>\n<th>final price<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>smartphone<\/td>\n<td>5<\/td>\n<td>2500<\/td>\n<\/tr>\n<tr>\n<td>wireless b<\/td>\n<td>50<\/td>\n<td>49960<\/td>\n<\/tr>\n<tr>\n<td>smartphone<\/td>\n<td>20<\/td>\n<td>1990<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\u4ea7\u54c1\u540d\u79f0\u53ea\u6709\u524d11\u4e2a\u5b57\u7b26\uff0c\u4f46\u6211\u4eec\u9700\u8981\u77e5\u9053\u5168\u540d\u3002<\/li>\n<li>\u4ec5\u663e\u793a\u6700\u7ec8\u4ef7\u683c\uff0c\u5373\u4ef7\u683c*\u6570\u91cf-\u6298\u6263\u3002<\/li>\n<li>\u6298\u6263\u53d6\u51b3\u4e8e\u6570\u91cf\u3002<\/li>\n<li> c__ \u6355\u83b7\u5b8c\u6574\u540d\u79f0\uff0c\u4f46\u4e0d\u66f4\u6539\u8bbe\u8ba1\u3002<\/li>\n<li> c__ \u6355\u83b7\u6570\u91cf\u5e76\u5c06\u5176\u6807\u8bb0\u4e3a\u6570\u91cf\u3002<\/li>\n<li> c__ \u6355\u83b7\u6298\u6263\u503c\u3002<\/li>\n<li> d__ \u5c06\u6240\u6709\u6355\u83b7\u7684\u8f93\u5165\u548c\u6700\u7ec8\u4ef7\u683c\u8f93\u51fa\u5230 sys.stdout\u3002<\/li>\n<\/ul>\n<p>\u6807\u51c6\u8f93\u51fa\u5c06\u663e\u793a\u8fd9\u4e9b\u884c\uff1a<\/p>\n<pre>i0:`smartphone 128gb` | qty:`5` | i2:`500` | discount:`0` | _:`2500`\ni0:`wireless bluetooth headphones` | qty:`50` | i2:`1000` | discount:`40` | _:`49960`\ni0:`smartphone 64gb black` | qty:`20` | i2:`100` | discount:`10` | _:`1990`\n<\/pre>\n<p>jinja2 \u6a21\u677f\uff1a<\/p>\n<pre>&lt;html lang=\"en\"&gt;\n&lt;head&gt;&lt;link href=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@5.3.0-alpha3\/dist\/css\/bootstrap.min.css\" rel=\"stylesheet\"&gt;&lt;\/head&gt;\n&lt;body&gt;\n    &lt;div class=\"container mt-5\"&gt;\n        &lt;h1&gt;shopping cart&lt;\/h1&gt;\n        &lt;table class=\"table table-striped\"&gt;\n            &lt;tr&gt;&lt;th&gt;product&lt;\/th&gt;&lt;th&gt;qty&lt;\/th&gt;&lt;th&gt;final price&lt;\/th&gt;&lt;\/tr&gt;\n            {% for item in purchases %}\n            {% set product = products[item['product']] %}\n            &lt;tr&gt;\n                &lt;td&gt;{{ c__(product['name'])[0:10] }}&lt;\/td&gt;\n                &lt;td&gt;{{ c__(item['qty'], name='qty') }}&lt;\/td&gt;\n                &lt;td&gt;{{ d__(c__(product['price']) * item['qty']\n                    - c__(discount(item['qty']), name='discount')) }}&lt;\/td&gt;\n            &lt;\/tr&gt;\n            {% endfor %}\n        &lt;\/table&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<p>app.py:<\/p>\n<pre>from flask import flask, render_template\nfrom pytracetoix import c__, d__\n\napp = flask(__name__)\n\napp.jinja2_env.globals['d__'] = d__\napp.jinja2_env.globals['c__'] = c__\n\ndiscounts = {50: 40, 20: 10, 10: 5, 0: 0}\nproducts = {\n    'wb50cc': {'name': 'wireless bluetooth headphones', 'price': 1000},\n    'ph20xx': {'name': 'smartphone 128gb', 'price': 500},\n    'ph50yy': {'name': 'smartphone 64gb black', 'price': 100}\n}\n\npurchases = [\n    {'product': 'ph20xx', 'qty': 5},\n    {'product': 'wb50cc', 'qty': 50},\n    {'product': 'ph50yy', 'qty': 20}\n]\n\n\ndef discount(qty): return next((k, v) for k, v in discounts.items() if k &lt;= qty)[1]\n\n\n@app.route('\/', methods=['get'])\ndef index():\n    return render_template('index.html', products=products, purchases=purchases, discount=discount)\n\n\nif __name__ == '__main__':\n    app.run(debug=true)\n<\/pre>\n<p>\u5982\u524d\u9762\u7684\u793a\u4f8b\uff0c\u6211\u4eec\u5c06 c__ \u6dfb\u52a0\u5230 app.py \u4e0a\u7684\u6298\u6263\u51fd\u6570\u4e2d\uff1a<\/p>\n<pre>def discount(qty): return c__(next((k, v) for k, v in discounts.items() if k &lt;= qty))[1]\n<\/pre>\n<p>\u5b83\u5c06\u5728\u8f93\u51fa\u4e2d\u6dfb\u52a0\u66f4\u4e30\u5bcc\u7684\u6298\u6263\u4fe1\u606f\uff1a<\/p>\n<pre>i0:`Smartphone 128GB` | qty:`5` | i2:`500` | i3:`(0, 0)` | discount:`0` | _:`2500`\ni0:`Wireless Bluetooth Headphones` | qty:`50` | i2:`1000` | i3:`(50, 40)` | discount:`40` | _:`49960`\ni0:`Smartphone 64GB Black` | qty:`20` | i2:`100` | i3:`(20, 10)` | discount:`10` | _:`1990`\n<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPyTraceToIX &#8211; \u5982\u4f55\u5728\u4e0d\u7834\u574f\u8bbe\u8ba1\u6216\u4ee3\u7801\u66f4\u6539\u7684\u60c5\u51b5\u4e0b\u8c03\u8bd5 Jinjaemplates\u3001Flask Web \u5e94\u7528\u7a0b\u5e8f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PyTraceToIX &#038;#82&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204474","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204474","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204474"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204474\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}