Lecture 3.1 (Tuesday, Jan. 27)

Note

In this class, we used the animals example.

Logistics

  • We're slipping some things — need to adjust the pacing
  • A1 due next Tuesday
  • Q1 slipped to next week
  • Schedules pushed back a bit, but hopefully we get back on target
  • Will slip at least one of P1, P2.

Homework advice:

  • Better to turn in working but incomplete code than something broken
  • I don't dig through crashes, generally.
  • Always view through the server

Jinja and Python

Let's talk about routes, Python, and Jinja. And expressions.

Routing URLs

What happens when the browser says GET /plays/12night/?

@app.route('/plays/<play_id>/')
def get_play(play_id):
    # The play_id variable contains the play ID
    pass

Some comments:

  • The play_id variable contains the play ID
  • We call it a parameter to the function
  • It functions exactly like a local variable
  • The <play_id> element of the route URL means ‘extract thing from here, pass it in as the play_id parameter’

So we can run Python code, and look up and display the play that is called with play_id.

Expressions

What's an expression?

A way of writing a value (or, in Python terms, an object).

Most programming languages have two categories of things:

  • statements take actions
  • expressions represent or compute values
  • Generally, an expression can stand on its own as a statement

What are some expressions?

  • Literals
    • 'hello' or "goodbye" (strings)
    • 3 (ints)
    • 7.2 (doubles)
  • Variable references: foo
  • Arithmetic and string operations: count + 5, 'foo' + 38
  • List constructors: ['foo', 3]
  • Dict constructors: {'foo': 38}
  • Function calls: math.abs(30)
  • Dict or list lookups: plays[play_id]

Crucial insight: there is nothing special about any particular kind of expression. If you can use a string literal, you can use any other expression that evaluates to a string, including a variable reference or a function call.

So, dict values can be any object, and can be assigned using any expression.

What is a variable? It's just a name, that holds some value.

The value that it holds can be freely changed by reassigning the variable.

Jinja

So we've seen a bit of Jinja templating.

Inserting variables:

{{ variable }}

Can actually insert any expression, where expressions are Jinja expressions (which are almost, but not quite, entirely unlike tea like Python expressions)

Blocks:

{% for foo in bar %}
{# In here, `foo` is a variable containing one element of `bar` #}
{% endfor %}

How does data get into Jinja?

Variables are defined in the render_template call:

return flask.render_template('template.html', data=some_data)

The left-hand side — data — is the name of the variable as it will appear in Jinja. The right hand side is any Python expression, the result of which is bound to the variable in Jinja.

Additional Matters

In the assignment, you need to figure out a few things:

  • what scene
  • whether there is a next scene, and if so, what it is
  • note, if you open one of the YAML files in PyCharm, it won't open the whole thing.