After use some cool features of Neovim kickstart. I try debugging a flask app.

After installing Neovim and some plugins. Now I want to try debugging Python project. I created a small Flask app from template with a few apis. And here’s the result. Amazing!

Here’s how to Link to heading

Prepare a Flask app Link to heading

I use this template to generate a Flask app repo.

Then I clone my new repo and install the requirements.

After that I run the app and test an endpoint to make sure it works.

Even though this app is simple, as long as we can debug this app, we can debug any complex real world Flask app.

Install debugpy with Mason Link to heading

Open up :Mason dialog and go to DAP tab with number 3 key. Search for debugpy and press i to install.

Then I create a file at ~/.config/nvim/lua/custom/plugins/dap-python.lua to add my new plugin config as follow.

return {
  'mfussenegger/nvim-dap-python',
  config = function ()
  require('dap-python').setup('~/.local/share/nvim/mason/packages/debugpy/venv/bin/python')
  end,
}

nvim-dap-python is a DAP plugin to connect between debugpy and nvim-dap. I need to specify path to python binary where debugpy was installed. Because it was installed by Mason, I need to search for it under ~/.local/share/nvim/mason/packages folder. Luckily I found it 😄 Because this path is quite generic, I leave it as is. Wait for all plugins are installed and restart nvim.

Start debugging Link to heading

Now I set some breakpoints with SPACE b key, and then hit F5.

Select option 1 to debug current file.

Breakpoint hits and we have multiple debug windows. Watches, Callstacks, Breakpoints, Scope variable and Console. I let it continue with F5 key again. If you notice, I also set an other breakpoint int api.py file. So I make a request to /api again.

And yes! The breakpoint hit. This proves that we can use Neovim to write code and debug it.

Debug run.py while editing any file Link to heading

To always be able to start debugging run.py while editing any files. I need to add a .vscode/launch.json file.

{
   "version": "0.2.0",
   "configurations": [
       {
           "type": "python",
           "request": "launch",
           "name": "Launch flask app",
           "program": "run.py"
       }
   ]
}

And then run :lua require('dap.ext.vscode').load_launchjs() to load the new configuration. Now when I press F5, there will be a new option to Launch flask app.