Skip to main content

A set of tools to aid in adding APISpec to Flask projects.

Project description

A set of tools to aid in adding APISpec to Flask projects.

Installation

pip install flask-apispec-tools

Configuration

flask-apispec-tools requires the following inside the Flask app config.

[FLASK_APISPEC_TOOLS]
version =
title =
description =
docs_dir =
docs_type =
version The version of your api.
title The name of your api.
description A description of your api.
docs_dir The directory where api docs are to be stored.
docs_type The format you want your docs created as. Can be either "json" or "yaml".

Setting Config Values

There are multiple ways to add the config items to the Flask app. See Flask Configuration Handling. While it is recommended to use separate config files or environment variables, flask-apispec-tools allows you to pass configuration values to the init() function. See Configuring flask-apispec-tools with init().

Referencing Other Config Options

Config values can be references to other config options using the format ${section:option}.

[FLASK_APISPEC_TOOLS]
version = ${METADATA:version}
title = ${METADATA:title}
description = ${METADATA:description}
docs_dir = myproj/static/docs
docs_type = json

Initialization

flask_apispec_tools.init(app)

This registers the cli command generate-api-docs with Flask and adds 3 endpoints ( /docs, /docs/json or /docs/yaml, /version) to the app.

Customizing Built-in Endpoints

Adding these endpoints can be disabled or their paths can be changed by passing additional arguments to init().

The example below sets the paths for the docs and docs_json endpoints and disables the version endpoint.

flask_apispec_tools.init(
    app,
    docs_endpoint='/api/docs',
    docs_json_endpoint='/api/docs/json',
    version_endpoint=False
)

APISpec Plugins

A list of apispec plugins can be passed to init() and they will be given to the APISpec object. See APISpec Using Plugins. You do not need to give init() the FlaskPlugin.

flask_apispec_tools.init(
    app,
    plugins=[MarshmallowPlugin(), MyCustomPlugin()]
)

Configuring flask-apispec-tools with init()

flask-apispec-tools can be configured by passing a dictionary of config values to init(). This will override any existing configuration values.

flask_apispec_tools.init(
    app,
    config_values={
        'docs_dir': '/docs',
        'docs_type': 'json'
    }
)

Generating API Docs

flask generate-api-docs

Options

-a, --all Include enpoints marked 'Exclude From Spec'.

Excluding Endpoints

Endpoints can be excluded from docs by adding Exclude From Spec at the top of the docstring. This exclusion can be ignored using -a or --all with flask generate-api-docs.

class MyEndpoint(MethodView):
    """Exclude From Spec"""

    def get(self):
        ...

Built-in Endpoints

endpoint description query parameters
/docs Display docs with Swagger UI. version (optional): Which version of the API docs to get.
/docs/json Get docs as JSON. version (optional): Which version of the API docs to get.
/docs/yaml Get docs as YAML. version (optional): Which version of the API docs to get.
/version Get the API version.

Additional Functions

These functions are used internally, but you may find them useful as well.

flask_apispec_tools.tools

config_values(option: str, *, config: Config = None) -> str | None:

Get the value of an option from the config.  

Args:  
	option: The option to get the value for. 
	config: Optional. Default: flask.current_app.config.  
Returns:  
	str: The config value. 
	None: The option was not found.

get_docs_filename(version: str = None, *, config: Config = None) -> str:

Get the name of a docs file for a specific version.  

Args:  
	version: Optional. Default: The version set in the config. 
	config: Optional. Default: flask.current_app.config.  
Returns:  
	 The docs filename.

get_docs_filepath(version: str = None, *, config: Config = None) -> str:

Get the filepath of a docs file for a specific version.  
  
Args:  
	version: Optional. Default: The version set in the config. 
	config: Optional. Default: flask.current_app.config.  
Returns:  
	The docs filepath.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-apispec-tools-0.1.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_apispec_tools-0.1.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file flask-apispec-tools-0.1.1.tar.gz.

File metadata

  • Download URL: flask-apispec-tools-0.1.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for flask-apispec-tools-0.1.1.tar.gz
Algorithm Hash digest
SHA256 02649c65a5b74770c4da1b5121e3e184d0deb97ba54a37ecaf8921974c709fdb
MD5 8e850bcedb628c1f404f9af9fcb8e818
BLAKE2b-256 ac494e2629e91c10ce4c5804f82145fa0ee582e0cfaf0c0ee62c2d96ee976b05

See more details on using hashes here.

File details

Details for the file flask_apispec_tools-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_apispec_tools-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1abd358caf73494f8d3c2e6df02f299d1ec06aee0faa45b59cb9e52466f4aae2
MD5 6ba3c61d2722185c5f0df5d113c631af
BLAKE2b-256 32c7df4d559fb2471df35b45c76a3c29cfa6a482a2fdc97295533e0f26bfaaba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page