List Todos Route

PyCharm’s editor allows, obviously, editing. But the left and right margins, aka the gutters, have rich functionality for helping you code. Let’s see these gutters in action by adding a second route.

Source for this step View video/audio walkthrough

Steps

  1. Open app.py in the editor, if it isn’t already opened.

  2. After the first route, add a second route with two errors and one warning:

    @appx.route('/todo/')    # Error
    def list_todos() :       # Warning, extra space before :
        x = 1                # Warning, unused variable
        return 'Todo List'
    
  3. Note the traceback in the Run tool window. Flask reloaded and Python exited due to SyntaxError: invalid syntax.

  4. The editor’s right gutter has two red lines (error) and one olive line (warnings). Mouse over each of these to see the actual issue.

  5. Correct the second error, the extra space before the colon, with PyCharm’s Ctrl-Alt-L (Reformat Code). (macOS: Cmd-Alt-L).

  6. Mouse over the x to see again the actual error.

  7. Click on the x.

  8. Click on the lightbulb that pops up and choose Remove statement.

  9. Change appx to app.

  10. Not only are the 3 gutter warnings gone, but there is a green checkbox at top. (Mouse over it for more detail.)

  11. Right-click in the left gutter, where the line numbers are shown.

  12. In the popup, turn off “Show Line Numbers”.

  13. Repeat to turn line numbers back on.

  14. To the left of def home_page, click the - to collapse the block. Click the + to expand.

  15. Add a link to the new route by changing def home_page to return 'Hello World! <a href="/todo/">Todos</a>'. app.py should now look like this:

    app.py
    from flask import Flask
    
    app = Flask(__name__)
    
    
    @app.route('/')
    def home_page():
        return 'Hello World! <a href="/todo/">Todos</a>'
    
    
    @app.route('/todo/')
    def list_todos():
        return 'Todo List'
    
    
    if __name__ == '__main__':
        app.run(debug=True)
    
  16. In the Run window, click the green play arrow button to start the now-corrected Flask application.

  17. Reload browser and click on the Todos link.

Analysis

The PyCharm IDE does a lot of work for you, and reserves areas on the screen for interacting with you on that work. The gutters are part of this.

  • Code analysis. PyCharm flags you when it finds something important. Errors and warnings are treated differently, showing up both in the gutter and in the editor. PyCharm Settings can be used to configure these errors and warnings.
  • Linting. The code analysis is driven by PyCharm’s support for various Python linting tools, including “PEP8” style guide tools.
  • Gutters. PyCharm puts these gutters to more use. For example, the left gutter is used for breakpoints (as discussed in Debugging), favorites, bookmarks, etc.

Extra Credit

  • Delete the flask import line, then have PyCharm generate the import for you by clicking on Flask(__name__) and hitting Alt-Enter.
  • What if you don’t want PEP8 issues to show up as warnings?
  • Can you turn off line numbers only for one tab?
  • Does the collapse/expand state survive closing and re-opening a file? Where is that information stored?