54
http://bit.ly/2FYAerq Sandbox!

2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

http://bit.ly/2FYAerq

Sandbox!

Page 2: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

FlaskJosh Archibald - January 17, 2020

[email protected]

Page 3: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Why Flask?

Page 4: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls
Page 6: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Client-side

Page 7: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Server-side

Page 8: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Client Server

● User interface○ Structure/content (HTML)○ Design (CSS)○ Interaction (JS)

● All done by the user’s browser

● Generally, anything to do with stored information○ User registration/login○ Storing info in a database○ Processing orders

● Done on a server

Page 9: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Flask● Simple microframework for server-side programming● Uses Python● Can work with a SQL database (and many others)● Dynamic HTML generation using Jinja templating language● Used in industry (https://stackshare.io/flask)

○ Netflix○ Lyft○ Zillow

● https://flask.palletsprojects.com/en/1.1.x/

Page 10: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

MVC

Page 11: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

M - Model - data-related logic (SQL)

V - View - the user interface presented (HTML/CSS/JS)

C - Controller - the interface between the model and view (Python)

Page 12: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Quick Python review● Functions● Modules● Dictionaries

Page 13: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

FunctionsIn functions.py,

● Write a function, square(n), that returns the square of n. For example, square(3) should return 9.

● Write a function, main(), that prints the squares of the numbers 0-9.

Page 14: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

ModulesIn modules.py,

● Import the square function from functions.py.● Print out the value of 10 squared, using the square function you just imported.

Page 15: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

DictionariesIn dictionary.py,

● Create a new dictionary mapping first names to ages. For our purposes, let’s say that Renee is 19 and Fernando is 29.

● Turns out that our data was wrong for Fernando -- he’s actually 23. Write a second line of code that will just change Fernando’s age to the correct number.

● This weekend is Renee’s birthday. Write a third line of code to increment Renee’s age by one.

● Finally, print the ages dictionary to see if your code works properly.

Page 16: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Routes

Page 17: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Routes“bind a function to a URL”

Page 18: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

cs50.harvard.edu/hls

Page 19: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

cs50.harvard.edu/hls

Page 20: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

cs50.harvard.edu/

Page 21: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

cs50.harvard.edu/

Page 22: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Example: Hello, World!

Page 23: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Example: Different routes

Page 24: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

/ /helloHello world! Hello Josh!

Page 25: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Routes with variables

Page 26: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

@app.route("/hello/<string:name>")

Page 27: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

@app.route("/hello/<string:name>")

Page 28: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

@app.route("/hello/<string:name>")

Page 29: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

@app.route("/hello/<string:name>")

Page 30: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Example: Hello, __

Page 31: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

/ /hello/Josh /hello/Fernie /hello/VibhaHello world! Hello Josh! Hello Fernie! Hello Vibha!

Page 32: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Templates

Page 33: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Intro to Jinja templating: extending a layoutlayout.html

{% block content %} {% endblock %}

index.html

{% extends "layout.html" %}{% block content %}<h1>Something goes here!</h1>{% endblock %}

Page 34: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Example: Templates, Inheritance

Page 35: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Static HTML<!DOCTYPE html><html lang="en"> <head> <meta charset="utf-8"> <title>Hello world</title> </head> <body> <h1>Hi there!</h1> </body></html>

Page 36: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Static HTML<!DOCTYPE html><html lang="en"> <head> <meta charset="utf-8"> <title>Hello world</title> </head> <body> <h1>Hi there!</h1> </body></html>

Page 37: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Intro to Jinja templating: logic in HTML!?{{ variable_name }}

{% if variable_name == "test" %}…{% else %}...{% endif %}

https://jinja.palletsprojects.com/en/2.10.x/

Page 38: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Example: Using variables in templates

Page 39: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

/ /hello/Josh /hello/Fernie<h1>Hello world!</h1> <h1>Hello Josh!</h1> <h1>Hello Fernie!</h1>

Page 40: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

POST vs. GET

Page 41: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

GET request examplehttps://www.google.com/search?q=Budapest

Page 42: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

GET request examplehttps://www.google.com/search?q=Budapest

https://bad.security.net/login?username=jsmith&password=dolphin

Page 43: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Example: Form submissions

Page 44: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

/

Name: Josh SUBMIT. <h1>Hello Josh!</h1>

Page 45: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Example: User registration and login

Page 46: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Sessions

Page 47: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Hashes

Page 48: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

MD5 (don’t use!)● password → 5f4dcc3b5aa765d61d8327deb882cf99● Elena Kagan → bfb23925528b7fa63e77aeed31c3532e● dolphin → 36cdf8b887a5cffc78dcd5c08991b993● Dolphin → 76f7843c2bc545f9ef3a174e319ea4e2● ...

Page 49: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

MD5 (don’t use!)● password → 5f4dcc3b5aa765d61d8327deb882cf99● Elena Kagan → bfb23925528b7fa63e77aeed31c3532e● dolphin → 36cdf8b887a5cffc78dcd5c08991b993● Dolphin → 76f7843c2bc545f9ef3a174e319ea4e2● ...

Page 50: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

A good hash function should● Use only the data being hashed● Use all of the data being hashed● Be deterministic (same result every time given same input; no randomness!)● Uniformly distribute data● Generate very different hash codes for very similar data.

Page 51: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Principle of password security: compare hashes● Don’t store the passwords in plaintext -- if the database is hacked, passwords

are in the open● Use advanced hash functions (not MD5) to store the irreversible hashes

instead of passwords● Compare the hashes

Page 52: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

In the context of Flask...from werkzeug.security import check_password_hash, generate_password_hash

check_password_hash(stored_hash, password_plaintext)

generate_password_hash(password_plaintext)

Page 53: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

Example: User registration and login

Page 54: 2FYAerq - CS50...Renee’s age by one. Finally, print the ages dictionary to see if your code works properly. Routes Routes “bind a function to a URL” cs50.harvard.edu/hls cs50.harvard.edu/hls

FlaskJosh Archibald - January 17, 2020

[email protected]