Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting JSON and python Flask - any techniques to use the Werkzeug debugger?

Alright, I'm working with a RESTful backend on my project, and submitting data via jquery.

I must say the werkzeug debugger is excellent for debugging specially when you're a terrible python programmer as me. You throw an exception on purpose where you want to investigate, and inspect the code and variables using the html the debugger rendered.

However when you send a post request instead of a get, if you throw an exception on the backend code, of course, the browser won't render the response text.

Is there any technique I can use to render the response text, considering it has javascript and everything?

I'm trying different things such as trying to inject the response text into a popup window, like:

           $.postJSON = function(url, data, callback, error_callback) {
                return jQuery.ajax({
                    'type': 'POST',
                    'url': url,
                    'contentType': 'application/json',
                    'data': JSON.stringify(data),
                    'dataType': 'json',
                    'success': callback,
                    'error': error_callback
                });
            };

            $.postJSON('/the_uri', {'foo': 'bar'}, 
            function(response) {
                var a = 0;
            }, 
            function(response) {
                var html = response.responseText;
                var my_window = window.open('', 'mywindow1', 'width=350,height=150');
                $(my_window.document).find('html').html(html);
            });
        });

But this won't take care of the javascript very well.

Does anyone have any suggestion?

like image 648
johnildergleidisson Avatar asked Mar 16 '12 17:03

johnildergleidisson


People also ask

How do you debug a Python Flask?

If you click on the “Run and Debug” icon on the left hand side of the IDE or alternatively type Ctrl+Shift+D you will see the “RUN AND DEBUG” window. Now click on the “create a launch. json file” link and when prompted to “Select a debug configuration” choose “Python File Debug the currently active Python file”.

What is debug mode in Flask explain with example?

Flask Debug mode allows developers to locate any possible error and as well the location of the error, by logging a traceback of the error. The Flask debug mode also enables developers to interactively run arbitrary python codes, so that one can resolve and get to the root cause on why an error happened.


2 Answers

Your approach was nearly correct. I am using the following code to open the response text in a new window (not specific to Werkzeug or Flask at all):

var w = window.open('', 'debug_stuff', 'width=540,height=150');
w.document.open();
w.document.write(response.responseText);
w.document.close();

The last line is the most important. Without it, the code would behave as yours -- it would not execute any JavaScript, because the browser doesn't know the DOM has been fully loaded.

like image 111
Markus Unterwaditzer Avatar answered Oct 19 '22 17:10

Markus Unterwaditzer


Not javascript, but have you tried to use Firebug, you can use the option for viewing the response in a new tab (Open Response in New Tab).

like image 39
rgamez Avatar answered Oct 19 '22 18:10

rgamez