pip install "fastapi[standard]"
fastapi dev main.py
or
python -m fastapi dev main.py
sample code
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/users/{user}")
async def users(user):
return {"message": f"hello {user}"}
@app.get("/items/{item_id}")
async def read_item(item_id):
return {"item_id": item_id}
add email path
/email/{email}
return email
add task
return returen html content
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
app = FastAPI()
UsersEmail = []
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/email_address/{email_address}")
async def email_address(email_address):
return {"confirmed email address": f"{email_address}"}
@app.get("/info/emails")
async def infoEmails():
return {"emails": UsersEmail}
# task1 dedup user emails
@app.get("/your_email_address/{your_email_address}", response_class=HTMLResponse)
async def your_email_address(your_email_address):
UsersEmail.append(your_email_address)
return "ok"
# task2 交互式保存用户邮件地址信息
@app.get("/index", response_class=HTMLResponse)
async def index():
return f"""
<!DOCTYPE html>
<html>
<head>
<title>哔哩哔哩 (゜-゜)つロ 干杯~-bilibili</title>
</head>
<body>
<h1>You entered email address as below: </h1>
<input>in put your email address</input>
</body>
</html>
"""