项目作者: cobobrien

项目描述 :
A WSGI web application framework
高级语言: Python
项目地址: git://github.com/cobobrien/fletch.git
创建时间: 2020-02-15T16:07:26Z
项目社区:https://github.com/cobobrien/fletch

开源协议:

下载


Fletch: Python Web Framework built for learning purposes

purpose
PyPI

Fletch - PyPi

Fletch 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.

Installation

  1. pip install fletch

How to use it

Basic usage:

  1. from fletch.api import API
  2. app = API()
  3. @app.route("/home")
  4. def home(request, response):
  5. response.text = "Hello from the HOME page"
  6. @app.route("/hello/{name}")
  7. def greeting(request, response, name):
  8. response.text = f"Hello, {name}"
  9. @app.route("/book")
  10. class BooksResource:
  11. def get(self, req, resp):
  12. resp.text = "Books Page"
  13. def post(self, req, resp):
  14. resp.text = "Endpoint to create a book"
  15. @app.route("/template")
  16. def template_handler(req, resp):
  17. resp.body = app.template(
  18. "index.html", context={"name": "Fletch", "title": "WSGI Framework"}).encode()

Unit Tests

The recommended way of writing unit tests is with pytest. There are two built in fixtures
that you may want to use when writing unit tests with Fletch. The first one is app which is an instance of the main API class:

  1. def test_route_overlap_throws_exception(app):
  2. @app.route("/")
  3. def home(req, resp):
  4. resp.text = "Welcome Home."
  5. with pytest.raises(AssertionError):
  6. @app.route("/")
  7. def home2(req, resp):
  8. resp.text = "Welcome Home2."

The other one is client that you can use to send HTTP requests to your handlers. It is based on the famous requests and it should feel very familiar:

  1. def test_parameterized_route(app, client):
  2. @app.route("/{name}")
  3. def hello(req, resp, name):
  4. resp.text = f"hey {name}"
  5. assert client.get("http://testserver/matthew").text == "hey matthew"

Templates

The default folder for templates is templates. You can change it when initializing the main API() class:

  1. app = API(templates_dir="templates_dir_name")

Then you can use HTML files in that folder like so in a handler:

  1. @app.route("/show/template")
  2. def handler_with_template(req, resp):
  3. resp.html = app.template(
  4. "example.html", context={"title": "Awesome Framework", "body": "welcome to the future!"})

Static Files

Just like templates, the default folder for static files is static and you can override it:

  1. app = API(static_dir="static_dir_name")

Then you can use the files inside this folder in HTML files:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>{{title}}</title>
  6. <link href="/static/main.css" rel="stylesheet" type="text/css">
  7. </head>
  8. <body>
  9. <h1>{{body}}</h1>
  10. <p>This is a paragraph</p>
  11. </body>
  12. </html>

Middleware

You can create custom middleware classes by inheriting from the fletch.middleware.Middleware class and overriding its two methods
that are called before and after each request:

  1. from fletch.api import API
  2. from fletch.middleware import Middleware
  3. app = API()
  4. class SimpleCustomMiddleware(Middleware):
  5. def process_request(self, req):
  6. print("Before dispatch", req.url)
  7. def process_response(self, req, res):
  8. print("After dispatch", req.url)
  9. app.add_middleware(SimpleCustomMiddleware)