<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>pro+</product>
<summary>Learn how Wing Pro speeds 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 Pro <https://wingware.com>`__ is a Python IDE that can be used to develop, test, and
debug Python code written for the `Flask <https://palletsprojects.com/p/flask/>`__ web
development framework.

If you do not already have Wing Pro installed, `download it now <https://wingware.com/downloads/wingpro>`__.

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. Choose ``Use
Existing Directory`` or ``Create New Directory`` to select or create your source directory
and then choose the ``Project Type`` of ``Flask``. Press ``Next`` and then select or create
the Python environment to use with your new project.

**Using an Existing Python Installation or Environment**: Select ``Use Existing Python``
and ``Command Line`` to use an existing Python installation or environment. This should be
set to the value of ``sys.executable`` (after ``import sys``) from the Python you wish to
use.

Or, if you are using Flask in an existing virtualenv or Anaconda environment, select
``Activated Env`` instead and enter the command that activates
the environment (for example, ``c:\path\to\env\Scripts\activate.bat``,
``/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. 

Note that using an activate command whose full path contains spaces will not work. In this
case, use the ``Command Line`` option as described above.

When an existing Python installation or environment is used, Flask is assumed to already
be installed into it.  If this is not the case, you can install it from the ``Packages``
tool after your project has been set up.

**Creating a New Environment**: You can also create a new virtualenv, Poetry env, uv env,
pipenv, conda env, or
Docker environment from here along with your project, as described in more detail in
`Creating Python Environments <ref:proj/creating-python-envs>`__.

When you have finished selecting your Python environment, press ``Create Project`` to
create your new project. If you are using an existing source directory, you will need to
save the project to disk using ``Save Project`` in the ``Project`` menu. If you created a
new source directory, the project will automatically be stored there.

Remote Hosts and VMs
....................

Wing Pro can work with Flask code that is running on a remote host or VM. 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, after first setting the ``Host`` in the ``New Project`` dialog.
See `Remote Hosts <ref:proj/remote-hosts>`__ for more information on remote development
with Wing Pro.

Containers
..........

For containers, select ``Local Host`` on the first page of the ``New Project`` dialog,
then ``Create New Environment`` on the second page, and set up a new ``Docker`` container.
For details, see `Using Wing Pro with Docker <ref:howtos/docker>`__.

Port Forwarding
...............

When a remote host or container is used with your project, Wing sets up port forwarding in
the remote host or container configuration created with your project, so that you can
connect to the Flask instance using a browser running on the same host as the IDE.

For example, if Flask is listening on port 8000 on the remote host or container, Wing
forwards port 8000 on localhost to the remote system, and the url you will use to access
Flask from the host where Wing is running is: ``http://localhost:8000``.

You can view and alter the port forwarding configuration on the ``Options`` page of the
remote host or container configuration.

Setting up Auto-Reload
----------------------

If you started a new Wing project with an existing code base, you should configure it to
auto-reload changed code by adding ``use_reloader=True`` to your ``app.run()`` call, as
follows:

.. python::

      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:

.. python::

      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.

Turning off Flask's Debugger
----------------------------

If you are starting to use Wing with an existing Flask source base and you see Flask
report exceptions in your browser and not in Wing's debugger, then you will need to
turn off Flask's built-in debugger.

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

.. python::

  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
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.

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

For more information see:

* `Flask home page <https://palletsprojects.com/p/flask/>`__ 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.

