{"id":301519,"date":"2024-05-20T18:10:29","date_gmt":"2024-05-20T10:10:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/301519.html"},"modified":"2024-05-20T18:10:52","modified_gmt":"2024-05-20T10:10:52","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e8%87%aa%e5%b8%a6%e7%9a%84idle%e4%b8%ad%e5%90%af%e5%8a%a8flask","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/301519.html","title":{"rendered":"\u5982\u4f55\u5728python\u81ea\u5e26\u7684idle\u4e2d\u542f\u52a8flask"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26213654\/564e71c1-94a0-49fc-883e-d113e5f48f90.webp\" alt=\"\u5982\u4f55\u5728python\u81ea\u5e26\u7684idle\u4e2d\u542f\u52a8flask\" \/><\/p>\n<p><p>\u5728Python\u81ea\u5e26\u7684IDLE\u4e2d\u542f\u52a8Flask\uff0c\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<strong>\u5b89\u88c5Flask\u3001\u521b\u5efaFlask\u5e94\u7528\u3001\u8fd0\u884cFlask\u5e94\u7528<\/strong>\u3002\u9996\u5148\uff0c\u786e\u4fdd\u60a8\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u3002\u63a5\u7740\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528pip\u5b89\u88c5Flask\u3002\u521b\u5efaFlask\u5e94\u7528\u7684\u8fc7\u7a0b\u4e2d\uff0c\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2aPython\u6587\u4ef6\uff0c\u5728\u8be5\u6587\u4ef6\u4e2d\u5bfc\u5165Flask\u5e93\uff0c\u5e76\u5b9a\u4e49\u60a8\u7684Web\u5e94\u7528\u3002\u6700\u540e\uff0c\u901a\u8fc7\u5728IDLE\u4e2d\u8fd0\u884cPython\u6587\u4ef6\u6765\u542f\u52a8\u60a8\u7684Flask\u5e94\u7528\u3002<strong>\u5b89\u88c5Flask\u662f\u8fd9\u4e00\u8fc7\u7a0b\u4e2d\u7684\u9996\u8981\u6b65\u9aa4<\/strong>\uff0c\u56e0\u4e3a\u6ca1\u6709Flask\u5e93\uff0c\u60a8\u5c06\u65e0\u6cd5\u521b\u5efa\u548c\u8fd0\u884cFlask\u5e94\u7528\u3002\u5b89\u88c5Flask\u901a\u5e38\u53ea\u9700\u8981\u6267\u884c\u4e00\u6761\u7b80\u5355\u7684pip\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5b89\u88c5FLASK<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u521b\u5efaFlask\u5e94\u7528\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fddFlask\u5e93\u5df2\u7ecf\u88ab\u5b89\u88c5\u5728\u60a8\u7684\u73af\u5883\u4e2d\u3002\u5728Python\u7684IDLE\u73af\u5883\u4e2d\u5b89\u88c5Flask\u901a\u5e38\u9700\u8981\u901a\u8fc7\u547d\u4ee4\u884c\u6765\u5b8c\u6210\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u547d\u4ee4\u884c\u754c\u9762\u3002<\/li>\n<li>\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5Flask\uff1a<code>pip install Flask<\/code>\u3002\u8fd9\u6761\u547d\u4ee4\u4f1a\u4ecePython\u7684\u5305\u7ba1\u7406\u5668PyPI\u4e2d\u4e0b\u8f7d\u5e76\u5b89\u88c5Flask\u53ca\u5176\u4f9d\u8d56\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728Python\u73af\u5883\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528Flask\u4e86\u3002<\/li>\n<\/ol>\n<p><p>\u6ce8\u610f\uff1a\u5982\u679c\u60a8\u6b63\u5728\u4f7f\u7528Python\u7684\u865a\u62df\u73af\u5883\uff0c\u8bf7\u786e\u4fdd\u60a8\u5df2\u7ecf\u6fc0\u6d3b\u76f8\u5e94\u7684\u865a\u62df\u73af\u5883\uff0c\u7136\u540e\u518d\u6267\u884c\u5b89\u88c5\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u521b\u5efaFLASK\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u521b\u5efaFlask\u5e94\u7528\u57fa\u672c\u4e0a\u6d89\u53ca\u5230\u7f16\u5199Python\u811a\u672c\uff0c\u8fd9\u4e9b\u811a\u672c\u5b9a\u4e49\u4e86\u5e94\u7528\u7684\u8def\u7531\u548c\u89c6\u56fe\u51fd\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\uff0c\u4f8b\u5982<code>app.py<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u9996\u5148\u5bfc\u5165Flask\u6a21\u5757\uff1a<code>from flask import Flask<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2aFlask\u5e94\u7528\u5b9e\u4f8b\uff1a<code>app = Flask(__name__)<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u5b9a\u4e49\u8def\u7531\u548c\u89c6\u56fe\u51fd\u6570\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#039;\/&#039;)<\/p>\n<p>def home():<\/p>\n<p>    return &#039;Hello, Flask!&#039;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u6839\u8def\u7531\uff0c\u5f53\u8bbf\u95ee\u5e94\u7528\u7684\u4e3b\u9875\u65f6\uff0c\u5b83\u4f1a\u8fd4\u56de\u201cHello, Flask!\u201d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h2>\u4e09\u3001\u8fd0\u884cFLASK\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u4e00\u65e6\u60a8\u521b\u5efa\u4e86Flask\u5e94\u7528\uff0c\u63a5\u4e0b\u6765\u60a8\u9700\u8981\u5728IDLE\u4e2d\u8fd0\u884c\u8fd9\u4e2a\u5e94\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u6253\u5f00IDLE\uff0c\u901a\u8fc7<code>File<\/code> &gt; <code>Open<\/code>\u627e\u5230\u5e76\u6253\u5f00\u60a8\u521b\u5efa\u7684<code>app.py<\/code>\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u5728IDLE\u4e2d\u8fd0\u884c\u8fd9\u4e2a\u6587\u4ef6\uff0c\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u6309<code>F5<\/code>\u5feb\u6377\u952e\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u5728<code>app.py<\/code>\u6587\u4ef6\u7684\u6700\u540e\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\u6765\u542f\u52a8Flask\u5e94\u7528\u7684Web\u670d\u52a1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if __name__ == &#039;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#039;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u542f\u52a8\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\u5668\uff0c\u5e76\u4e14\u56e0\u4e3a\u8bbe\u7f6e\u4e86<code>debug=True<\/code>\uff0c\u4efb\u4f55\u4ee3\u7801\u6539\u52a8\u90fd\u4f1a\u81ea\u52a8\u91cd\u8f7d\uff0c\u65b9\u4fbf\u5f00\u53d1\u548c\u8c03\u8bd5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h2>\u56db\u3001\u8c03\u8bd5\u4e0e\u6d4b\u8bd5<\/h2>\n<\/p>\n<p><p>\u5728Flask\u5e94\u7528\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u8c03\u8bd5\u548c\u6d4b\u8bd5\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u90e8\u5206\u3002<strong>\u4f7f\u7528IDLE\u8fdb\u884cFlask\u5f00\u53d1\u65f6\uff0c\u5229\u7528\u5176\u4ea4\u4e92\u5f0f\u73af\u5883\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u4ee3\u7801\u6d4b\u8bd5\u548c\u8c03\u8bd5<\/strong>\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5229\u7528IDLE\u7684\u76f4\u89c2\u754c\u9762\uff0c\u60a8\u53ef\u4ee5\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u68c0\u67e5\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u53ca\u65f6\u53d1\u73b0\u95ee\u9898\u6240\u5728\u3002<\/li>\n<li>\u5728\u5f00\u53d1Flask\u5e94\u7528\u65f6\uff0c\u5efa\u8bae\u9891\u7e41\u8fd0\u884c\u5e94\u7528\u5e76\u901a\u8fc7\u8bbf\u95ee\u5e94\u7528\u7684URL\u6d4b\u8bd5\u5404\u4e2a\u8def\u7531\u7684\u529f\u80fd\u662f\u5426\u7b26\u5408\u9884\u671f\u3002<\/li>\n<\/ol>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u542f\u52a8Flask\u5e94\u7528\u5e76\u4e0d\u590d\u6742\uff0c\u9075\u5faa\u4e0a\u8ff0\u6b65\u9aa4\u5373\u53ef\u5728Python\u81ea\u5e26\u7684IDLE\u4e2d\u6210\u529f\u8fd0\u884cFlask\u5e94\u7528\u3002\u4ece\u5b89\u88c5Flask\u5230\u521b\u5efa\u548c\u8fd0\u884cFlask\u5e94\u7528\uff0c\u6bcf\u4e2a\u6b65\u9aa4\u90fd\u6709\u5176\u91cd\u8981\u6027\uff0c\u503c\u5f97\u5f00\u53d1\u8005\u4eec\u7ec6\u5fc3\u5bf9\u5f85\u3002\u5c24\u5176\u662f\u5b89\u88c5Flask\u8fd9\u4e00\u6b65\uff0c\u4e3a\u540e\u7eed\u7684\u5f00\u53d1\u5de5\u4f5c\u5960\u5b9a\u4e86\u57fa\u7840\u3002\u6b63\u5982\u524d\u6240\u8ff0\uff0c<strong>\u5b89\u88c5Flask\u662f\u5f00\u59cbFlask\u5e94\u7528\u5f00\u53d1\u7684\u5173\u952e\u4e00\u6b65<\/strong>\uff0c\u5b83\u786e\u4fdd\u4e86\u5f00\u53d1\u73af\u5883\u4e2d\u5df2\u7ecf\u5177\u5907\u4e86\u521b\u5efa\u548c\u8fd0\u884cFlask\u5e94\u7528\u6240\u9700\u7684\u4e00\u5207\u6761\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728Python\u81ea\u5e26\u7684IDLE\u4e2d\u5b89\u88c5Flask\uff1f<\/strong><\/p>\n<p>\u5728Python\u81ea\u5e26\u7684IDLE\u4e2d\u5b89\u88c5Flask\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ea\u9700\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>\u6253\u5f00Python\u7684IDLE\u3002<\/li>\n<li>\u5728\u83dc\u5355\u680f\u4e2d\u9009\u62e9\u201c\u6587\u4ef6\u201d -&gt; \u201c\u65b0\u6587\u4ef6\u201d\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\u3002<\/li>\n<li>\u5728\u65b0\u6587\u4ef6\u4e2d\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\u5e76\u4fdd\u5b58\u4e3a<code>.py<\/code>\u6587\u4ef6\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-python\">pip install flask\n<\/code><\/pre>\n<ul>\n<li>\u7136\u540e\u5728IDLE\u4e2d\u9009\u62e9\u201c\u8fd0\u884c\u201d -&gt; \u201c\u8fd0\u884c\u6a21\u5757\u201d\u6216\u4f7f\u7528\u5feb\u6377\u952e<code>F5<\/code>\u8fd0\u884c\u8fd9\u4e2aPython\u6587\u4ef6\u3002<\/li>\n<li>\u8fd9\u5c06\u81ea\u52a8\u5b89\u88c5Flask\u5e93\u3002<\/li>\n<\/ul>\n<p><strong>2. \u5982\u4f55\u5728Python\u7684IDLE\u4e2d\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\u7a0b\u5e8f\uff1f<\/strong><\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u5728Python\u7684IDLE\u4e2d\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n<pre><code class=\"language-python\">from flask import Flask\n\napp = Flask(__name__)\n\n@app.route(&#039;\/&#039;)\ndef hello_world():\n    return &#039;Hello, World!&#039;\n\nif __name__ == &#039;__main__&#039;:\n    app.run()\n<\/code><\/pre>\n<ul>\n<li>\u5c06\u4e0a\u8ff0\u4ee3\u7801\u4fdd\u5b58\u4e3a<code>.py<\/code>\u6587\u4ef6\u3002<\/li>\n<li>\u5728IDLE\u4e2d\u8fd0\u884c\u8fd9\u4e2a\u6587\u4ef6\u3002<\/li>\n<li>\u6253\u5f00\u6d4f\u89c8\u5668\u5e76\u8bbf\u95ee<code>http:\/\/localhost:5000<\/code>\uff0c\u60a8\u5e94\u8be5\u80fd\u591f\u770b\u5230\u663e\u793a\u201cHello, World!\u201d\u7684\u9875\u9762\u3002<\/li>\n<\/ul>\n<p><strong>3. \u5982\u4f55\u5728Python\u7684IDLE\u4e2d\u8c03\u8bd5Flask\u5e94\u7528\u7a0b\u5e8f\uff1f<\/strong><\/p>\n<p>\u8981\u5728Python\u7684IDLE\u4e2d\u8c03\u8bd5Flask\u5e94\u7528\u7a0b\u5e8f\uff0c\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u65ad\u70b9\uff0c\u8c03\u8bd5\u4ee3\u7801\u884c\u3002\u5728\u9700\u8981\u8c03\u8bd5\u7684\u884c\u4e0a\uff0c\u5355\u51fb\u5de6\u4fa7\u7684\u884c\u53f7\uff0c\u5c06\u5728\u8be5\u884c\u8bbe\u7f6e\u4e00\u4e2a\u65ad\u70b9\u3002<\/li>\n<li>\u8fd0\u884cFlask\u5e94\u7528\u7a0b\u5e8f\u3002\u60a8\u53ef\u4ee5\u5728IDLE\u4e2d\u9009\u62e9\u201c\u8fd0\u884c\u201d -&gt; \u201c\u8fd0\u884c\u6a21\u5757\u201d\u6216\u4f7f\u7528\u5feb\u6377\u952e<code>F5<\/code>\u6765\u8fd0\u884c\u3002<\/li>\n<li>\u5f53\u5e94\u7528\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u5b83\u4f1a\u505c\u5728\u8bbe\u7f6e\u7684\u65ad\u70b9\u5904\u3002<\/li>\n<li>\u4f7f\u7528IDLE\u7684\u8c03\u8bd5\u529f\u80fd\uff0c\u60a8\u53ef\u4ee5\u9010\u884c\u6267\u884c\u4ee3\u7801\u548c\u89c2\u5bdf\u53d8\u91cf\u7684\u503c\u3002<\/li>\n<li>\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528IDLE\u7684\u8c03\u8bd5\u5de5\u5177\u680f\u4e0a\u7684\u5176\u4ed6\u8c03\u8bd5\u529f\u80fd\uff0c\u5982\u5355\u6b65\u6267\u884c\u3001\u7ee7\u7eed\u6267\u884c\u7b49\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u81ea\u5e26\u7684IDLE\u4e2d\u542f\u52a8Flask\uff0c\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u5b89\u88c5Flask\u3001\u521b\u5efaFlask\u5e94\u7528\u3001\u8fd0\u884cF [&hellip;]","protected":false},"author":3,"featured_media":301550,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/301519"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=301519"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/301519\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/301550"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=301519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=301519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=301519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}