from random import randint from sanic import Sanic from sanic.response import text app = Sanic("Example") @app.middleware("request") def append_request(request): request.ctx.num = randint(0, 100) @app.get("/pop") def pop_handler(request): return text(request.ctx.num) @app.get("/key_exist") def key_exist_handler(request): # Check the key is exist or not if hasattr(request.ctx, "num"): return text("num exist in request") return text("num does not exist in request") app.run(host="0.0.0.0", port=8000, debug=True)
Related articles
authorized sanic
# -*- coding: utf-8 -*- from functools import wraps from sanic import Sanic from sanic.response import json
sanic blueprints
from sanic import Blueprint, Sanic from sanic.response import file, json app = Sanic("Example") blueprint = Blueprint("bp_example", url_prefix="/my_blueprint") blueprint2 = Blueprint("bp_example2", url_prefix="/my_blueprint2")
fastapi request directly
from fastapi import FastAPI, Request app = FastAPI() @app.get("/items/{item_id}") def read_root(item_id: str, request: Request):
fastapi websockets
from fastapi import FastAPI, WebSocket from fastapi.responses import HTMLResponse app = FastAPI()
simple fastapi wsgi
from fastapi import FastAPI from fastapi.middleware.wsgi import WSGIMiddleware from flask import Flask, escape, request
matplotlib multicursor
Showing a cursor on multiple plots simultaneously. This example generates three axes split over two different figures. On hovering the cursor over data in one subplot, the values of that datapoint are shown in all axes
polygon selector
import matplotlib.pyplot as plt from matplotlib.widgets import PolygonSelector # To create the polygon programmatically fig, ax = plt.subplots() fig.show() selector = PolygonSelector(ax, lambda *args: None)
matplotlib radio buttons
Using radio buttons to choose properties of your plot. Radio buttons let you choose between multiple options in a visualization. In this case, the buttons let the user choose one of the three different sine waves to be shown in the plot.
sanic delayed response
from asyncio import sleep from sanic import Sanic, response app = Sanic("DelayedResponseApp", strict_slashes=True) app.config.AUTO_EXTEND = False
sanic exception monitoring
from sanic.exceptions import SanicException from sanic.handlers import ErrorHandler class CustomHandler(ErrorHandler): def default(self, request, exception): # Here, we have access to the exception object