Skip to Content
ファストAPI
book

ファストAPI

by Bill Lubanovic
March 2025
Intermediate to advanced
280 pages
3h 20m
Japanese
O'Reilly Media, Inc.
Content preview from ファストAPI

第7章. フレームワークの比較

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

骨組みは必要ない。必要なのは絵画であってフレームではない。

クラウス・キンスキー(俳優

プレビュー

、Flask、Django、または一般的なPythonウェブフレームワークを使用したことがある開発者にとって、この章はFastAPIの類似点と相違点を指摘する。 そうでなければ、この本を束ねる接着剤にならないため、耐え難い細部には触れない。 この章の比較は、これらのフレームワークからFastAPIにアプリケーションを移行しようと考えている場合、または単に興味がある場合に役立つ。

新しいウェブフレームワークについて最初に知りたいことの一つは、どうやって始めるかであり、トップダウンの方法は、ルート(URLやHTTPメソッドから関数へのマッピング)を定義することである。 次のセクションでは、FastAPIとFlaskでこれを行う方法を比較する。なぜなら、これらはDjangoよりも互いに似ており、同じようなアプリケーションのために一緒に検討される可能性が高いからである。

Flask

Flaskは、 自身をマイクロフレームワークと呼んでいる。基本的な機能は提供されており、必要に応じてサードパーティのパッケージをダウンロードして補完する。 Djangoよりも小さく、使い始めのうちは習得が早い。

FlaskはASGIではなくWSGIをベースとした同期型である。quartという新しいプロジェクトがFlaskを複製し、ASGIサポートを追加している。

FlaskとFastAPIがどのようにWebルーティングを定義しているのか、トップから見ていこう。

パス

トップレベルでは、 FlaskもFastAPIも、デコレーターを使ってルートとWebエンドポイントを関連付ける。例7-1では、URLパスから挨拶する人を取得する例3-11第3章に戻る)を重複させてみよう。

例 7-1. FastAPI パス
from fastapi import FastAPI

app = FastAPI()

@app.get("/hi/{who}")
def greet(who: str):
    return f"Hello? {who}?"

デフォルトでは、FastAPI はf"Hello? {who}?" 文字列を JSON に変換し、ウェブクライアントに返す。

例7-2は、Flaskがそれを行う方法を示している。

例7-2. Flaskパス
from flask import Flask, jsonify

app = Flask(__name__)

@app.route("/hi/<who>", methods=["GET"])
def greet(who: str):
    return jsonify(f"Hello? {who}?")

デコレーターのwho が、<> で囲まれていることに注目してほしい。Flaskでは、メソッドを引数として含める必要がある-デフォルトのGET を除いて。meth⁠ods=​["GET"] を省略することもできたが、明示的であることに越したことはない。

Flask 2.0は、app.route の代わりに@app.getのようなFastAPIスタイルのデコレーターをサポートしている。

Flaskのjsonify() 関数は、引数をJSON文字列に変換し、JSONであることを示すHTTPレスポンスヘッダとともに返す。(他のデータ型ではなく) ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

GitHubアクションを学ぶ

GitHubアクションを学ぶ

Brent Laster
仕事ではじめる機械学習 第2版

仕事ではじめる機械学習 第2版

有賀 康顕, 中山 心太, 西林 孝

Publisher Resources

ISBN: 9798341624658