Quantrium.ai
Published in

Quantrium.ai

QUANTRIUM GUIDES

Creating a Hello World API using Swagger UI and Python

Getting started with your first Swagger UI document

Steps to Create a Swagger UI Document

$ pip install Flask$ pip install flasgger
from flask import Flask, requestfrom flasgger import Swagger, LazyString, LazyJSONEncoderfrom flasgger import swag_from
app = Flask(__name__)
app.json_encoder = LazyJSONEncoder
swagger_template = dict(info = {    'title': LazyString(lambda: 'My first Swagger UI document'),    'version': LazyString(lambda: '0.1'),    'description': LazyString(lambda: 'This document depicts a      sample Swagger UI document and implements Hello World functionality after executing GET.'),    },    host = LazyString(lambda: request.host))swagger_config = {    "headers": [],    "specs": [        {            "endpoint": 'hello_world',            "route": '/hello_world.json',            "rule_filter": lambda rule: True,            "model_filter": lambda tag: True,        }    ],    "static_url_path": "/flasgger_static",    "swagger_ui": True,    "specs_route": "/apidocs/"}
swagger = Swagger(app, template=swagger_template,                               config=swagger_config)
@swag_from("hello_world.yml", methods=['GET'])@app.route("/")def hello_world():    return "Hello World!!!"
openapi: 3.0.0tags:  - name: Hello Worldget:

description: None
responses: '200': description: Successful response '400': description: Bad Request '500': description: Internal Server Error
if __name__ == '__main__':    app.run()
Document obtained after running 127.0.0.1:5000/apidocs in browser
Result obtained after selecting GET response from the document
Result obtained after selecting “Try it out” button
Result after executing GET response by selecting “Execute” option

--

--

This is Quantrium’s official tech blog. A blog on how technology enables us to develop great software applications for our clients.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bhargav Sridhar

An enthusiastic learner and a Math fanatic who wants to share knowledge with others.