diff --git a/units/03 - Introduction to Flask/Introduction to Flask Slides.md b/units/03 - Introduction to Flask/Introduction to Flask Slides.md index 4ecb746..ec518b5 100644 --- a/units/03 - Introduction to Flask/Introduction to Flask Slides.md +++ b/units/03 - Introduction to Flask/Introduction to Flask Slides.md @@ -835,8 +835,6 @@ This is an anchor to a section in the webpage returned by the web server. This i --- - - @@ -980,3 +978,153 @@ You can use both absolute and relative URLs in anchor tags. ``` --- + +#### Query Parameters and Request Class In Flask + + + + + +--- + +#### A Quick Review of URL's And Schemes + + + + + +--- + +#### What Is HTTP/HTTPS? + + + + + +--- + +#### Website Speak In HTTP + + + + + +--- + +#### In Flask, We Access HTTP Requests With The `request` Class + +`http://127.0.0.1:5000/hello?name=Brady` + +```python +from flask import Flask, request + +app = Flask(__name__) + +@app.route('/hello') +def hello(): + name = request.args.get('name') + return "Hello, " + name + +app.run() +``` + +**Notice that request.args is a dictionary! Remember those?** + +```python +my_dictionary = {"name": "Brady"} +``` + +--- + +#### Request looks like this + + + + + +--- + +#### Working With Multiple Arguments + + + + + +```python + +from flask import Flask, request + +app = Flask(__name__) + +@app.route('/hello') +def hello(): + return "Here are all your args:" + str(request.args) + +app.run() +``` + +**Looks similar to** + +```python +my_dictionary = {"name": "Brady", "age": 31, "city": "Salt Lake City"} +``` + +--- + +#### Handling User Input with Query Parameters + +```python +from flask import Flask, request + +app = Flask(__name__) + +@app.route('/greet') +def greet(): + name = request.args.get('name') + if name: + return "Hello, " + {name} + else: + return 'Please enter your name.' + +app.run() +``` + +--- + +#### Query parameters are Often Used to Filter, Sort or Search For Data + +```python + +from flask import Flask, request + +app = Flask(__name__) + +# A list of data in JSON format +people = [ + {"name": "Brady", "age": 31}, + {"name": "Marcos", "age": 31}, + {"name": "Nephi", "age": "??"}, +] + +# A route that filters the data by a 'name' query parameter +@app.route('/people') +def filter_people(): + # Get the 'name' query parameter from the URL + name = request.args.get('name') + + if name is None: + return str(people) + + # Filter the data by name, if name is provided, using a loop + filtered_people = [] + for person in people: + if person['name'] == name: + filtered_people.append(person) + + + return str(filtered_people) + +app.run(debug=True) + +# http://localhost:5000/people?name=John +``` diff --git a/units/03 - Introduction to Flask/Introduction to Flask Slides.pdf b/units/03 - Introduction to Flask/Introduction to Flask Slides.pdf index 6c36ef7..6e1f9cc 100644 Binary files a/units/03 - Introduction to Flask/Introduction to Flask Slides.pdf and b/units/03 - Introduction to Flask/Introduction to Flask Slides.pdf differ diff --git a/units/03 - Introduction to Flask/assets/Chrome Request.png b/units/03 - Introduction to Flask/assets/Chrome Request.png new file mode 100644 index 0000000..9d24c0a Binary files /dev/null and b/units/03 - Introduction to Flask/assets/Chrome Request.png differ diff --git a/units/03 - Introduction to Flask/assets/Example Url.png b/units/03 - Introduction to Flask/assets/Example Url.png new file mode 100644 index 0000000..2167a38 Binary files /dev/null and b/units/03 - Introduction to Flask/assets/Example Url.png differ diff --git a/units/03 - Introduction to Flask/assets/Flask Header.png b/units/03 - Introduction to Flask/assets/Flask Header.png new file mode 100644 index 0000000..d60f7fb Binary files /dev/null and b/units/03 - Introduction to Flask/assets/Flask Header.png differ diff --git a/units/03 - Introduction to Flask/assets/HTTP Request Example.png b/units/03 - Introduction to Flask/assets/HTTP Request Example.png new file mode 100644 index 0000000..e0e7712 Binary files /dev/null and b/units/03 - Introduction to Flask/assets/HTTP Request Example.png differ diff --git a/units/03 - Introduction to Flask/assets/Query Param Example.png b/units/03 - Introduction to Flask/assets/Query Param Example.png new file mode 100644 index 0000000..bf52602 Binary files /dev/null and b/units/03 - Introduction to Flask/assets/Query Param Example.png differ diff --git a/units/03 - Introduction to Flask/assets/Website Traffic.png b/units/03 - Introduction to Flask/assets/Website Traffic.png new file mode 100644 index 0000000..2e41029 Binary files /dev/null and b/units/03 - Introduction to Flask/assets/Website Traffic.png differ