Flasgger —an API playground with Flask and Swagger UI

Sukanya Bag
Analytics Vidhya
Published in
2 min readAug 31, 2020

What is Swagger ?

Swagger is in essence an Interface Description Language for describing RESTful APIs expressed using JSON. Swagger is used together with a set of open-source software tools to design, build, document, and use RESTful web services. Swagger includes automated documentation, code generation, and test-case generation.

What is Swagger UI?

The Swagger UI is an open source project to visually render documentation for an API defined with the OpenAPI (Swagger) Specification. Swagger UI lets you visualize and interact with the API’s resources without having any of the implementation logic in place, making it easy for back end implementation and client side consumption. Swagger UI is available for download in the GitHub repository, or can be generated for any new or existing OpenAPI-defined API in the integrated SwaggerHub platform.

What is Flask?

(duhhhh!! C’mon you already know this haha ! :P)

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.

Why it is awesome?

Because it is simple yet powerful, talking is cheaper look at the code!

Run the above script and then you can start posting to the API-

What is Flasgger?

Flasgger is a Flask extension to extract OpenAPI-Specification from all Flask views registered in your API. Flasgger also comes with SwaggerUI embedded so you can access http://localhost:5000/apidocs and visualize and interact with your API resources. Flasgger is a Flask extension to help the creation of Flask APIs with documentation and live playground powered by SwaggerUI.

Install it

pip install flasgger

Create your app

Now run your app and access http://localhost:5000/apidocs/index.html and you will play with Swagger UI!

NOTE: All the default urls can be changed in configuration.

It is also possible to use a separate file for specs

Create your api specification in a separated YML file

and then change the code to read from it using swag_from decorator

Validation

If you put the specs in a separate file it is also possible to use the same specs to validate the input!

Hope you liked this blog post!

Try to make apps and run using Flasgger and Swagger UI, its free and great!

You can also run your Machine Learning models in the same way!

YAY!

Please share your thoughts about it, if you feel for the same!

Happy Pythoning!

Happy Swagging!

Until next time…!

--

--

Sukanya Bag
Analytics Vidhya

I love to teach Machine Learning in simple words! All links at bio.link/sukannya