@@ -835,8 +835,6 @@ This is an anchor to a section in the webpage returned by the web server. This i
835835
836836---
837837
838- <!-- _footer: 'Unit 3: Introduction to Flask (image source: https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Web_mechanics/What_is_a_URL' -->
839-
840838<span class =" centered " >
841839<img src =" assets/mdn-url-all.png " />
842840</span >
@@ -980,3 +978,153 @@ You can use both absolute and relative URLs in anchor tags.
980978```
981979
982980---
981+
982+ #### Query Parameters and Request Class In Flask
983+
984+ <span class =" center wide " >
985+ <img src =" assets/Flask Header.png " />
986+ </span >
987+
988+ ---
989+
990+ #### A Quick Review of URL's And Schemes
991+
992+ <span class =" centered " >
993+ <img src =" assets/Example Url.png " />
994+ </span >
995+
996+ ---
997+
998+ #### What Is HTTP/HTTPS?
999+
1000+ <span class =" centered " >
1001+ <img src =" assets/HTTP Request Example.png " />
1002+ </span >
1003+
1004+ ---
1005+
1006+ #### Website Speak In HTTP
1007+
1008+ <span class =" centered " >
1009+ <img src =" assets/Website Traffic.png " />
1010+ </span >
1011+
1012+ ---
1013+
1014+ #### In Flask, We Access HTTP Requests With The ` request ` Class
1015+
1016+ ` http://127.0.0.1:5000/hello?name=Brady `
1017+
1018+ ``` python
1019+ from flask import Flask, request
1020+
1021+ app = Flask(__name__ )
1022+
1023+ @app.route (' /hello' )
1024+ def hello ():
1025+ name = request.args.get(' name' )
1026+ return " Hello, " + name
1027+
1028+ app.run()
1029+ ```
1030+
1031+ ** Notice that request.args is a dictionary! Remember those?**
1032+
1033+ ``` python
1034+ my_dictionary = {" name" : " Brady" }
1035+ ```
1036+
1037+ ---
1038+
1039+ #### Request looks like this
1040+
1041+ <span class =" centered " >
1042+ <img src =" assets/Chrome Request.png " />
1043+ </span >
1044+
1045+ ---
1046+
1047+ #### Working With Multiple Arguments
1048+
1049+ <span class =" centered " >
1050+ <img src =" assets/Query Param Example.png " />
1051+ </span >
1052+
1053+ ``` python
1054+
1055+ from flask import Flask, request
1056+
1057+ app = Flask(__name__ )
1058+
1059+ @app.route (' /hello' )
1060+ def hello ():
1061+ return " Here are all your args:" + str (request.args)
1062+
1063+ app.run()
1064+ ```
1065+
1066+ ** Looks similar to**
1067+
1068+ ``` python
1069+ my_dictionary = {" name" : " Brady" , " age" : 31 , " city" : " Salt Lake City" }
1070+ ```
1071+
1072+ ---
1073+
1074+ #### Handling User Input with Query Parameters
1075+
1076+ ``` python
1077+ from flask import Flask, request
1078+
1079+ app = Flask(__name__ )
1080+
1081+ @app.route (' /greet' )
1082+ def greet ():
1083+ name = request.args.get(' name' )
1084+ if name:
1085+ return " Hello, " + {name}
1086+ else :
1087+ return ' Please enter your name.'
1088+
1089+ app.run()
1090+ ```
1091+
1092+ ---
1093+
1094+ #### Query parameters are Often Used to Filter, Sort or Search For Data
1095+
1096+ ``` python
1097+
1098+ from flask import Flask, request
1099+
1100+ app = Flask(__name__ )
1101+
1102+ # A list of data in JSON format
1103+ people = [
1104+ {" name" : " Brady" , " age" : 31 },
1105+ {" name" : " Marcos" , " age" : 31 },
1106+ {" name" : " Nephi" , " age" : " ??" },
1107+ ]
1108+
1109+ # A route that filters the data by a 'name' query parameter
1110+ @app.route (' /people' )
1111+ def filter_people ():
1112+ # Get the 'name' query parameter from the URL
1113+ name = request.args.get(' name' )
1114+
1115+ if name is None :
1116+ return str (people)
1117+
1118+ # Filter the data by name, if name is provided, using a loop
1119+ filtered_people = []
1120+ for person in people:
1121+ if person[' name' ] == name:
1122+ filtered_people.append(person)
1123+
1124+
1125+ return str (filtered_people)
1126+
1127+ app.run(debug = True )
1128+
1129+ # http://localhost:5000/people?name=John
1130+ ```
0 commit comments