<type>how-to</type>
<domain>install</domain>
<id>flask</id>
<title>Using Wing with Flask</title>
<date>$Date: 2006-12-28 17:21:05 -0500 (Thu, 28 Dec 2006) $</date>
<product>per+</product>
<summary>Learn how Wing can be used to speed up development and debugging of Python code written for the Flask web development framework.</summary>

.. image:: /images/screenshots/wing7-screenshot-linen.png
  :alt: Wing Screen Shot
  :width: 574
  :height: 423
  :popup: 1

`Wing <https://wingware.com>`__ is a Python IDE that can be used to develop, test, and
debug Python code written for the `Flask <http://flask.pocoo.org/>`__ 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 <https://wingware.com/downloads>`__.

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 <ref:howtos/quickstart>`__.

Project Configuration
---------------------

To create a new project, use ``New Project`` in Wing's ``Project menu. Select the project
type ``Flask`` and under ``Python Executable`` select ``Custom`` 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 interactively in Python.  If you are using
virtualenv, this will be the virtualenv's Python executable::

  import sys
  sys.executable

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 <ref:proj/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
      app.run()
    
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 ``app.run()`` line in
the above example with the following::

      app.run(use_reloader=True)

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']
      app.run(use_reloader=True, extra_files=watch_files)

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

Related Documents
-----------------

For more information see:

* `Flask home page <http://flask.pocoo.org/>`__ provides links to documentation.

* `Quickstart Guide <ref:howtos/quickstart>`__ contains additional basic information
  about getting started with Wing.
  
* `Tutorial <ref:intro/tutorial>`__ provides a gentler introduction to Wing's features.

* `Wing Reference Manual <ref:manual>`__ documents Wing in detail.

