You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+19-16Lines changed: 19 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,14 +8,14 @@ from flask import Flask
8
8
app = Flask(__name__)
9
9
```
10
10
11
-
Running the Server
11
+
Running the Server:
12
12
13
13
```python
14
14
if__name__=='__main__':
15
15
app.run(debug=True)
16
16
```
17
17
18
-
App routes define the path to different pages in your application. This means that when you run python app.py, on http://127.0.0.1:5000/ it will run the function my_first_route that will print hello world.
18
+
App routes define the path to different pages in your application. This means that when you run python app.py, on http://127.0.0.1:5000/ it will run the function `my_first_route` that will print hello world.
19
19
```python
20
20
@app.route('/')
21
21
defmy_first_route():
@@ -31,29 +31,29 @@ def my_second_route(name):
31
31
```
32
32
33
33
**Problem 1**
34
-
**Write a route that takes a first name, last name, and graduating year in the route. If this route is hit, wit print out the line "<firstname> <lastname> will graduate in <graudating_year>"**
34
+
**Write a route that takes a first name, last name, and graduating year in the route. If this route is hit, wit print out the line `<firstname> <lastname> will graduate in <graduating_year>`**
35
35
36
36
37
-
So, what are we using Flask for and why are routes useful ... to build at API.
37
+
So, what are we using Flask for and why are routes useful ... to build an API.
38
38
39
39
An API, or Application Programming Interface, is a set of subroutine definitions, protocols, and tools for building application software. It defines communication between various software components.
40
40
41
41
Lets give an example. Let's say on the frontend you want to display all a list of names that are stored in your database. You are going to send a GET request that will be sent to one of these routes that you define in Flask. The function to handle the route will understand that you are trying to get some information, retrieve it from the database, and set in back to the frontend in a json format.
42
42
43
43
44
-
The GET request is part of a protocol called REST, which stands for Representational State Transfer.
44
+
The `GET` request is part of a protocol called REST, which stands for Representational State Transfer.
45
45
46
46
There are many types of requests, put the most important ones are:
47
47
48
-
GET: gets information from a database
48
+
`GET`: gets information from a database
49
49
50
-
POST: adds information to a database
50
+
`POST`: adds information to a database
51
51
52
-
PUT: modifies information in a database
52
+
`PUT`: modifies information in a database
53
53
54
-
DELETE: deletes information in a database
54
+
`DELETE`: deletes information in a database
55
55
56
-
From the nnb project from last semester, you can see an example of a get request that uses postgress database. Maps.query.all() goes into postgress, finds the table labeled Maps, and gets everything. The data is then put into a list and turned into a json object. If it fails, it will send the correct error message
56
+
From the nnb project from last semester, you can see an example of a get request that uses postgress database. Maps.query.all() goes into postgress, finds the table labeled `Maps`, and gets everything. The data is then put into a list and turned into a json object. If it fails, it will send the correct error message
57
57
```python
58
58
#Gets all maps
59
59
@app.route('/maps', methods=['GET'])
@@ -68,7 +68,7 @@ def getallyears():
68
68
return jsonify({"status": "failed", "message": "Endpoint, /years, needs a GET request"})
69
69
```
70
70
71
-
Here's a POST request example from the same project
71
+
Here's a POST request example from the same project:
72
72
```python
73
73
#Add a map
74
74
@app.route('/maps', methods=['POST'])
@@ -125,14 +125,17 @@ For example
125
125
]
126
126
```
127
127
128
-
**Then create a route for /get_all_users that will receive a GET request and return the list of all current users in a json format. It will return an error message for everything other than a GET request.**
128
+
**Then create a route for `/get_all_users` that will receive a GET request and return the list of all current users in a json format. It will return an error message for everything other than a GET request.**
129
129
130
-
**Next create a route called /add_user that will receieve a POST request. Inside the request data there will be a user with an id, name, and age. The function will take the request data and add a new user to the globale list of users. Also, add appropriate sucess/error responses in a json format.**
130
+
**Next create a route called `/add_user` that will receieve a POST request. Inside the request data there will be a user with an id, name, and age. The function will take the request data and add a new user to the globale list of users. Also, add appropriate success/error responses in a json format.**
131
131
132
-
**Next create a route called /modify_user that will receieve a PUT request. In the request data have an id so they know which user is being modified, and then have a new name or age for the user. In the function, edit the user with that id in the global list of users. Also, add appropriate sucess/error responses in a json format.**
132
+
**Next create a route called `/modify_user` that will receieve a PUT request. In the request data have an id so they know which user is being modified, and then have a new name or age for the user. In the function, edit the user with that id in the global list of users. Also, add appropriate success/error responses in a json format.**
133
133
134
-
**Next create a route called /delete_user that will receieve a DELETE request and a name. The request data will have an id,and then that user is deleted from teh global array. Also, add appropriate sucess/error responses in a json format.**
134
+
**Next create a route called `/delete_user` that will receieve a DELETE request and a name. The request data will have an id,and then that user is deleted from teh global array. Also, add appropriate sucess/error responses in a json format.**
135
135
136
136
**To test everything, download postman and make requests**
137
137
138
-
Setting up the database and defining it is alot of work, so we'll leave that for your tech leads to teach you. Also, for the course of this intro project, we're doing everything in app.py. In your projects though, you are going to organize the endpoints into different files, have a folder to define the models, and other files for the database connection.
138
+
Setting up the database and defining it is alot of work, so we'll leave that for your tech leads to teach you. Also, for the course of this intro project, we're doing everything in app.py. **In your projects though, you are going to organize the endpoints into different files, have a folder to define the models, and other files for the database connection. **
0 commit comments