This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import FastAPI | |
app = FastAPI() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pip install fastapi[all] uvicorn[all] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import FastAPI, Response | |
from fastapi.responses import JSONResponse | |
app = FastAPI(title="Demo REST Server") | |
@app.get("/hello") | |
async def hello() -> Response: | |
return JSONResponse({"message": "Hello, World"}) | |
@app.get("/get_user/{user_id}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pydantic import BaseModel | |
class Item(BaseModel): | |
name: str | |
price: float | |
quantity: int | |
@app.put("/update/{item_id}") | |
async def update_item(item_id: int, item: Item) -> Response: | |
return JSONResponse({ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from FastAPI import Form, File, UploadedFile | |
from FastAPI.responses import JSONResponse | |
@app.post("/upload/") | |
async def upload_file(file_id: int = Form(), file: UploadFile = File()) -> Response: | |
return JSONResponse({ | |
"file_id": file_id, | |
"file_name": file.filename, | |
"file_type": file.content_type | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.get("/view_request_info") | |
async def view_request_info(request: Request) -> Response: | |
return JSONResponse({k: v for k, v in request.headers.items()}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.middleware("http") | |
async def check_token(request: Request, _next) -> Response: | |
if request.headers["apiKey"] != "this_is_my_key": | |
return JSONResponse({"message": "Your key is mismatched"}) | |
start_time = time.time() | |
response: Response = await _next(request) | |
response.headers["X-Process-Time"] = str(time.time() - start_time) | |
return response |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import List | |
from fastapi import FastAPI | |
from fastapi import WebSocket, Request, WebSocketDisconnect | |
from fastapi.templating import Jinja2Templates | |
templates = Jinja2Templates("template") | |
app = FastAPI(title="Demo WebSocket Chatroom") | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Chatroom</title> | |
</head> | |
<body> | |
<h1>WebSocket Chatroom</h1> | |
<h2>Your ID: <span id="ws-id"></span></h2> | |
<form action="" onsubmit="sendMessage(event)"> | |
<input type="text" id="messageText" autocomplete="off"/> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import keras | |
from __future__ import print_function | |
from keras.datasets import fashion_mnist | |
from keras.layers import Dense, Flatten | |
from keras.layers import Conv2D, MaxPooling2D | |
from keras.models import Sequential | |
import matplotlib.pylab as plt | |
batch_size = 128 | |
num_classes = 10 |