-
Notifications
You must be signed in to change notification settings - Fork 0
Add slides for HTTP Get requests and query params #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| --- | ||
| theme: css/custom-simple.css | ||
| highlightTheme: css/layout.css | ||
| --- | ||
|
|
||
| <!-- .slide: class="center" --> | ||
|
|
||
| # Intermediate Python | ||
|
|
||
| ### Unit 3: Introduction to Flask | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once this content gets moved to the other .md file you should be able to remove this whole slide. |
||
|
|
||
| --- | ||
|
|
||
| <!-- | ||
| paginate: true | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once this content gets moved to the other .md file you should be able to remove these comments. |
||
| footer: 'Unit 3: Introduction to Flask' | ||
| --> | ||
|
|
||
| #### Query Parameters and Request Class In Flask | ||
|
|
||
| <span class="center wide"> | ||
| <img src="assets/Flask Header.png" /> | ||
| </span> | ||
|
|
||
| --- | ||
|
|
||
| #### A Quick Review of URL's And Schemes | ||
|
|
||
| <span class="centered"> | ||
| <img src="assets/Example Url.png" /> | ||
| </span> | ||
|
|
||
| --- | ||
|
|
||
| #### What Is HTTP/HTTPS? | ||
|
|
||
| <span class="centered"> | ||
| <img src="assets/HTTP Request Example.png" /> | ||
| </span> | ||
|
|
||
| --- | ||
|
|
||
| #### Website Speak In HTTP | ||
|
|
||
| <span class="centered"> | ||
| <img src="assets/Website Traffic.png" /> | ||
| </span> | ||
|
|
||
| --- | ||
|
|
||
| #### 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 | ||
|
|
||
| <span class="centered"> | ||
| <img src="assets/Chrome Request.png" /> | ||
| </span> | ||
|
|
||
| --- | ||
|
|
||
| #### Working With Multiple Arguments | ||
|
|
||
| <span class="centered"> | ||
| <img src="assets/Query Param Example.png" /> | ||
| </span> | ||
|
|
||
| ```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 | ||
| ``` | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just add these to the main slides file (https://github.com/UtahRETC/IntroProgrammingPythonPart22023/blob/main/units/03%20-%20Introduction%20to%20Flask/Introduction%20to%20Flask%20Slides.md) for this unit. That way there's a single file with all of the slides.