Examples¶
- Basic example: /examples/basic/app.py
- Class-based view example: /examples/cbv/app.py
- ORM example (with Flask-SQLAlchemy): /examples/orm/app.py
- Pagination example (with Flask-SQLAlchemy): /examples/pagination/app.py
- OpenAPI example: /examples/openapi/app.py
- Base response example: /examples/base_response/app.py
- Token auth example: /examples/auth/token_auth/app.py
- Basic auth example: /examples/auth/basic_auth/app.py
- Dataclass example (with marshmallow-dataclass): /examples/dataclass/app.py
- File upload example: /examples/file_upload/app.py
If you have built an application with APIFlask, feel free to submit a pull request to add the source link here.
- Flog (under active construction)
Follow the commands in the Installation section to run these examples on your computer.
Installation¶
Build the environment¶
For macOS and Linux:
$ git clone https://github.com/apiflask/apiflask
$ cd apiflask/examples
$ python3 -m venv venv
$ source venv/bin/activate
$ pip3 install -r requirements.txt
For Windows:
> git clone https://github.com/apiflask/apiflask
> cd apiflask\examples
> python -m venv venv
> venv\Scripts\activate
> pip install -r requirements.txt
Choose the application¶
Each example application store in a sub-folder:
/basic
: Basic example/cbv
: Classed-based view example/orm
: ORM example (with Flask-SQLAlchemy)/pagination
: Pagination example (with Flask-SQLAlchemy)/openapi/basic
: Basic OpenAPI example/openapi/custom_decorators
: OpenAPI example with custom decorators/base_response
: Base response example/dataclass
: Dataclass example (with marshmallow-dataclass)
To run a specific example, you have to change into the corresponding folder. For example, if you want to run the basic example:
$ cd basic
Run, Flask, Run!¶
After change into the desired folder, use flask run
command to run
the example application:
$ flask run
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Try it out¶
When the application is running, now you can visit the interactive API documentation at http://localhost:5000/docs. Inside the detail tab of each endpoint, you can click the "Try it out" button to test the APIs:
Then click the "Execute" button, it will send a request to the related endpoint and retrieve the response back:
Do some experiments¶
If you want to do some experiment on the example application, just open the app.py
with your favorite editor. To make the application reload every time after you change the code, use the --reload
option for flask run
:
$ flask run --reload
Or run in debug mode:
$ flask run --debug
With debug mode, it will enable the reloader and debugger as default. See Debug Mode for more details.