0Pricing
Flask Academy · レッスン

リソースクラスとApiオブジェクト

HTTPメソッドを持つリソースとしてエンドポイントをモデル化します

「リソースクラスとApiオブジェクト」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

A Cleaner Way to Build APIs

Plain Flask routes work, but for APIs Flask-RESTful gives you a tidy, class-based structure that scales as endpoints grow. 🧩

Install the Extension

Flask-RESTful ships separately. Add it to your project with pip before you import anything from it.

pip install flask-restful

Import the Pieces

You need two names: Api, the manager that wires endpoints, and Resource, the base class for each one.

from flask_restful import Api, Resource

Wrap Your App in an Api

Create an Api object around your Flask app. It becomes the registry where every resource gets mounted.

app = Flask(__name__)
api = Api(app)

What a Resource Is

A Resource is a Python class representing one thing in your API, like a task or a user, grouped in one place.

class HelloWorld(Resource):
    pass

Subclass Resource

Every endpoint you build starts by subclassing Resource. Methods inside it will answer HTTP verbs.

class TaskList(Resource):
    def get(self):
        return {"tasks": []}

Mount It on a URL

Use add_resource to attach a resource class to a URL path. That is how the Api learns where it lives.

api.add_resource(TaskList, "/tasks")

Return Plain Dicts

Just return a Python dict and Flask-RESTful serializes it to JSON for you, no manual jsonify needed. ✨

def get(self):
    return {"message": "hello"}

No More Route Decorators

Notice you skipped @app.route entirely. The Api object and add_resource replace the decorator workflow.

Run It Like Any Flask App

Startup is unchanged: the same app.run serves your resource-based API on the dev server.

if __name__ == "__main__":
    app.run(debug=True)

Why Classes Help

Grouping related verbs in one class keeps each endpoint self-contained and far easier to read than scattered functions.

Quick Check

Which object connects a Resource class to a URL?

Recap: Resources and the Api

You wrapped your app in an Api, subclassed Resource, and mounted it with add_resource. A clean API skeleton! 🎉

よくある質問

「リソースクラスとApiオブジェクト」レッスンは無料ですか?

はい。「リソースクラスとApiオブジェクト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。

「リソースクラスとApiオブジェクト」で何を学びますか?

HTTPメソッドを持つリソースとしてエンドポイントをモデル化します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Flask Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「リソースクラスとApiオブジェクト」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このFlask Academyレッスンでコードを書いて実行できますか?

はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. リソースクラスとApiオブジェクト
  2. HTTPメソッドをクラスハンドラーに対応付ける
  3. reqparseで解析と検証を行う
  4. BlueprintにマウントするRESTリソース
← Flask Academyに戻る