Package: bumbo-ivan
Bumbo: Python Web Framework built for learning purposesBumbo is a Python web framework built for learning purposes.It's a WSGI framework and can be used with any WSGI application server such as Gunicorn.Installationpipinstallbumbo-ivanHow to use itBasic usage:frombumbo.apiimportAPIapp=API()@app.route("/home")defhome(request,response):response.text="Hello from the HOME page"@app.route("/hello/{name}")defgreeting(request,response,name):response.text=f"Hello,{name}"@app.route("/book")classBooksResource:defget(self,req,resp):resp.text="Books Page"defpost(self,req,resp):resp.text="Endpoint to create a book"@app.route("/template")deftemplate_handler(req,resp):resp.body=app.template("index.html",context={"name":"Bumbo","title":"Best Framework"}).encode()Unit 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/matthew").text=="hey matthew"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":"Awesome Framework","body":"welcome to the future!"})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>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)ORM APIThis is a orm with a SQLite support.Connect to the database:frombumbo.ormimportDatabasedb=Database("./test.db")Define tables:formbumbo.ormimportTable,Column,ForaignKey...classAuthor(Table):name=Column(str)age=Column(int)classBook(Table):title=Column(str)published=Column(bool)author=ForaignKey(Author)Create tables:db.create(Author)db.create(Book)Create an instance and insert a row:greg=Author(name="George",age=13)db.save(greg)Fetch all rows:author=db.all(Author)Get a specific row:author=db.get(Author,47)Save an object with a foreign key reference:book=Book(title="Building an ORM",published=True,author=greg)db.save()Fetch an object with a foreign key:print(Book.get(55).author.name)Update an object:book.title="How to build an ORM"db.update(book)Delete an object:db.delete(Book,id=book.id)