-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython-Flask-Notes.txt
More file actions
81 lines (62 loc) · 2.88 KB
/
Copy pathpython-Flask-Notes.txt
File metadata and controls
81 lines (62 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
Python Flask framework
Flask is usually used as a restful API
API - a program that takes in some data and gives back some other data, usually after processing it.
For example, Twitter Search. Twitter search accepts data (the search parameters), processes it (finds the tweets that match those parameters), and sends data back (the tweets themselves).
If you are a user of the Twitter Search API, you can send them the search parameters and you get back the tweets.
REST - Representational State Transfer
A way of thinking of how server responds to requests
Returns resources
Stateless - one request cannot depend on other requests
Server only knows about the current request and not any previous requests
Jinja - templating language
https://github.com/schoolofcode-me/rest-api-sections/tree/master/section3
pip install flask
(pip3 or pip3.5 etc install flask)
install flask
recommended install in a virtual environment
WSGI - Web Server Gateway Interface
Python standard described in detail in PEP 3333
describes how a web server communicates with web applications, and how web applications can be chained together to process one request
(Add boilerplate from usefulPython/)
https://www.youtube.com/watch?v=K5LBC3hLl8w
Register blueprints
python server.py
http://localhost:5000/
@app.route()
by default are GET requests
Need to add method=['POST'] in parameter for other verbs
from flask import jsonify - similar to GSON and Jackson
jsonify - creates json data for output.
return jsonify({"output": "content"})
json always returns double quotes never single quotes
Flask-restful
https://github.com/schoolofcode-me/rest-api-sections/tree/master/section4
limited to only REST standard.
pip freeze
shows libraries currently installed and in use in current python environment
pip install virtualenv
tool used to create virtual environments
virtualenv venv --python=python3.8
will create a folder called venv and install python 3.8
source venv/bin/activate [mac]
./venv/Scripts/activate.bat [win]
activates virutal environment
deactivate [mac]
./venv/Scripts/deactivate.bat [win]
exits python virtual environment
pip install flask-restful
install flask-restful
Api works with resources and each resource has to be a class
You inherit Resource (package)
No JSONify when using flask-restful
HTTP status code: 201 = created
202 = Accepted
Will create later
Using correct status code is important
SQLAlchemy
Create resources and models folder
add __init__.py file in these folders
@classmethod - annotation that is only used for the class.
Not directed interacted with by the client
Methods in a file should be separated by 1 line.
Classes in a file should be separated by 2 lines.