پرش به محتویات

Examples

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: OpenAPI example
  • /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 experiment

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

Furthermore, you can run the application in debug mode; it will enable the reloader and debugger as default. To enable the debug mode, you will need to set the environment variable FLASK_ENV to development before executing flask run, see Debug Mode for more details.