Date: Feb. 10th, 2020
Lecturer: NIghT cAt
Application Programming Interface
Request
Response
Server
Client
Request
Response
Server
Client
Get
Post
Request
Response
Server
Client
Receive Request
Find Corresponding Function
Search
Database
Representation Function
an abstraction in which software providing generic functionality can be selectively changed by additional user-written code.
Key Distinguishing Feature in Normal Libraries:
Model
View
Controller
The central component of the patter, it directory manages the data, logic, and rules of the application.
Any representation of information.
Accepts any input and converts it into command for the model or view.
We're going to use pipenv which can manage dependencies for an application
1. Use it, we don't need to use pip and venv repectively
2. Generate and update Pipfile, Pipfile.lock
3. Can load different environment parameters through .env file
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
flask = "*"
line-bot-sdk = "*"
beautifulsoup4 = "*"
requests = "*"
lxml = "*"
python-dotenv = "*"
[requires]
python_version = "3.7"
{
"_meta": {
"hash": {
"sha256": "700e4ebe9f2dbcfb36b51afda233ab0fa894eba97e6e1b86c13362800b5dd10c"
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.7"
},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"default": {
"beautifulsoup4": {
"hashes": [
"sha256:05fd825eb01c290877657a56df4c6e4c311b3965bda790c613a3d6fb01a5462a",
"sha256:9fbb4d6e48ecd30bcacc5b63b94088192dcda178513b2ae3c394229f8911b887",
"sha256:e1505eeed31b0f4ce2dbb3bc8eb256c04cc2b3b72af7d551a4ab6efd5cbe5dae"
],
"index": "pypi",
"version": "==4.8.2"
},
"certifi": {
"hashes": [
"sha256:017c25db2a153ce562900032d5bc68e9f191e44e9a0f762f373977de9df1fbb3",
"sha256:25b64c7da4cd7479594d035c08c2d809eb4aab3a26e5a990ea98cc450c320f1f"
],
"version": "==2019.11.28"
},
# ...
brew install pipenv # Download the pipenv
cd [Project_Directory] # Change the directory
pipenv --three # Create the virtual environment
pip install pipenv # Download the pipenv
cd [Project_Directory] # Change the directory
pipenv --three # Create the virtual environment
pipenv shell
exit # or "Ctrl + D"
Warning: YOU SHOULD START UP THE VIRTUAL ENVIRONMENT BEFORE
pipenv install flask
Warning again:
WITH THE FILE EXTENSION ".py"
Anyway, we use "app.py" as example
from flask import Flask
app = Flask(__name__)
Every Flask web api should have an app instance.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "ハロー・ワールド" # Hello, world
Decorators allow us to wrap another function in order to extend the behavior of wrapped function, without permanently modifying it.
def print_func_name(func):
def wrap():
print("Now use function '{}'".format(func.__name__))
func()
return wrap
def dog_bark():
print("Bark !!!")
def cat_meow():
print("Meow ~~~")
if __name__ == "__main__":
print_func_name(dog_bark)()
print_func_name(cat_meow)()
def print_func_name(func):
def wrap():
print("Now use function '{}'".format(func.__name__))
func()
return wrap
@print_func_name
def dog_bark():
print("Bark!!!")
@print_func_name
def cat_meow():
print("Meow~~~")
if __name__ == "__main__":
dog_bark()
cat_meow()
To simplified the Syntax that the function can be presented by simple symbol or few codes.
export FLASK_APP=app.py
flask run
set FLASK_APP=app.py
flask run
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "ハロー・ワールド" # Hello, world
@app.route("/hello")
def hello:
return "Hi, stranger~"