Quiz

  1. Can you explain a bit more on how static javascript file, python file, and html file works together? I dont really understand the Flask-Static files page.

    A web page often has "supporting" files, like CSS (often several of those), JS (several of those) and images (logos, branding, etc). (Do a "view source" on this page.)

    All of those are static, and can be simply put in a static folder and sent to the browser when it requests them.

    Flask takes care of all of that for us.

  2. Can you further explain the example of the Flask render_template function to combining the static template file with the random dynamic data?

    Sure. The template file is an HTML page, but with places in it for filling in data, marked with double braces, like:

    
    <h1>This is the home page for {{username}}</h1>
    
    
  3. A bit confused, is a form embedded in a static page, or is a form a completely independent thing?

    You were right the first time. A form is written using the HTML language, which we typically put in template files. It might have some dynamic parts, but it's mmostly static. We'll see some examples today.

  4. How is url_for used in static files? / Could you go over generating urls for static files? Where are they used?

    Glad to. An HTML template file might look like:

    
    <!doctype html>
    <head>
        <title>Homepage</title>
        <link type="text/css"
              rel="stylesheet"
              href="{{ url_for('static', filename = 'style.css') }}">
        <script src="{{ url_for('static', filename = 'frontend.js') }}">
    </head>
    <header><img src="{{ url_for('static', filename = 'homepage-logo.png') }}" alt="logo"></header>
    <h1>This is the home page for {{username}}</h1>
    
    
  5. Should we always avoid having HTML content in our python files ?

    Yes. I can imagine small snippets, but mostly our responses will be complete pages, with CSS and JS and images and stuff like that, so too bulky and static to clutter up our app.py with.

  6. How do you distinguish between using GET and POST methods? / Do the different encoding of get and post data mean anything in practice?

    We use GET when the purpose the request (the URL or the form submission) is to GET some information from the server.

    We use POST when the purpose of the request (the form submission) is to POST (upload, update, insert, delete) some information to the server.

    A GET request (URL) can be meaningfully saved, bookmarked, sent to a friend, etc. The page that shows a book on Amazon.com.

    A POST is a one-time thing. (Typically) The page that orders a book on Amazon.com

  7. How does Flask handle data differently in GET and POST requests?

    It puts them in different dictionary-like objects:

    • request.args for GET
    • request.form for POST
  8. How are request.args and request.form different in how they hold data? And how do we access each when there is a lot of data?

    They are identical as data structures; you use them the same way.

    I usually use the .get() method, since that won't raise an exception if the data is missing.

    
        if request.method == 'GET':
            data = request.args
        else:
            data = request.form
        # these will print None if the data is not in the request
        print(data.get('username'))
        print(data.get('home_address'))
        print(data.get('zip_code'))
    
    
  9. Are there kinds of expression/statements jinja2 cannot do?

    I haven't found any, but I haven't tried to do anything super fancy (e.g. recursion over a tree). It's intended for rendering data structures, so iteration and conditionals is about as fancy as I've ever needed.

  10. I would appreciate another review of the template language / some chances to practice.

    We'll do some now!