Migwell
Migwell

Reputation: 20117

How do you debug url routing in Flask?

I'm developing a Flask website using Visual Studio 2013's PythonTools, which has its own debugger, and that allows me to step through the initial setup code, up until app.run()

However the code I want to debug is the routing code, like this:

@app.route('/')
def url_index():
     return render_template('index.html') 

I know that function is running because the server does respond with index.html, but if I put a breakpoint on the last line, it'll never get hit.

Is there any way to debug these routing functions? Flask says it comes with a debugger but how do I use it? Will it be compatible with Visual Studio?

Upvotes: 11

Views: 8680

Answers (4)

T3hc13h
T3hc13h

Reputation: 602

You can turn off reloading with debug mode by using

app.run(debug=True, use_reloader=False)

The Flask error handling docs go into the details of the debugging options.

Upvotes: 1

Migwell
Migwell

Reputation: 20117

6 months later, and while it still doesn't look possible to automatically debug URL routing in flask, you can manually attach a debugger to the flask process, though you'll have to re-add it if you restart the server or if the auto-reloader detects changes in your .py files and restarts.

Just go: Tools -> Attach to Process and select the Python.exe that is not greyed out (that's the initial flask code that visual studio is already debugging), and then do something that would cause the breakpoint to be hit (e.g. reload the page), and you should have success.

Upvotes: 4

roim
roim

Reputation: 4930

Sadly the current version of PTVS doesn't support Flask projects.

Good thing is: the already released PTVS 2.1 alpha does: http://pytools.codeplex.com/wikipage?title=Flask

Upvotes: 2

atupal
atupal

Reputation: 17210

For the Flask debugger, you can set app.debug to True:

app.debug = True

or

app.run(debug=True)

And then:

@app.route('/')
def index():
   raise
   return render_template('index.html') 

And then you can debug the function with the Flask debugger in your browser.

Upvotes: 4

Related Questions