Swagger is a RESTful document generation tool. Swagger is a RESTful document generation tool.

The official description is “The Best APIs are Built with Swagger Tools”.

Swagger can be used in many language frameworks, such as “flask-rest-swagger” in Python’s Flask framework, “Django-rest-Swagger” in Django framework, and even Tornado has it, but it is used much less than the first two.

Because swagger is powerful and has many integration tools, today we will focus on Swagger – UI.

Why swagger-UI

It’s often said in the programming world that there are two things programmers hate:

1. Write comments and documents 2. Others do not write comments or documents.

Why do you say that? Because it is difficult to manage document comments, there will often be API updates, documents are still old, and various synchronization is inconsistent. It causes a lot of problems and delays each other, so people don’t like to document comments.

The main reason for using Swagger is that swagger can reduce the synchronization problem of our front-end and back-end development documents. Swagger can automatically generate API documents from our code annotations, so as to facilitate the use of front-end docking.

Secondly, it can display the list of all our interfaces, which is very convenient for us to conduct interface debugging at the front and back end. The front-end student docking interface can be operated directly on the page, without switching from postman, PAW and other network tools. It is very simple and convenient.

Here is an official preview of the table definition:

Django Swagger is installed to use

Swagger UI: Django swagger UI: Django swagger UI: Django Swagger UI

PIP install Django-rest-Swagger django 1.8+ Django Rest Framework 3.5.1+ Python 2.7, 3.5, 3.6

In the INSTALLED_APPS

    INSTALLED_APPS = (
        ...        'rest_framework_swagger'.)Copy the code

Add document address:

from django.conf.urls import urlfrom rest_framework_swagger.views import get_swagger_view

schema_view = get_swagger_view(title='Pastebin API')

test_urlpatterns = [
    url(r'^ $', schema_view)  We need to note that this is used locally or in test environments, not online. The easiest way to do this is to use the environment variable I mentioned earlier to determine whether to add test_urlPatterns to the current environment. if current_env not in ['product', 'staging']:
    url_patterns = url_patterns + test_urlpatterns
Copy the code

The result is something like this:

If combined with Django-filter, it will be much easier to perform front-end filtering tests, almost in between raising hands for XXX admin pages. Swagger with Django-Rest-Framework can be said to greatly improve the speed of back-end WRITE add/Delete (CRUD) and interconnection completion.

Use of other languages

Swagger is not only used by Python, but also by other language frameworks. I recommend it to all of you, whether you’re a Python programmer or not.

Other language tools integrated address: https://swagger.io/tools/open-source/open-source-integrations/

I do not know how many other companies, every time API update, the document is not updated, resulting in the connection with each other inconsistent situation, anyway, I have heard the feedback of friends around.

If you find yourself in this situation, don’t hesitate to swagger.

Swagger codeGen, swagger Editor, swagger CodeGen If you are interested, you can find out for yourself.

Related articles:

Container Deployment Practices for Django Applications (Part 2)

Deploying Django applications using Docker Container Deployment Practices (Part 1)

Python Web development goes from getting started to giving up

Six tips for writing high-quality Python

Common Python questions and tool recommendations