{"id":339923,"date":"2021-02-05T12:57:09","date_gmt":"2021-02-05T04:57:09","guid":{"rendered":"http:\/\/4563.org\/?p=339923"},"modified":"2021-02-05T12:57:09","modified_gmt":"2021-02-05T04:57:09","slug":"fastapi-%e7%9a%84%e6%ad%a3%e7%a1%ae%e5%b7%a5%e7%a8%8b%e5%8c%96%e6%96%b9%e5%bc%8f%e6%98%af%e4%bb%80%e4%b9%88%e6%a0%b7%e7%9a%84%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=339923","title":{"rendered":"FastAPI \u7684\u6b63\u786e\u5de5\u7a0b\u5316\u65b9\u5f0f\u662f\u4ec0\u4e48\u6837\u7684\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  FastAPI \u7684\u6b63\u786e\u5de5\u7a0b\u5316\u65b9\u5f0f\u662f\u4ec0\u4e48\u6837\u7684\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : LeeReamond <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5982\u9898\uff0c\u91cd\u6784\u9879\u76ee\u65f6\u7ec4\u7ec7\u6587\u4ef6\u9047\u5230\u4e00\u4e2a\u5faa\u73af\u5bfc\u5165\u95ee\u9898\u3002<\/p>\n<p>\u901a\u5e38 web \u5e94\u7528\u505a\u6cd5\u662f\u5c06\u89c6\u56fe\u5355\u72ec\u62ff\u51fa\u6765\u653e\u5728\u4e00\u8d77\uff0c\u8def\u7531\u4e5f\u62ff\u51fa\u6765\u653e\u5728\u4e00\u8d77\uff0c\u7b49\u7b49\u7b49\u7b49\uff0c\u7136\u540e\u542f\u52a8\u670d\u52a1\u65f6\u4ece\u7a0b\u5e8f\u5165\u53e3\u4e00\u80a1\u8111\u7684\u5bfc\u5165\u3002\u4f46\u662f\u4e3a\u4e86\u65b9\u4fbf\u4f7f\u7528\uff0c\u6211\u4eec\u5e0c\u671b\u5c06\u7a0b\u5e8f\u6240\u6709\u914d\u7f6e\u96c6\u4e2d\u8d77\u6765\u653e\u5728\u5165\u53e3\u5904\uff0c\u8fd9\u6837\u542f\u52a8\u65f6\u5c31\u53ef\u4ee5\u8f7b\u6613\u5730\u5f00\u5173\u529f\u80fd\u3002<\/p>\n<p>\u6240\u4ee5\u6bd4\u5982\u4e00\u4e2a\u5178\u578b\u60c5\u51b5\uff0c\u5728\u7a0b\u5e8f\u5165\u53e3\uff08\u5047\u8bbe\u540d\u4e3a app.py \uff09\u5904\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a jinja2 template\uff0c\u8fd9\u79cd\u6a21\u677f\u6587\u4ef6\u5939\u7684\u4f4d\u7f6e\u5c5e\u4e8e\u914d\u7f6e\u95ee\u9898\uff0c\u4e0d\u9002\u5408\u5199\u6b7b\u5728\u4e1a\u52a1\u6a21\u5757\u91cc\u3002\u4f46\u8fd9\u4e9b template \u53c8\u9700\u8981\u88ab\u4e1a\u52a1\u6a21\u5757\u6240\u8c03\u7528\uff0c\u6bd4\u5982\u67d0\u4e2a\u8def\u7531\u8fd4\u56de\u67d0\u4e2a\u6e32\u67d3\u540e\u7684\u6a21\u677f\u4e4b\u7c7b\u7684\u3002\u8fd9\u5c31\u4ea7\u751f\u4e86\uff0c\u89c6\u56fe\u9700\u8981\u5bfc\u5165 app.py \u91cc\u7684\u5185\u5bb9\uff0c\u800c app.py \u53c8\u9700\u8981\u5bfc\u5165\u89c6\u56fe\u624d\u80fd\u8fd0\u884c\uff0c\u4ea7\u751f\u4e86\u5faa\u73af\u5bfc\u5165\u7684\u95ee\u9898\u3002<\/p>\n<p>\u901a\u5e38\u7684 web \u6846\u67b6\u6709\u5f88\u591a\u53d8\u901a\u7684\u89e3\u51b3\u529e\u6cd5\uff0c\u6bd4\u5982\u504f\u51fd\u6570\u3001\u88c5\u9970\u5668\u3001\u6216\u8005\u4e00\u4e9b\u5171\u4eab\u72b6\u6001\u7ec4\u4ef6\u4e4b\u7c7b\u7684\u3002\u4f46\u662f fastapi \u7531\u4e8e\u6ce8\u5165\u4e86\u53c2\u6570\u5316\u6a21\u5757\uff0c\u5bfc\u81f4\u6bd4\u5982\u5728\u4e0b\u9762\u7684\u89c6\u56fe\u903b\u8f91\u4e2d<\/p>\n<pre><code>@app.get('\/') async def root(request:Request , template: jinja2.Tmeplate):     return template.RenderResponse('index.html' , {'title':'hello world'})  root = functools.partial(template = template) <\/code><\/pre>\n<p>\u6bd4\u5982\u8fd9\u79cd\u65b9\u5f0f\u662f\u884c\u4e0d\u901a\u7684\uff0c\u6240\u6709\u8f93\u5165\u53c2\u6570\u90fd\u4f1a\u88ab fastapi \u62ff\u6765\u8fdb\u884c\u53c2\u6570\u68c0\u67e5\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5249480\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : johnsona <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             fullstack-fastapi \u8fd9\u4e2a\u4ed3\u5e93                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5249481\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : LeeReamond <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @johnsona \u770b\u4e86\u4e00\u4e0b\uff0c\u8fd8\u662f\u6ca1\u6709\u89e3\u51b3\u5de5\u7a0b\u95ee\u9898\uff0c\u4ed6\u8fd9\u4e2a app=FastAPI()\u548c\u89c6\u56fe\u662f\u5199\u5728\u4e00\u8d77\u7684\u3002<\/p>\n<p>\u53e6\u5916\u770b\u6709\u51e0\u4e2a\u4eba\u6536\u85cf\u8fd9\u4e2a\u5e16\u5b50\u4e86\uff0c\u672c\u7740\u8d1f\u8d23\u7684\u6001\u5ea6\u56de\u590d\u4e00\u4e0b\u6211\u76ee\u524d\u7684\u89e3\u51b3\u65b9\u6848\u3002\u6211\u7531\u4e8e\u524d\u6bb5\u65f6\u95f4\u7814\u7a76\u4e86\u4e00\u4e0b PEP563\uff0c\u6240\u4ee5\u81ea\u7136\u5730\u60f3\u5230\u8fd0\u884c\u65f6\u83b7\u53d6 globals \u7684\u65b9\u6cd5\uff0c\u65e0\u8bba\u5bfc\u5165\u8fc7\u7a0b\u5982\u4f55\uff0c\u8fd0\u884c\u65f6\u90fd\u53ef\u4ee5\u6bd4\u8f83\u7b80\u5355\u5730\u4ece sys.modules \u91cc\u83b7\u5f97\u5b9e\u4f8b\u5bf9\u8c61\u3002\u7406\u8bba\u4e0a\u8fd9\u4e2a\u505a\u6cd5\u4e0d\u4f1a\u4ea7\u751f\u5b89\u5168\u95ee\u9898\u3001\u4e0d\u4f1a\u88ab deprecated\uff0c\u7b97\u662f\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u7684 hack\uff0c\u5bfb\u5740\u901f\u5ea6\u65b9\u9762\u57fa\u672c\u4e5f\u53ef\u4ee5\u5ffd\u7565\u5f71\u54cd\uff0c\u76ee\u524d\u662f\u582a\u7528\u3002<\/p>\n<p>\u4f46\u662f\u6700\u597d\u8fd8\u662f\u4e0d hack\uff0c\u6240\u4ee5\u6211\u6765 v2 \u95ee\u4e00\u4e0b\u5927\u5bb6\u7684\u505a\u6cd5\uff0c\u4f46\u662f\u4f3c\u4e4e\u6ca1\u4eba\u56de\u590d                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5249482\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : johnsona <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @LeeReamond \u542c\u8d77\u6765\u6709\u70b9\u50cf flask \u5faa\u73af\u5f15\u7528\u7684\u95ee\u9898 \u529e\u6cd5\u6709 \u5728 app.py \u7ed3\u5c3e\u5f15\u5165\u89c6\u56fe\u30022\uff0c\u901a\u8fc7\u4f20\u5165 app \u800c\u4e0d\u662f import app \u65b9\u5f0f\u628a app \u7ed9\u89c6\u56fe 3 \u7c7b\u4f3c flask \u84dd\u56fe\u7684\u4e1c\u897f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5249483\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : LeeReamond <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @johnsona \u95ee\u9898\u5728\u4e8e\u65e0\u6cd5\u4f20\u5165\uff0c\u89c6\u56fe\u6700\u7ec8\u903b\u8f91\u51fd\u6570\u7684\u53c2\u6570\u662f\u56fa\u5b9a\u7684\uff0c\u4f60\u4e0d\u80fd\u5f15\u5165\u4e00\u4e9b\u4e0e http \u65e0\u5173\u7684\u5176\u4ed6\u4e1a\u52a1\u53c2\u6570                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5249484\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : johnsona <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @LeeReamond <br \/>def register \uff08 app \uff09\uff1a<br \/> @app.route<br \/> def hello \uff08\uff09\uff1a<br \/> return 1<br \/>\u4e0d\u77e5\u9053\u662f\u5426\u8981\u8fd9\u4e2a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5249485\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : LeeReamond <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @johnsona \u4f60\u600e\u4e48\u628a register \u548c hello()\u8054\u7cfb\u8d77\u6765\u3002\u6bd4\u5982\u6211\u5728 main.py \u4e2d\u6709\u8fd9\u51e0\u884c\u4ee3\u7801<\/p>\n<p>app=fastapi()<br \/>template=jinja2.template()<\/p>\n<p>\u7136\u540e\u89c6\u56fe\u4e2d\u6709<br \/>@app.get()<br \/>def hello()<br \/> return template.render(&#8216;index.html&#8217;)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5249486\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : johnsona <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @LeeReamond from xxx import register<br \/> register \uff08 app \uff09                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>FastAPI \u7684\u6b63\u786e\u5de5\u7a0b\u5316\u65b9\u5f0f\u662f&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/339923"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=339923"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/339923\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=339923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=339923"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=339923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}