Using Wing with Flask

Index of All Documentation » How-Tos » How-Tos for Web Development »

Wing Pro Screenshot

Wing is a Python IDE that can be used to develop, test, and debug Python code written for the Flask web development framework.

Two versions of Wing are appropriate for use with this document: Wing Pro is the full-featured Python IDE for professional developers, and Wing Personal is a free alternative with reduced feature set.

If you do not already have Wing installed, download it now.

This document describes how to configure Wing for Flask. To get started using Wing as your Python IDE, please refer to the tutorial in Wing's Help menu or read the Quickstart Guide.

Project Configuration

To create a new project, use New Project in Wing's Project menu. Select the project type Flask and enter the Python Executable to use. If you are using Flask in a virtualenv or Anaconda environment, select Activated Env and enter the command that activates the environment (for example, \path\to\env\Scripts\activate, /path/to/env/bin/activate, or Anaconda's activate env). The drop down menu to the right of this field lists recently used and automatically found environments. If you are not using a named environment, select Command Line instead and then enter the full path of the Python you plan to use with Flask. You can determine the correct value to use by executing the following commands in Python:

import sys

Press OK and then add the directory with your source code to the new project with Add Existing Directory in the Project menu.

Remote Development

Wing Pro can work with Flask code that is running on a remote host, VM, or container. To do this, you need to be able to connect to the remote system with SSH. Then you can create your project in the same way as above, using the Connect to Remote Host via SSH project type. See Remote Hosts for more information on remote development with Wing Pro.

Debugging Flask in Wing

To debug Flask in Wing you need to turn off Flask's built-in debugger, so that Wing's debugger can take over reporting exceptions.

To do this, you can set up your main entry point as in the following example:

from flask import Flask
app = Flask(__name__)


if __name__ == "__main__":
    import os
    if 'WINGDB_ACTIVE' in os.environ:
        app.debug = False

Notice that this turns off Flask's debugging support only if Wing's debugger is present.

Once this is done, use Set Current as Main Entry Point in the Debug menu to set this file as your main entry point. Then you can start debugging from the IDE, see Flask's output in the Debug I/O tool, and load pages from a browser to reach breakpoints or exceptions in your code.

Use Restart Debugging in the Debug menu or the restart icon in the toolbar to quickly restart Flask after making changes to your code. Or if you have Wing Pro you can automate this as described in the next section.

Setting up Auto-Reload with Wing Pro

With the above configuration, you will need to restart Flask whenever you make a change to your code. If you have Wing Pro, you can avoid this by replacing the line in the above example with the following:

Then enable Debug Child Processes under the Debug/Execute tab in Project Properties from the Project menu. This tells Wing Pro to debug also child processes created by Flask, including the reloader process.

Now Flask will automatically restart on its own whenever you save an already-loaded source file to disk.

You can add additional files for Flask to watch as follows:

watch_files = ['/path/to/file1', '/path/to/file2'], extra_files=watch_files)

Whenever any of these additional files changes, Flask will also automatically restart.

Related Documents

For more information see: