Thursday, October 31, 2019

Wingware Blog: Efficient Flask Web Development with Wing 7

Wing can develop and debug Python code running under Flask, a web framework that is quick to get started with and easy to extend as your web application grows.

To create a new project, use New Project in Wing's Project menu and select the project type Flask. If Flask is not installed into your default Python, you may also need to set Python Executable to the full path of the python or python.exe you want to use. This is the value of sys.executable (after import sys) in the desired Python installation or virtualenv.

Next, add your files to the project with Add Existing Directory in the Project menu.

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. This is done by setting the debug attribute on the Flask application to False:

app.debug = False

Then use Set Current as Main Entry Point in the Debug menu to set your main entry point, so you can start debugging from the IDE even if the main entry point file is not visible in the editor.

Once debug is started, you can load pages from a browser to reach breakpoints or exceptions in your code. Output from the Flask process is shown in Wing's Debug I/O tool.

Example

Here's an example of a complete "Hello World" Flask application that can be debugged with Wing:

import os
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "<h3>Hello World!</h3><p>Your app is working.</p>"

if __name__ == "__main__":
    if 'WINGDB_ACTIVE' in os.environ:
        app.debug = False
    app.run()

To try it, start debugging it in Wing and use the URL printed to the Debug I/O tool to load the page in a web browser. Setting a breakpoint on the return statement will stop there whenever the page is reloaded in the browser.

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, either with Restart Debugging in the Debug menu or with the restart toolbar icon.

If you have Wing Pro, you can avoid the need to restart Flask by telling it to auto-restart when code changes on disk, and configuring Wing to automatically debug the restarted process.

Flask is configured by adding a keyword argument to your app.run() line:

app.run(use_reloader=True)

Wing is configured by enabling 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, and Wing will debug the restarted process. 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)


That's it for now! We'll be back soon with more Wing Tips for Wing Python IDE.

As always, please don't hesitate to email support@wingware.com if you run into problems or have any questions.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...