Package: tiny-web
tiny_webPython WSGI micro-framework to build api/siteInstallationpipinstalltiny_webQuickstartfromtiny_web.apiimportApiapp=Api()# Basic route@app.route("/home")defhome(request,response):response.text="Hello from the HOME page"returnresponse# Parametrized route@app.route("/hello/{name}")defgreeting(request,response,name):response.text=f"Hello,{name}"returnresponse'''Class based controller.Class methods are handlers for http request methods'''@app.route("/book")classBooksResource:defget(self,req,resp):resp.text="Books Page"returnresponsedefpost(self,req,resp):resp.text="Endpoint to create a book"returnresponse'''You can response with templates.Templates may be served in "templates" folder.Jinja2 used as a template engine.'''@app.route("/template")deftemplate_handler(req,resp):resp.body=app.template("index.html",context={"name":"Bumbo","title":"Best Framework"}).encode()returnresponse'''You also can use query string parameters."tiny_web" uses "webob" library to wrap requests and responses.So if query looks like "/users?name=john" you can handle params like this.See more on https://docs.pylonsproject.org/projects/webob/en/stable/reference.html'''@app.route("/users")defusers(request,response):name=request.GET.get("name")response.html=f"Hello{name}"returnresponse'''You may use helpers for html or json'''@app.route("/users")defusers(request,response):response.html=app.template("users.html")returnresponse@app.route("/items")defusers(request,response):items=[{'id':1,'name':'item_1'},{'id':2,'name':'item_2'}]response.json={'items':items}returnresponseUnit TestsThe recommended way of writing unit tests is withpytest. There are two built in fixtures
that you may want to use when writing unit tests with Bumbo. The first one isappwhich is an instance of the mainAPIclass:deftest_route_overlap_throws_exception(app):@app.route("/")defhome(req,resp):resp.text="Welcome Home."withpytest.raises(AssertionError):@app.route("/")defhome2(req,resp):resp.text="Welcome Home2."The other one isclientthat you can use to send HTTP requests to your handlers. It is based on the famousrequestsand it should feel very familiar:deftest_parameterized_route(app,client):@app.route("/{name}")defhello(req,resp,name):resp.text=f"hey{name}"assertclient.get("http://testserver/john").text=="hey john"TemplatesThe default folder for templates istemplates. You can change it when initializing the mainAPI()class:app=API(templates_dir="templates_dir_name")Then you can use HTML files in that folder like so in a handler:@app.route("/show/template")defhandler_with_template(req,resp):resp.html=app.template("example.html",context={"title":"Hello","body":"World!"})Static FilesJust like templates, the default folder for static files isstaticand you can override it:app=API(static_dir="static_dir_name")Then you can use the files inside this folder in HTML files:<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>{{title}}</title><linkhref="/static/main.css"rel="stylesheet"type="text/css"></head><body><h1>{{body}}</h1><p>This is a paragraph</p></body></html>Also you may want use custom 404 page, just make '404.html' in the root of templates folder.MiddlewareYou can create custom middleware classes by inheriting from thebumbo.middleware.Middlewareclass and overriding its two methods
that are called before and after each request:frombumbo.apiimportAPIfrombumbo.middlewareimportMiddlewareapp=API()classSimpleCustomMiddleware(Middleware):defprocess_request(self,req):print("Before dispatch",req.url)defprocess_response(self,req,res):print("After dispatch",req.url)app.add_middleware(SimpleCustomMiddleware)TODO:TemplatesExceptionsStatic filesMiddlewaresDocumentationBuild packageDockerize and deploy demo