List of Todos

We aren’t really using any data yet. Let’s make a small step in that direction. Along the way, we’ll see other PyCharm ways to execute code: the Python Console and the Terminal tool.

Source for this step View video/audio walkthrough

Steps

  1. On the line after defining app, let’s add a todo list with two todo items in it:

    todos = [dict(id=1, title='First'), dict(id=2, title='Second')]
    

    Note

    The code line above extends to the right. Make sure you copy to the end of the line.

  2. Our list_todos view needs to take these list entries, convert it to a string of HTML, and return it:

    def list_todos():
        div = '<div><a href="/todo/{id}">{title}</a></div>'
        items = [div.format(id=t['id'], title=t['title']) for t in todos]
        return '\n'.join(items)
    
  3. To see if this works, we could of course just reload it in the browser. Let’s use interactive Python to poke around. Select Tools -> Python Console.

  4. In the Python Console tool window:

    from app import todos, list_todos
    todos
    list_todos()
    
  5. Click the red X in the Python Console tool window to close the Python Console.

  6. Open PyCharm Preferences with Ctrl-Alt-S. (macOS: Cmd-,)

  7. Navigate to Build, Execution, Deployment -> Console -> Python Console and look at the options, then click Cancel to dismiss the Preferences popup.

  8. Open the command line by clicking in the bottom of the screen in Terminal.

  9. PyCharm starts you in the project directory. Let’s take a look at where PyCharm stores its data for the project preferences:

    > dir .idea
    
  10. Click the red X in the Terminal tool window to close the terminal.

  11. Reload your browser and confirm that our data-driven approach works.

  12. Your app.py should match the following:

    app.py in List of Todos
    from flask import Flask
    
    app = Flask(__name__)
    todos = [dict(id=1, title='First'), dict(id=2, title='Second')]
    
    
    @app.route('/')
    def home_page():
        return 'Hello World! <a href="/todo/">Todos</a>'
    
    
    @app.route('/todo/')
    def list_todos():
        div = '<div><a href="/todo/{id}">{title}</a></div>'
        items = [div.format(id=t['id'], title=t['title']) for t in todos]
        return '\n'.join(items)
    
    
    @app.route('/todo/<todo_id>')
    def show_todo(todo_id):
        return 'Todo {todo_id}'.format(todo_id=todo_id)
    
    
    if __name__ == '__main__':
        app.run(debug=True)
    

Analysis

PyCharm has the Run Tool (and as we’ll see later, the Debugger Tool) for executing your project’s code. But the interactive prompt is also useful:

  • Python Console. Get to a Python prompt, in the context of your project.
  • Terminal. Get to the Windows, macOS, or Linux console prompt, with the working directory set to your project root.

Extra Credit

  1. Can you execute just a selection of app.py code in the Python Console?
  2. iPython has a great command line interface for interactive Python. Can PyCharm’s Python Console use it?
  3. Is there a way to auto-activate your virtual environment when opening a Terminal?
  4. Python extra credit...will Python 3.6 make the .format() less cumbersome? Passing in the keyword arguments is no fun.