1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
TL;DR. Trong suốt bài viết này, chúng tôi sẽ sử dụng Flask và Python để phát triển API RESTful. Chúng tôi sẽ tạo một điểm cuối trả về dữ liệu tĩnh [từ điển]. Sau đó, chúng ta sẽ tạo một lớp có hai chuyên ngành và một vài điểm cuối để chèn và truy xuất các thể hiện của các lớp này. Cuối cùng, chúng ta sẽ xem cách chạy API trên bộ chứa Docker. Mã cuối cùng được phát triển trong suốt bài viết này có sẵn trong kho GitHub này. tôi hy vọng bạn thích nó
"Flask cho phép các nhà phát triển Python tạo API RESTful nhẹ. "
Tweet cái này đi
Bản tóm tắt
Bài viết này được chia thành các phần sau
Tại sao lại là trăn?
Ngày nay, việc chọn Python để phát triển ứng dụng đang trở thành lựa chọn rất phổ biến. , Python là một trong những ngôn ngữ lập trình phát triển nhanh nhất, đã vượt qua cả Java về số lượng câu hỏi được đặt ra trên nền tảng này. Trên GitHub, ngôn ngữ này cũng có dấu hiệu được áp dụng đại trà, chiếm vị trí thứ hai trong số các ngôn ngữ lập trình hàng đầu năm 2021
Cộng đồng khổng lồ hình thành xung quanh Python đang cải thiện mọi khía cạnh của ngôn ngữ. Ngày càng có nhiều thư viện nguồn mở được phát hành để giải quyết nhiều chủ đề khác nhau, như Trí tuệ nhân tạo, Học máy và phát triển web. Bên cạnh sự hỗ trợ to lớn được cung cấp bởi cộng đồng nói chung, Python Software Foundation cũng cung cấp tài liệu tuyệt vời, nơi những người mới sử dụng có thể tìm hiểu bản chất của nó một cách nhanh chóng
Tại sao bình?
Khi nói đến phát triển web trên Python, có ba khung chính. Django, Flask và một trình phát tương đối mới FastAPI. Django cũ hơn, trưởng thành hơn và phổ biến hơn một chút. Trên GitHub, khung này có khoảng 66 nghìn sao, 2. 2 nghìn người đóng góp, ~ 350 bản phát hành và hơn 25 nghìn lượt chia tách
FastAPI đang phát triển với tốc độ cao, với 48 nghìn sao trên Github, 370 cộng tác viên và hơn 3. dĩa 9k. Khung trang nhã này được xây dựng cho các API hiệu suất cao và viết mã nhanh không thể bỏ qua
Flask, mặc dù ít phổ biến hơn, không thua xa. Trên GitHub, Flask có gần 60 nghìn sao, ~650 người đóng góp, ~23 bản phát hành và gần 15 nghìn nhánh
Mặc dù Django cũ hơn và có cộng đồng rộng rãi hơn một chút, nhưng Flask vẫn có những điểm mạnh của nó. Ngay từ đầu, Flask đã được xây dựng với khả năng mở rộng và đơn giản. Các ứng dụng Flask được biết đến là nhẹ, chủ yếu được so sánh với các ứng dụng Django của chúng. Các nhà phát triển Flask gọi nó là một microframework, trong đó micro[] có nghĩa là mục tiêu là giữ cho lõi đơn giản nhưng có thể mở rộng. Flask sẽ không đưa ra nhiều quyết định cho chúng tôi, chẳng hạn như sử dụng cơ sở dữ liệu nào hoặc chọn công cụ mẫu nào. Cuối cùng, Flask có tài liệu phong phú đề cập đến mọi thứ mà nhà phát triển cần để bắt đầu. FastAPI tuân theo cách tiếp cận "vi mô" tương tự như Flask, mặc dù nó cung cấp nhiều công cụ hơn như UI Swagger tự động và là một lựa chọn tuyệt vời cho API. Tuy nhiên, vì nó là một khung mới hơn nên nhiều tài nguyên và thư viện khác tương thích với các khung như Django và Flask nhưng không phải với FastAPI
Nhẹ, dễ sử dụng, tài liệu đầy đủ và phổ biến, Flask là một lựa chọn tốt để phát triển API RESTful
Khởi động ứng dụng Flask
Đầu tiên và quan trọng nhất, chúng ta sẽ cần cài đặt một số phụ thuộc trên máy phát triển của mình. Chúng ta sẽ cần cài đặt Python 3, Pip [Python Package Index] và Flask
Cài đặt Python 3
Nếu chúng tôi đang sử dụng một số phiên bản gần đây của bản phân phối Linux phổ biến [như Ubuntu] hoặc macOS, thì có thể chúng tôi đã cài đặt Python 3 trên máy tính của mình. Nếu chúng tôi đang chạy Windows, có thể chúng tôi sẽ cần cài đặt Python 3, vì hệ điều hành này không đi kèm với bất kỳ phiên bản nào
Sau khi cài đặt Python 3 trên máy của chúng tôi, chúng tôi có thể kiểm tra xem chúng tôi đã thiết lập mọi thứ như mong đợi chưa bằng cách chạy lệnh sau
python --version
# Python 3.8.9
Lưu ý rằng lệnh trên có thể tạo ra một đầu ra khác khi chúng ta có phiên bản Python khác. Điều quan trọng là bạn đang chạy ít nhất
# we might need to replace pip with pip3
pip install Flask
6 hoặc mới hơn. Thay vào đó, nếu chúng tôi nhận được "Python 2", chúng tôi có thể thử phát hành # we might need to replace pip with pip3
pip install Flask
7. Nếu lệnh này tạo ra kết quả chính xác, chúng ta phải thay thế tất cả các lệnh trong toàn bộ bài viết để sử dụng # we might need to replace pip with pip3
pip install Flask
8 thay vì chỉ # we might need to replace pip with pip3
pip install Flask
9Cài đặt Pip
Pip là công cụ được đề xuất để cài đặt các gói Python. Mặc dù trang cài đặt chính thức tuyên bố rằng
# hello.py
from flask import Flask
app = Flask[__name__]
@app.route["/"]
def hello_world[]:
return "Hello, World!"
0 được cài đặt nếu chúng tôi đang sử dụng Python 2 >= # hello.py
from flask import Flask
app = Flask[__name__]
@app.route["/"]
def hello_world[]:
return "Hello, World!"
1 hoặc Python 3 >= # hello.py
from flask import Flask
app = Flask[__name__]
@app.route["/"]
def hello_world[]:
return "Hello, World!"
2, cài đặt Python thông qua # hello.py
from flask import Flask
app = Flask[__name__]
@app.route["/"]
def hello_world[]:
return "Hello, World!"
3 trên Ubuntu không cài đặt # hello.py
from flask import Flask
app = Flask[__name__]
@app.route["/"]
def hello_world[]:
return "Hello, World!"
0. Do đó, hãy kiểm tra xem chúng ta có cần cài đặt riêng # hello.py
from flask import Flask
app = Flask[__name__]
@app.route["/"]
def hello_world[]:
return "Hello, World!"
0 hay đã có nó...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
3Nếu lệnh trên tạo ra một đầu ra tương tự như
# hello.py
from flask import Flask
app = Flask[__name__]
@app.route["/"]
def hello_world[]:
return "Hello, World!"
6, thì chúng ta nên bắt đầu. Nếu chúng tôi nhận được # hello.py
from flask import Flask
app = Flask[__name__]
@app.route["/"]
def hello_world[]:
return "Hello, World!"
7, chúng tôi có thể thử thay thế # hello.py
from flask import Flask
app = Flask[__name__]
@app.route["/"]
def hello_world[]:
return "Hello, World!"
0 bằng # hello.py
from flask import Flask
app = Flask[__name__]
@app.route["/"]
def hello_world[]:
return "Hello, World!"
9. Nếu chúng tôi không thể tìm thấy Pip cho Python 3 trên máy của mình, chúng tôi có thể làm theo hướng dẫn tại đây để cài đặt Pipcài đặt bình
Chúng ta đã biết Flask là gì và khả năng của nó. Do đó, hãy tập trung vào việc cài đặt nó trên máy của chúng tôi và thử nghiệm xem liệu chúng tôi có thể chạy ứng dụng Flask cơ bản hay không. Bước đầu tiên là sử dụng
# hello.py
from flask import Flask
app = Flask[__name__]
@app.route["/"]
def hello_world[]:
return "Hello, World!"
0 để cài đặt Flask# we might need to replace pip with pip3
pip install Flask
Sau khi cài đặt gói, chúng tôi sẽ tạo một tệp có tên
python --version
# Python 3.8.9
01 và thêm năm dòng mã vào đó. Vì chúng tôi sẽ sử dụng tệp này để kiểm tra xem Flask đã được cài đặt đúng chưa, chúng tôi không cần lồng nó vào một thư mục mới# hello.py
from flask import Flask
app = Flask[__name__]
@app.route["/"]
def hello_world[]:
return "Hello, World!"
5 dòng mã này là mọi thứ chúng ta cần để xử lý các yêu cầu HTTP và trả về "Xin chào, Thế giới. " thông điệp. Để chạy nó, chúng ta thực hiện lệnh sau
python --version
# Python 3.8.9
0Trên Ubuntu, chúng tôi có thể cần chỉnh sửa biến
02 để có thể chạy bình trực tiếp. Để làm điều đó, hãypython --version # Python 3.8.9
03 và sau đópython --version # Python 3.8.9
04python --version # Python 3.8.9
Sau khi thực hiện các lệnh này, chúng tôi có thể truy cập ứng dụng của mình bằng cách mở trình duyệt và điều hướng đến
python --version
# Python 3.8.9
05 hoặc bằng cách phát hành python --version
# Python 3.8.9
06Môi trường ảo [virtualenv]
Mặc dù PyPA—nhóm Cơ quan đóng gói Python—khuyến nghị sử dụng
# hello.py
from flask import Flask
app = Flask[__name__]
@app.route["/"]
def hello_world[]:
return "Hello, World!"
0 làm công cụ để cài đặt các gói Python, nhưng chúng tôi sẽ cần sử dụng một gói khác để quản lý các phần phụ thuộc của dự án. Đúng là # hello.py
from flask import Flask
app = Flask[__name__]
@app.route["/"]
def hello_world[]:
return "Hello, World!"
0 hỗ trợ, nhưng công cụ này thiếu một số tính năng cần thiết cho các dự án nghiêm túc chạy trên các máy phát triển và sản xuất khác nhau. Trong số các vấn đề của nó, những vấn đề gây ra nhiều vấn đề nhất là
0 cài đặt các gói trên toàn cầu, khiến việc quản lý nhiều phiên bản của cùng một gói trên cùng một máy trở nên khó khăn# hello.py from flask import Flask app = Flask[__name__] @app.route["/"] def hello_world[]: return "Hello, World!"
09 cần tất cả các phụ thuộc và phụ thuộc được liệt kê rõ ràng, một quy trình thủ công tẻ nhạt và dễ bị lỗipython --version # Python 3.8.9
Để giải quyết những vấn đề này, chúng tôi sẽ sử dụng Pipenv. Pipenv là trình quản lý phụ thuộc tách biệt các dự án trong môi trường riêng tư, cho phép cài đặt các gói cho mỗi dự án. Nếu bạn đã quen thuộc với NPM hoặc trình đóng gói của Ruby, thì về tinh thần, nó cũng tương tự như các công cụ đó
# we might need to replace pip with pip3
pip install Flask
2Bây giờ, để bắt đầu tạo một ứng dụng Flask nghiêm túc, hãy tạo một thư mục mới chứa mã nguồn của chúng ta. Trong bài viết này, chúng tôi sẽ tạo Cashman, một API RESTful nhỏ cho phép người dùng quản lý thu nhập và chi phí. Do đó, chúng ta sẽ tạo một thư mục có tên là
# we might need to replace pip with pip3
pip install Flask
22. Sau đó, chúng tôi sẽ sử dụng # we might need to replace pip with pip3
pip install Flask
23 để bắt đầu dự án của mình và quản lý các phụ thuộc của chúng tôi# we might need to replace pip with pip3
pip install Flask
5Lệnh thứ hai tạo môi trường ảo của chúng tôi, nơi tất cả các phụ thuộc của chúng tôi được cài đặt và lệnh thứ ba sẽ thêm Flask làm phụ thuộc đầu tiên của chúng tôi. Nếu chúng tôi kiểm tra thư mục của dự án, chúng tôi sẽ thấy hai tệp mới
24 chứa thông tin chi tiết về dự án của chúng tôi, chẳng hạn như phiên bản Python và các gói cần thiết# we might need to replace pip with pip3 pip install Flask
25 chứa chính xác phiên bản của từng gói mà dự án của chúng tôi phụ thuộc vào và các phụ thuộc bắc cầu của nó# we might need to replace pip with pip3 pip install Flask
mô-đun Python
Giống như các ngôn ngữ lập trình chính thống khác, Python cũng có khái niệm về mô-đun để cho phép các nhà phát triển tổ chức mã nguồn theo chủ đề/chức năng. Tương tự như các gói Java và không gian tên C#, các mô-đun trong Python là các tệp được sắp xếp trong các thư mục mà các tập lệnh Python khác có thể nhập. Để tạo một module trên ứng dụng Python, chúng ta cần tạo một thư mục và thêm một tệp trống có tên là
# we might need to replace pip with pip3
pip install Flask
26Hãy tạo mô-đun đầu tiên trên ứng dụng của chúng tôi, mô-đun chính, với tất cả các điểm cuối RESTful của chúng tôi. Bên trong thư mục của ứng dụng, hãy tạo một ứng dụng khác có cùng tên,
# we might need to replace pip with pip3
pip install Flask
27. Thư mục gốc # we might need to replace pip with pip3
pip install Flask
22 được tạo trước đó sẽ chứa siêu dữ liệu về dự án của chúng ta, chẳng hạn như những phụ thuộc mà nó có, trong khi thư mục mới này sẽ là mô-đun chứa các tập lệnh Python của chúng ta# we might need to replace pip with pip3
pip install Flask
1Bên trong mô-đun chính, hãy tạo một tập lệnh có tên là
# we might need to replace pip with pip3
pip install Flask
29. Trong tập lệnh này, chúng tôi sẽ xác định điểm cuối đầu tiên của ứng dụng của chúng tôi# we might need to replace pip with pip3
pip install Flask
3Như trong ví dụ trước, ứng dụng của chúng ta trả về thông báo "Xin chào, thế giới. " thông điệp. Chúng tôi sẽ bắt đầu cải thiện nó trong giây lát, nhưng trước tiên, hãy tạo một tệp thực thi có tên là
# we might need to replace pip with pip3
pip install Flask
50 trong thư mục gốc của ứng dụng của chúng tôi# we might need to replace pip with pip3
pip install Flask
5Mục tiêu của tệp này là để tạo điều kiện khởi động ứng dụng của chúng tôi. Mã nguồn của nó sẽ như sau
...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
30Lệnh đầu tiên xác định tập lệnh chính sẽ được thực thi bởi Flask. Lệnh thứ hai chạy ứng dụng Flask của chúng ta trong bối cảnh môi trường ảo lắng nghe tất cả các giao diện trên máy tính [
# we might need to replace pip with pip3
pip install Flask
51]Ghi chú. chúng tôi đang đặt bình chạy ở chế độ gỡ lỗi để nâng cao trải nghiệm phát triển của mình và kích hoạt tính năng tải lại nóng, vì vậy chúng tôi không phải khởi động lại máy chủ mỗi khi thay đổi mã. Nếu bạn chạy Flask trong sản xuất, chúng tôi khuyên bạn nên cập nhật các cài đặt này cho sản xuất
Để kiểm tra xem tập lệnh này có hoạt động chính xác không, chúng tôi chạy
# we might need to replace pip with pip3
pip install Flask
52 để nhận được kết quả tương tự như khi thực hiện lệnh "Xin chào, thế giới. " đăng kí...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
31Tạo một điểm cuối RESTful với Flask
Giờ đây, ứng dụng của chúng ta đã được cấu trúc, chúng ta có thể bắt đầu mã hóa một số điểm cuối có liên quan. Như đã đề cập trước đây, mục tiêu của ứng dụng của chúng tôi là giúp người dùng quản lý thu nhập và chi phí. Chúng tôi sẽ bắt đầu bằng cách xác định hai điểm cuối để xử lý thu nhập. Hãy thay thế nội dung của tệp
# we might need to replace pip with pip3
pip install Flask
53 bằng nội dung sau...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
32Kể từ khi cải thiện ứng dụng của mình, chúng tôi đã xóa điểm cuối trả về "Xin chào, thế giới. " cho người dùng. Ở vị trí của nó, chúng tôi đã xác định một điểm cuối để xử lý các yêu cầu HTTP
# we might need to replace pip with pip3
pip install Flask
54 để trả lại thu nhập và một điểm cuối khác để xử lý các yêu cầu HTTP # we might need to replace pip with pip3
pip install Flask
55 để thêm các yêu cầu mới. Các điểm cuối này được chú thích bằng # we might need to replace pip with pip3
pip install Flask
56 để xác định các tuyến lắng nghe yêu cầu trên điểm cuối # we might need to replace pip with pip3
pip install Flask
57. Để tạo thuận lợi cho quá trình này, chúng tôi hiện đang điều chỉnh thu nhập dưới dạng. Tuy nhiên, chúng tôi sẽ sớm tạo các lớp biểu thị thu nhập và chi phí
Để tương tác với cả hai điểm cuối mà chúng tôi đã tạo, chúng tôi có thể khởi động ứng dụng của mình và đưa ra một số yêu cầu HTTP
...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
33Mô hình ánh xạ với các lớp Python
Sử dụng từ điển trong trường hợp sử dụng đơn giản như trường hợp trên là đủ. Tuy nhiên, đối với các ứng dụng phức tạp hơn xử lý các thực thể khác nhau và có nhiều quy tắc kinh doanh và xác thực, chúng ta có thể cần đóng gói dữ liệu của mình vào các lớp Python
Chúng tôi sẽ cấu trúc lại ứng dụng của mình để tìm hiểu quy trình ánh xạ các thực thể [như thu nhập] dưới dạng các lớp. Điều đầu tiên mà chúng tôi sẽ làm là tạo một mô hình con để chứa tất cả các thực thể của chúng tôi. Hãy tạo một thư mục
# we might need to replace pip with pip3
pip install Flask
58 bên trong mô-đun # we might need to replace pip with pip3
pip install Flask
27 và thêm một tệp trống có tên là # we might need to replace pip with pip3
pip install Flask
26 vào đó...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
34Ánh xạ một siêu lớp Python
Chúng tôi sẽ tạo ba lớp trong mô-đun/thư mục mới này.
# we might need to replace pip with pip3
pip install Flask
11, # we might need to replace pip with pip3
pip install Flask
12 và # we might need to replace pip with pip3
pip install Flask
13. Lớp đầu tiên sẽ là cơ sở cho hai lớp khác và chúng tôi sẽ gọi nó là # we might need to replace pip with pip3
pip install Flask
11. Hãy tạo một tệp có tên # we might need to replace pip with pip3
pip install Flask
15 trong thư mục # we might need to replace pip with pip3
pip install Flask
58 với đoạn mã sau...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
35Bên cạnh lớp
# we might need to replace pip with pip3
pip install Flask
11, chúng ta cũng đã định nghĩa một lớp # we might need to replace pip with pip3
pip install Flask
18. Chúng tôi sẽ sử dụng cái sau để giải tuần tự hóa và tuần tự hóa các phiên bản của # we might need to replace pip with pip3
pip install Flask
11 từ và tới các đối tượng JSON. Lớp này kế thừa từ một lớp cha khác có tên là # we might need to replace pip with pip3
pip install Flask
30 thuộc về một gói chưa được cài đặt...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
36Marshmallow là một gói Python phổ biến để chuyển đổi các kiểu dữ liệu phức tạp, chẳng hạn như các đối tượng, sang và từ các kiểu dữ liệu Python tích hợp. Chúng tôi có thể sử dụng gói này để xác thực, tuần tự hóa và giải tuần tự hóa dữ liệu. Chúng tôi sẽ không đi sâu vào xác nhận trong bài viết này, vì nó sẽ là chủ đề của một bài viết khác. Mặc dù, như đã đề cập, chúng tôi sẽ sử dụng
# we might need to replace pip with pip3
pip install Flask
31 để tuần tự hóa và giải tuần tự hóa các thực thể thông qua các điểm cuối của chúng tôiÁnh xạ thu nhập và chi phí dưới dạng các lớp Python
Để giữ cho mọi thứ ngăn nắp và có ý nghĩa hơn, chúng tôi sẽ không hiển thị lớp
# we might need to replace pip with pip3
pip install Flask
11 trên các điểm cuối của chúng tôi. Chúng tôi sẽ tạo hai chuyên môn để xử lý các yêu cầu. # we might need to replace pip with pip3
pip install Flask
12 và # we might need to replace pip with pip3
pip install Flask
13. Hãy tạo một tệp có tên # we might need to replace pip with pip3
pip install Flask
35 bên trong mô-đun # we might need to replace pip with pip3
pip install Flask
58 với đoạn mã sau...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
37Giá trị duy nhất mà lớp này quảng cáo cho ứng dụng của bạn là nó mã hóa cứng loại giao dịch. Loại này là một Python enumerator mà chúng ta vẫn phải tạo, nó sẽ giúp chúng ta lọc các giao dịch trong tương lai. Hãy tạo một tệp khác, được gọi là
# we might need to replace pip with pip3
pip install Flask
37, bên trong # we might need to replace pip with pip3
pip install Flask
58 để đại diện cho điều tra viên này...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
38Mã của điều tra viên khá đơn giản. Nó chỉ định nghĩa một lớp có tên là
# we might need to replace pip with pip3
pip install Flask
39 kế thừa từ # we might need to replace pip with pip3
pip install Flask
50 và định nghĩa hai loại. # we might need to replace pip with pip3
pip install Flask
51 và # we might need to replace pip with pip3
pip install Flask
52Cuối cùng, hãy tạo lớp đại diện cho chi phí. Để làm điều đó, hãy thêm một tệp mới có tên là
# we might need to replace pip with pip3
pip install Flask
53 bên trong # we might need to replace pip with pip3
pip install Flask
58 với đoạn mã sau...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
39Tương tự như ________ 412, lớp này mã hóa cứng loại giao dịch, nhưng bây giờ nó chuyển ________ 452 sang lớp cha. Sự khác biệt là nó biến đổi
# we might need to replace pip with pip3
pip install Flask
57 đã cho thành âm. Do đó, bất kể người dùng gửi giá trị dương hay âm, chúng tôi sẽ luôn lưu trữ dưới dạng âm để tiện cho việc tính toánTuần tự hóa và giải tuần tự hóa các đối tượng với Marshmallow
Với siêu lớp
# we might need to replace pip with pip3
pip install Flask
11 và các chuyên môn hóa của nó được triển khai đầy đủ, giờ đây chúng tôi có thể nâng cao các điểm cuối của mình để xử lý các lớp này. Hãy thay thế nội dung # we might need to replace pip with pip3
pip install Flask
53 thành# we might need to replace pip with pip3
pip install Flask
0Phiên bản mới mà chúng tôi vừa triển khai bắt đầu bằng cách xác định lại biến
...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
300 thành danh sách ...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
301 và ...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
302, hiện được gọi là ...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
303. Bên cạnh đó, chúng tôi cũng đã thay đổi cách thực hiện của cả hai phương pháp xử lý thu nhập. Đối với điểm cuối được sử dụng để truy xuất thu nhập, chúng tôi đã xác định một phiên bản của ...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
304 để tạo biểu diễn JSON về thu nhập. Chúng tôi cũng từng chỉ trích xuất thu nhập từ danh sách ...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
303. Cuối cùng, chúng tôi gửi lại mảng thu nhập JSON cho người dùngĐiểm cuối chịu trách nhiệm chấp nhận thu nhập mới cũng được tái cấu trúc. Thay đổi ở điểm cuối này là việc bổ sung
...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
304 để tải phiên bản của # we might need to replace pip with pip3
pip install Flask
12 dựa trên dữ liệu JSON do người dùng gửi. Vì danh sách ...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
303 xử lý các trường hợp của # we might need to replace pip with pip3
pip install Flask
11 và các lớp con của nó, chúng tôi chỉ thêm # we might need to replace pip with pip3
pip install Flask
12 mới vào danh sách đóHai điểm cuối khác chịu trách nhiệm xử lý chi phí,
...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
312 và ...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
313, gần như là bản sao của đối tác ...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
314 của chúng. Sự khác biệt là- thay vì xử lý các trường hợp của
12, chúng tôi xử lý các trường hợp của# we might need to replace pip with pip3 pip install Flask
13 để chấp nhận chi phí mới,# we might need to replace pip with pip3 pip install Flask
- và thay vì lọc theo
317, chúng tôi lọc theo... in_memory_datastore = { "COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"}, "ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"}, "APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"}, "BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"}, "PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"}, "SIMULA67": {"name": "SIMULA67", "publication_year": 1967, "contribution": "class/object split, subclassing, protected attributes"}, "Pascal": {"name": "Pascal", "publication_year": 1970, "contribution": "modern unary, binary, and assignment operator syntax expectations"}, "CLU": {"name": "CLU", "publication_year": 1975, "contribution": "iterators, abstract data types, generics, checked exceptions"}, } ...
318 để gửi lại chi phí cho người dùng... in_memory_datastore = { "COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"}, "ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"}, "APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"}, "BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"}, "PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"}, "SIMULA67": {"name": "SIMULA67", "publication_year": 1967, "contribution": "class/object split, subclassing, protected attributes"}, "Pascal": {"name": "Pascal", "publication_year": 1970, "contribution": "modern unary, binary, and assignment operator syntax expectations"}, "CLU": {"name": "CLU", "publication_year": 1975, "contribution": "iterators, abstract data types, generics, checked exceptions"}, } ...
Điều này kết thúc việc triển khai API của chúng tôi. Nếu chúng tôi chạy ứng dụng Flask của mình ngay bây giờ, chúng tôi sẽ có thể tương tác với các điểm cuối, như được hiển thị ở đây
# we might need to replace pip with pip3
pip install Flask
1Ứng dụng Dockerizing Flask
Vì chúng tôi dự định cuối cùng sẽ phát hành API của mình trên đám mây, nên chúng tôi sẽ tạo một
...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
319 để mô tả những gì cần thiết để chạy ứng dụng trên bộ chứa Docker. Chúng tôi cần cài đặt Docker trên máy phát triển của mình để kiểm tra và chạy các phiên bản dockerized của dự án của chúng tôi. Xác định công thức Docker [...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
319] sẽ giúp chúng tôi chạy API trong các môi trường khác nhau. Tức là trong tương lai chúng ta cũng sẽ cài đặt Docker và chạy chương trình của mình trên các môi trường như vàHãy tạo
...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
319 trong thư mục gốc của dự án của chúng tôi với đoạn mã sau# we might need to replace pip with pip3
pip install Flask
2Mục đầu tiên trong công thức xác định rằng chúng ta sẽ tạo bộ chứa Docker dựa trên hình ảnh Docker mặc định của Python 3. Sau đó, chúng tôi cập nhật APK và cài đặt
# we might need to replace pip with pip3
pip install Flask
23. Có # we might need to replace pip with pip3
pip install Flask
23, chúng tôi xác định thư mục làm việc mà chúng tôi sẽ sử dụng trong hình ảnh và sao chép mã cần thiết để khởi động và chạy ứng dụng. Trong bước thứ tư, chúng tôi sử dụng # we might need to replace pip with pip3
pip install Flask
23 để cài đặt tất cả các phụ thuộc Python của chúng tôi. Cuối cùng, chúng tôi xác định rằng hình ảnh của chúng tôi sẽ giao tiếp qua cổng ...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
325 và hình ảnh này, khi được thực thi, cần chạy tập lệnh # we might need to replace pip with pip3
pip install Flask
50 để khởi động FlaskGhi chú. Đối với
319 của chúng tôi, chúng tôi sử dụng Python phiên bản 3. 8, tuy nhiên, tùy thuộc vào cấu hình hệ thống của bạn,... in_memory_datastore = { "COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"}, "ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"}, "APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"}, "BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"}, "PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"}, "SIMULA67": {"name": "SIMULA67", "publication_year": 1967, "contribution": "class/object split, subclassing, protected attributes"}, "Pascal": {"name": "Pascal", "publication_year": 1970, "contribution": "modern unary, binary, and assignment operator syntax expectations"}, "CLU": {"name": "CLU", "publication_year": 1975, "contribution": "iterators, abstract data types, generics, checked exceptions"}, } ...
23 có thể đã đặt một phiên bản khác cho Python trong tệp# we might need to replace pip with pip3 pip install Flask
24. Vui lòng đảm bảo rằng phiên bản Python trong cả# we might need to replace pip with pip3 pip install Flask
319 và... in_memory_datastore = { "COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"}, "ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"}, "APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"}, "BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"}, "PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"}, "SIMULA67": {"name": "SIMULA67", "publication_year": 1967, "contribution": "class/object split, subclassing, protected attributes"}, "Pascal": {"name": "Pascal", "publication_year": 1970, "contribution": "modern unary, binary, and assignment operator syntax expectations"}, "CLU": {"name": "CLU", "publication_year": 1975, "contribution": "iterators, abstract data types, generics, checked exceptions"}, } ...
24 được căn chỉnh, nếu không bộ chứa docker sẽ không thể khởi động máy chủ# we might need to replace pip with pip3 pip install Flask
Để tạo và chạy bộ chứa Docker dựa trên
...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
319 mà chúng tôi đã tạo, chúng tôi có thể thực hiện các lệnh sau# we might need to replace pip with pip3
pip install Flask
3...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
319 đơn giản nhưng hiệu quả và việc sử dụng nó cũng dễ dàng tương tự. Với các lệnh này và ...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
319 này, chúng tôi có thể chạy bao nhiêu phiên bản API tùy thích mà không gặp sự cố nào. Vấn đề chỉ là xác định một cổng khác trên máy chủ hoặc thậm chí là một máy chủ khácBảo mật API Python với Auth0
Bảo mật API Python bằng Auth0 rất dễ dàng và mang lại nhiều tính năng tuyệt vời cho bảng. Với Auth0, chúng ta chỉ cần viết vài dòng code để lấy
- Một giải pháp quản lý danh tính vững chắc, bao gồm đăng nhập một lần
- quản lý người dùng
- Hỗ trợ cho các nhà cung cấp nhận dạng xã hội [như Facebook, GitHub, Twitter, v.v. ]
- Nhà cung cấp nhận dạng doanh nghiệp [Active Directory, LDAP, SAML, v.v. ]
- Cơ sở dữ liệu riêng của chúng tôi về người dùng
Ví dụ: để bảo mật các API Python được viết bằng Flask, chúng ta chỉ cần tạo một trình trang trí
...
in_memory_datastore = {
"COBOL": {"name": "COBOL", "publication_year": 1960, "contribution": "record data"},
"ALGOL": {"name": "ALGOL", "publication_year": 1958, "contribution": "scoping and nested functions"},
"APL": {"name": "APL", "publication_year": 1962, "contribution": "array processing"},
"BASIC": {"name": "BASIC", "publication_year": 1964, "contribution": "runtime interpretation, office tooling"},
"PL": {"name": "PL", "publication_year": 1966, "contribution": "constants, function overloading, pointers"},
"SIMULA67": {"name": "SIMULA67", "publication_year": 1967,
"contribution": "class/object split, subclassing, protected attributes"},
"Pascal": {"name": "Pascal", "publication_year": 1970,
"contribution": "modern unary, binary, and assignment operator syntax expectations"},
"CLU": {"name": "CLU", "publication_year": 1975,
"contribution": "iterators, abstract data types, generics, checked exceptions"},
}
...
335# we might need to replace pip with pip3
pip install Flask
4Sau đó, sử dụng nó trong các điểm cuối của chúng tôi
# we might need to replace pip with pip3
pip install Flask
5Để tìm hiểu thêm về cách bảo mật API Python bằng Auth0, hãy xem hướng dẫn này. Cùng với các hướng dẫn về công nghệ phụ trợ [như Python, Java và PHP], trang web Tài liệu Auth0 cũng cung cấp các hướng dẫn cho ứng dụng Di động/Gốc và ứng dụng Trang đơn
Bước tiếp theo
Trong bài viết này, chúng ta đã tìm hiểu về các thành phần cơ bản cần thiết để phát triển ứng dụng Flask có cấu trúc tốt. Chúng tôi đã xem xét cách sử dụng
# we might need to replace pip with pip3
pip install Flask
23 để quản lý các phụ thuộc của API của chúng tôi. Sau đó, chúng tôi đã cài đặt và sử dụng Flask và Marshmallow để tạo các điểm cuối có khả năng nhận và gửi phản hồi JSON. Cuối cùng, chúng tôi cũng đã xem xét cách neo đậu API, điều này sẽ tạo điều kiện thuận lợi cho việc phát hành ứng dụng lên đám mâyMặc dù có cấu trúc tốt nhưng API của chúng tôi vẫn chưa hữu ích. Trong số những điều chúng tôi có thể cải thiện, chúng tôi sẽ đề cập đến các chủ đề sau trong bài viết sau