SoFunction
Updated on 2024-12-10

Example of python-tornado's interface wrapped with swagger

The main reason for writing this post, is because there is no related stuff, resulting in I have absolutely no idea what to do, after two nights of fiddling, finally figured it out, if anyone needs tornado + swagger output mode, you can follow this set;

Mainly static file generation

We use swagger just to make a static page, that is, to generate a static file:

A few essential tools: swagger-py-codegen we do not use their file framework only its generated static file, copy it to their own folder;

1) It should be noted that the generation of files need to use the yaml file, when editing the file, the main thing is to pay attention to the base_path file changes, you generate the file request will be added to the contents of your basepath, will cause the request error, For example Your url:port/GET?testparas=test, then if you say that your static file generation process, as shown in the following figure

2) the generated file, swagger-ui in the file is generated in accordance with your yaml file, the file is the address of the place settings, as follows

3) At this point we can start our swagger program, the

The next step is to accept some settings in tornado, mainly for the application:

The advanced methods of tornado are not really used here, just a random parameter set for testing:

`import 
import 
import os


abs_path = (("__file__"))
print(abs_path)

class IndexHandler():
 def get(self, *args, **kwargs):
  para = self.get_argument('test', None)
  ("test tornado {}".format(para))


if __name__ == '__main__':
 app = ([
  (r'/GET', IndexHandler)
 ],
  static_path=(abs_path, 'static')
 )
 # Listening to ports
 (8000)
 ().start()`

The directory structure is as follows

The code to generate swagger-ui is as follows:

swagger_py_codegen -s  project-one -p demo -tlp=tornado --ui --spec

where -ui -spec is the key code to generate swagger-ui

The above example of this python-tornado's interface wrapped with swagger is all I have to share with you, I hope it will give you a reference, and I hope you will support me more.