Debugging Python code in a browser with wdb debugger

Check out the new site at https://rkblog.dev.

wdb is a nice Python debugger running as a server and capable of launching debug session in your browser. It can handle simple scripts, web apps (Tornado, Flask or Django based and alike) as well as other multithread and multiprocess programs. It's easy to use and not much configuration is required.

The server

Install it:
pip install wdb
And start it:
wdb.server.py &
Thats it

Debuggin

Basic script can be debugged like this:
python -m wdb your_file.py
It will trigger a debugging session in your browser and the execution stopped at first line of the script:
wdb debugger in action

On the right side we have the stacktrace, on the left the debugged Python code, and below it - an interactive Python console. Aside of Python code it also supports debugger commands, like .s will tell the debugger to move to next break-point (or next line in this case).

Debugging Django or other applications you will want to use breakpoints - a places in code at which execution should stop launching the debug session. To create them for wdb just import it and then place in the code:

wdb.set_trace()
Also exceptions will trigger the debug session.

Django

One of wdb usages is to debug Django applications. To do that some configuration is needed. At first in wsgi.py find:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
And add after it:
from wdb.ext import WdbMiddleware
application = WdbMiddleware(application)
In settings.py add:
DEBUG = True
DEBUG_PROPAGATE_EXCEPTIONS = True
And that's it.
wdb debugging a Django application

There are IDEs like PyCharm that have their own debuggers. They offer similar or equal set of features. You also don't have to modify the code to set breakpoints or to start debugging. However to use them you have to use those specific IDEs (and some of then are non-free or may not be available for all platforms). Pick the right tool for your needs.

RkBlog

Django web framework tutorials, 1 July 2013


Check out the new site at https://rkblog.dev.
Comment article
Comment article RkBlog main page Search RSS Contact