2

Does anybody debug flask application in vim using this one for example. What I want: I want to set break point in controller method for example

def login():
(breakpoint)>> some code
...
...

Somehow run flask app and when I send for example login form stop on this breakpoint and debug source code.

Thanks.

3
  • Are you aware that (in Flask debug mode) you can open the debugger inside the browser from a traceback page? Commented Mar 27, 2013 at 12:34
  • Nope I just want to set break point in controller method run app and stop on breakpoint Commented Mar 27, 2013 at 12:54
  • Flask uses the Werkzeug debugger and I suggest you give it a try. Commented Mar 27, 2013 at 13:21

2 Answers 2

3

Do you know about Python debbuger? You can set breakpoints anywhere in your code using this line:

import pdb; pdb.set_trace()

If you're using vim, you might like this shortcut as well:

:ia pdb import pdb; pdb.set_trace()<ESC>
Sign up to request clarification or add additional context in comments.

Comments

3

Below is the relevant parts of my setup that allows me to press F7 on a line and get a pdb.set_trace() line inserted. Shift+F7 removes it again. The debugging itself happens outside of vim (on the command-line where the program is executed), but has never let me down.

This implementation requires the brilliant ipdb, but should be easy enough to modify if/as necessary.

~/.vim/ftplugin/python/python.vim:

...
map <S-F7> :py RemoveBreakpoints()<CR>
map <F7> :py SetBreakpoint()<CR>
...

~/.vim/ftplugin/python/custom.py:

...
def SetBreakpoint():
    nLine = int( vim.eval('line(".")') )

    strLine = vim.current.line
    strWhite = re.search('^(\s*)', strLine).group(1)

    vim.current.buffer.append(
        (
            "%(space)spdb.set_trace() %(mark)s Breakpoint %(mark)s" %
            {'space': strWhite, 'mark': '#' * 30}
        ),
        nLine - 1
    )

    for strLine in vim.current.buffer:
        if strLine == "import ipdb as pdb":
            break
    else:
        vim.current.buffer.append('import ipdb as pdb', 2)
        vim.command('normal j1')
    vim.command('write')

def RemoveBreakpoints():
    nCurrentLine = int( vim.eval('line(".")') )

    nLines = []
    nLine = 1
    for strLine in vim.current.buffer:
        if strLine == 'import ipdb as pdb' or strLine.lstrip().startswith('pdb.set_trace()'):
            nLines.append(nLine)
        nLine += 1

    nLines.reverse()

    for nLine in nLines:
        vim.command('normal %dG' % nLine)
        vim.command('normal dd')
        if nLine < nCurrentLine:
            nCurrentLine -= 1

    vim.command('normal %dG' % nCurrentLine)
    vim.command('write')
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.