How to get html page displayed in swagger UI (flask) - flask

I have used apispec and flask-swagger-ui with flask.
The flask route returns a html page but on swagger UI I could only see the html code not the actual page.
Is there any way to get the html page displayed on swagger UI.
the flask code along with the APIspec documentation
swagger UI response
expected response

OpenAPI describes the text/code response your API will return. Swagger UI is correctly displaying the body of your response, not how that response will be interpreted and displayed by a particular consumer of your API (in this case a web browser). What you are trying to do is outside the intended scope of this tool. See this issue in which the developers rejected this idea for reasons of security.
Alternative:
Use the description property to include a link to additional documentation that includes images or screenshots of what this response would look like.

Related

flasgger-flask-swagger: request parameters not displayed in Webpage

I want to create a REST API using flask. I also would like to document the api using swagger 2.0. This is the reason why I chose flasgger to create a webpage for the documentation. Right now the web page is rendered but the parameters for the request are not displayed, although I did add them in the yaml file.
I added the result of the webpage as image.
Part of the content of the yaml file can be seen in the other image.
In the main file I configured the Swagger object like in the image below.
I would be really thankful, if you could help find the reason why, I am not able to see those parameters.
Source code
documentation file
webpage result
you need to wrap your view with the decorator #swag_from("your_yaml_file_here")
for example:
from flasgger import swag_from
#app.route('/colors/<palette>/')
#swag_from('colors.yml')
def colors(palette):
....
If you do not want to use the decorator you can use the docstring file: shortcut.
#app.route('/colors/<palette>/')
def colors(palette):
"""
file: colors.yml
"""
...
Refer the link https://github.com/flasgger/flasgger to know more.

Is there a way in django to update the same page with the response without totally rendering it?

Is there a way in django to update the same page with the response without totally rendering it. I am trying to create a code editor to test. But when I am returning the results my contents are removed. I understand it is because I am rendering the page . I need the contents to retain. How can I do it using redirect? I am including the render statement I used and a screenshot of how it looks here:
Steps:
Handle post request
Program execution code
Save the result in a variable called "message". Then I used
return render(request, 'editor.html', {'message': message})
I want to redirect the message to the same page without rendering a new page.
[Before submission][1]
[After submission][2]
[1]: https://i.stack.imgur.com/BxoLU.png
[2]: https://i.stack.imgur.com/uiEOU.png
Any help will be appreciated. Thank you.
it is possible. using ajax in front-end and render or render_to_string in back-end(Django). using ajax you're able to call a url (with/without data you want to send), then in the views.py you can do anything needed and return render(request,'template.html, context). then in ajax you have success: function (res) { $('#id_of_element').append(res)}. this function in ajax will append the recived response which is a rendered HTML template to the target element.
For that, you have to switch to a different web software paradigm called "single page application", which implies that both, backend and frontend, are functional software components on their own, instead of having a "dumb" HTML frontend that only displays what the backend renders.
In a regular web application, the front end is served from a backend with all the information that is going to display. In a Single Page Application, the front end is served by a server independent of the backend server, and the frontend and backend interact through an API served by the backend.
With this architecture, the frontend component is responsible for requesting and providing data from and to the backend, as well as for displaying the data and getting user's interaction, and the mean for interchanging data with the backend is called an ajax, that is an asynchronous request.
The only language accepted by web browsers is javascript, but there are many frameworks and second level languages that can render a javascript application, like React, Angular, Vue, and many others.

How to profile an AJAX endpoint in Flask?

To profile a GET endpoint in Flask, I've been using the Line Profile Panel for Flask Debug Toolbar.
This doesn't work on AJAX/XHR endpoints though. I've also tried line_profiler but it doesn't play nice with Flask.
Martijn Pieters' comment is correct. You can use Flask-Debug-API together with the Line Profiler Panel (https://github.com/jlfwong/flask_debugtoolbar_lineprofilerpanel).
Once both are added to your code, then you can navigate through the API Browser to the endpoint you want to test, submit a request to it, then you will have access to the results of the line profiler.

use django-rest-swagger 2 with custom swagger.json

I have a project build with django-rest-framework, and I want to use django-rest-swagger to get API documentation, so I made a swagger.json file via swagger editor, then my question is:
How can I make django-rest-swagger read and render my own swagger.json instead of auto-generated from code?
I've checked the django-rest-swagger doc over and over again but nothing found about that.
Any comment will be appreciated.
I know this is old post, but I ran into the same issue and wanted to provide my work around.
If you are trying to create a Swagger UI from an external JSON, this was my work around. I am using django but wanted to provide the swagger api of another framework and server. Here are two options:
The simplest solution is to just manually render the swagger html template and insert the endpoint url that provides the JSON inside SwaggerUi(), this is generally located in the last block.
Alternatively, if you cannot access the json directly or have a static file, create your own rest end point that either reads the file, or makes the request to the remote server, and then itself serves up the desired JSON. Reference this endpoint in your swagger template.

How can I do a redirect from a plugin in Django CMS?

I have a form in a plugin that on submit I need to redirect to another page.
What's the best way to accomplish this?
So possible solutions are:
Use an App-Hook
Throw an exception in your plugin render method that would be caught by a middleware class and do the redirect from there.
Create a middleware class and during the "process_response" method check for a value on the request object that was added during the render method of the plugin then do the redirect.
Plugins are not really suitable for processing POST requests and there's no way to influence the HTTP Response object from a plugin (other than the content of that response).
The reason Plugins have no hook to process POST requests is that a single page is usually composed of several plugins, and figuring out which plugin should handle the POST request would be very hard. For the same reason they can't change the response, since two plugins could try to change the response in an incompatible fashion.
The solution for this is to have a dedicated POST endpoint for your plugins (either static via urlpatterns or via an Apphook). That endpoint would then redirect to another page, or the page the form is on, so the plugin would send some data with it. Alternatively the plugin submits the form via AJAX to that endpoint and redirects/acts in javascript.