How to Use Flask’s Debug Mode

Introduction

If you’re brand new to web development with Python and Flask, you may be confused on how to debug your code in an IDEless environment. You may know Python, but you’re having trouble adjusting to the way errors are handled in both Flask and web development in general. When using Flask for the first time you may have even encountered this very unhelpful message when you’ve made a mistake:

Well, have no fear! In this quick guide I will show you how easy it is to debug your code in Flask.

Setting Up Debug Mode:

Python will automatically grab any syntax mistakes you may have in your Flask code, but it will not be able to find all your errors. This is where Flask’s intuitive debug mode comes in handy. All you have to do is write in like so:

Now whenever an error occurs in your code you’ll be thrown right into Flask’s debug mode.

Using Debug Mode:

When you have an error in your Flask application like I have here, you will be sent to a page that looks like this:

This page gives you a small description of your error, and even gives you a traceback to see what led to that error. But what you may not know is that you can take it further with debug mode’s console.

To use the console you will first need to hover over one of the lines in the traceback, and click on the little icon that looks like a console on the right.

Once you click on that you will be led to a page that looks like this:

In order to use Flask’s console and take a deeper look at your code you will need to find your console pin in the terminal of your editor. This will be right after where it says “Debug is active!” in your terminal. Enter your pin and you will be able to access a console like this where you can take a deeper look at all your variables etc.

And that’s the basics of debug mode in Flask! One thing to note is that you only want to use debug mode in production, because people will be able to see your traceback if they encounter an error. Instead just takeout the code that says , and instead of seeing your code they’ll just see the generic error message that we had before. Thanks for reading and I hope this helped!

Full-Stack Engineer with a background in Javascript, React.js, React Native, Redux, Ruby, and Ruby on Rails.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store