{"id":1941,"date":"2024-04-29T09:19:16","date_gmt":"2024-04-29T01:19:16","guid":{"rendered":"http:\/\/blog.xtaa.cn\/?p=1941"},"modified":"2024-04-29T09:33:12","modified_gmt":"2024-04-29T01:33:12","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-fastapi%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/blog.xtaa.cn\/index.php\/2024\/04\/29\/%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-fastapi%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 FastAPI\uff1f"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7684\u6b65\u9aa4\u5c06\u5e2e\u52a9\u4f60\u5f00\u59cb\u4f7f\u7528\u00a0FastAPI\u00a0\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 Web \u5e94\u7528\u3002<\/p>\n\n\n\n<p>1.\u5b89\u88c5 FastAPI\uff1a\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5&nbsp;<code>fastapi<\/code>\uff0c\u5e76\u5b89\u88c5&nbsp;<code>uvicorn<\/code>&nbsp;\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install fastapi<\/code><\/pre>\n\n\n\n<p>2. \u521b\u5efa FastAPI \u5e94\u7528\uff1a\u5728\u4f60\u559c\u6b22\u7684 IDE \u7f16\u8f91\u5668\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Python \u6587\u4ef6\uff0c\u4f8b\u5982<code>fastapi-demo.py<\/code>\u3002\u7136\u540e\u5c06\u4ee5\u4e0b\u4ee3\u7801\u590d\u5236\u5230\u6587\u4ef6\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from fastapi import FastAPI\n\napp = FastAPI()\n\n@app.get(\"\/\")\ndef read_root():\n    return {\"Hello\": \"World\"}<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a&nbsp;<code>app<\/code>&nbsp;\u7684 FastAPI \u5b9e\u4f8b\uff0c\u5e76\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6839\u8def\u7531\uff0c\u8fd4\u56de\u4e00\u4e2a\u7b80\u5355\u7684 JSON \u54cd\u5e94\u3002<\/p>\n\n\n\n<p>3. \u8fd0\u884c FastAPI \u5e94\u7528\uff1a\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8 FastAPI \u5e94\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>uvicorn main:app --reload<\/code><\/pre>\n\n\n\n<p>FastAPI \u5c06\u5728\u672c\u5730\u542f\u52a8\u4e00\u4e2a\u670d\u52a1\u5668\uff0c\u5e76\u76d1\u542c\u9ed8\u8ba4\u7aef\u53e3\uff088000\uff09\u3002\u4f60\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee&nbsp;<code>http:\/\/127.0.0.1:8000<\/code>\uff0c\u770b\u5230&nbsp;<code>{\"Hello\": \"World\"}<\/code>&nbsp;\u7684\u54cd\u5e94\u3002<\/p>\n\n\n\n<p>4. \u6dfb\u52a0\u66f4\u591a\u7684\u8def\u7531\u548c\u529f\u80fd\uff1a\u4f60\u53ef\u4ee5\u7ee7\u7eed\u5728\u5e94\u7528\u4e2d\u6dfb\u52a0\u66f4\u591a\u7684\u8def\u7531\u548c\u529f\u80fd\uff0c\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8fdb\u884c\u6269\u5c55\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@app.get(\"\/items\/{item_id}\")\ndef read_item(item_id: int, q: str = None):\n    return {\"item_id\": item_id, \"q\": q}<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u6dfb\u52a0\u4e86\u4e00\u4e2a\u540d\u4e3a&nbsp;<code>read_item<\/code>&nbsp;\u7684\u65b0\u8def\u7531\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a&nbsp;<code>item_id<\/code>&nbsp;\u53c2\u6570\u548c\u4e00\u4e2a\u53ef\u9009\u7684&nbsp;<code>q<\/code>&nbsp;\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a JSON \u54cd\u5e94\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"%E5%AE%9E%E8%B7%B5%E6%A1%88%E4%BE%8B\">\u5b9e\u8df5\u6848\u4f8b<\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 FastAPI \u6784\u5efa\u7684\u5b9e\u8df5\u6848\u4f8b\uff0c\u80fd\u591f\u5728 IDE \u7f16\u8f91\u5668\u4e2d\u76f4\u63a5\u8fd0\u884c\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from fastapi import FastAPI\n\napp = FastAPI()\n\n@app.get(\"\/\")\ndef read_root():\n    return {\"Hello\": \"World\"}\n\n@app.get(\"\/items\/{item_id}\")\ndef read_item(item_id: int, q: str = None):\n    return {\"item_id\": item_id, \"q\": f\"\u63a5\u53e3id\uff1a{item_id}\"}\n\nif __name__ == \"__main__\":\n    import uvicorn\n    uvicorn.run(app, host=\"127.0.0.1\", port=8000)<\/code><\/pre>\n\n\n\n<p>\u590d\u5236\u4ee5\u4e0a\u4ee3\u7801\u5230\u4f60\u7684 IDE \u7f16\u8f91\u5668\u4e2d\uff0c\u5e76\u8fd0\u884c\u5b83\u3002\u7136\u540e\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee&nbsp;<code>http:\/\/localhost:8000\/<\/code>\uff0c\u4f60\u5c06\u770b\u5230&nbsp;<code>{\"Hello\": \"World\"}<\/code>&nbsp;\u7684\u54cd\u5e94\u3002\u540c\u6837\u5730\uff0c\u4f60\u4e5f\u53ef\u4ee5\u8bbf\u95ee&nbsp;<code>http:\/\/localhost:8000\/items\/42?q=somequery<\/code>\uff0c\u67e5\u770b\u5e26\u6709\u53c2\u6570\u7684\u54cd\u5e94\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7684\u6b65\u9aa4\u5c06\u5e2e\u52a9\u4f60\u5f00\u59cb\u4f7f\u7528\u00a0FastAPI\u00a0\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 Web \u5e94\u7528\u3002 1.\u5b89\u88c5 FastAPI\uff1a\u5728\u547d\u4ee4\u884c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1941","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1941"}],"version-history":[{"count":2,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1941\/revisions"}],"predecessor-version":[{"id":1943,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1941\/revisions\/1943"}],"wp:attachment":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1941"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}