0

I have a command created with flask-script and I want to debug it with VSCode debugger.

But when I run the VScode Debugger configured for Flask, it simply executes the command without stopping at the breakpoint.

But, when I specify breakpoint() in the code, it execution pauses as desired.

.vscode/launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Flask: Debug",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/app.py",
            "args": [
                "runserver"
            ]
        }        
    ]
}

app.py

from distutils.debug import DEBUG
from flask import Flask, jsonify,request
# from flask_sqlalchemy import SQLAlchemy
import os
import click

from flask_script import Manager,Command,Server


from commands import CustomCommand

application = Flask(__name__)

manager = Manager(application)


class CustomServer(Server):
    def __call__(self, app, *args, **kwargs):
        #Hint: Here you could manipulate app
        return Server.__call__(self, app, *args, **kwargs)

# manager.add_command('db', MigrateCommand)



manager = Manager(application)
manager.add_command('runserver', CustomServer())

manager.add_command('custom_command', CustomCommand)

if __name__ == '__main__':
    manager.run()

command.py

# Custom flask script command
from flask_script import Command, Option

class CustomCommand(Command):

    def run(self):
        """Run job"""
        print("Running Command. . .")
        print("Ok")

I run the command with:

python app.py custom_command

When I place VSCode breakpoint at the print command inside the run() method of CustomCommand class, it does not pause there.

What could be the issue and the fix? Thanks in advance.

Aadarsha
  • 176
  • 1
  • 13
  • Can you provide a Minimal, Reproducible Example: https://stackoverflow.com/help/minimal-reproducible-example – MingJie-MSFT May 19 '22 at 07:07
  • Your code didn't use the method "run" in your app.py. You define "manager = Manager(application)", "run" in manager.run() is not the "run()" you defined in command.py – MingJie-MSFT May 19 '22 at 08:00
  • Apologies for not providing context on how I run the command. I run the command with `python app.py custom_command`. It works fine. The command is executing properly, but when I insert preakpoint in VSCode, the execution does not stop in the breakpoint. – Aadarsha May 19 '22 at 08:15
  • You may not understand what I mean. The code you provided is not used the method "run()". It will certainly not execute the contents under the method "run()" – MingJie-MSFT May 19 '22 at 08:23
  • It is running in my localmachine as we are using `flask_script.Manager` to instantiate `manager` and adding command using `manager.add_command('custom_command', CustomCommand)`. So, when I run `python app.py custom_command`, it invokes CusomCommand.run() as well. It is running without any issues in my local machine. – Aadarsha May 19 '22 at 08:37
  • It will load this method instead of executing it. You can put the breakpoint at line "def run(self):". Then it will stop. We can move to chat for more discussion. – MingJie-MSFT May 19 '22 at 08:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/244867/discussion-between-mingjie-msft-and-aadarsha). – MingJie-MSFT May 19 '22 at 08:47
  • I figured this out. YAY!!! Thank you for your time and response, @MingJie-MSFT . So the issue was in launch.json where I have to run the command instead of runserver, then VSCode breakpoints will work. Also, the command executes as flask_script executes the run() method. Thanks for your responses. Will update the snippet that worked for me in the answer below. – Aadarsha May 19 '22 at 08:52

1 Answers1

0

So, the problem was in the vscode/launch.json configuration.

It seems that VSCode only looks for breakpoints when the command is executed through launch.json file. I added the configuration for custom_command and the code execution paused on the inserted breakpoint.

Here is my updated launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Flask: Debug",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/app.py",
            "args": [
                "runserver"
            ]
        },
        {
            "name": "Flask: Custom Command",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/app.py",
            "args": [
                "custom_command"
            ]
        }        
    ]
}
Aadarsha
  • 176
  • 1
  • 13