Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 167 additions & 0 deletions units/03 - Introduction to Flask/ HTTP Get And Query Paramaters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
---
Copy link
Copy Markdown
Member

@minond minond May 9, 2023

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.

theme: css/custom-simple.css
highlightTheme: css/layout.css
---

<!-- .slide: class="center" -->

# Intermediate Python

### Unit 3: Introduction to Flask
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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
```
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.