Response

class bobtail.response.Response(options: BaseOptions)[source]

The Response object is available from within a Route method.

set_body(value: Dict) None[source]

Set the body of the request. For example:

def get(self, req: Request, res: Response):
    res.set_body({id: 1})
Parameters:

value

set_headers(value: Dict) None[source]

Headers can be set from within a route method. The Content-Type header get set dynamically from Response methods. By default, Content-Type is set to application/json. For Example:

def get(self, req: Request, res: Response):
    res.set_headers({
        "Content-Type": "application/json",
    })
Parameters:

value – Excepts a dict

set_html(template_str: str) None[source]

Renders an HTML string. See TEMPLATE_DIR for the template directory path. For example:

template_str = "<h1>Hello!</h1>"

def get(self, req: Request, res: Response):
    res.set_html(template_str)
Parameters:

template_str

set_static(path: str)[source]

To declare a static route postfix a * to the route’s path:

from bobtail import BobTail AbstractRoute, BaseOptions
from bobtail_jinja2 import BobtailJinja2

routes = [
    (Static(), "/static/*"),
]

class Options(BaseOptions):
    STATIC_DIR = "app/static"
    TEMPLATE_DIR = "app/templates"

blog = BobTail(routes=routes, options=Options())
blog.use(BobtailJinja2(template_dir="app/templates"))

Calling set_static from within a route method will render a static file such as a .css, .js or a media type file. The BaseOptions class sets the STATIC_DIR directory. For example:

class Static(AbstractRoute):
    def get(self, req: Request, res: Response) -> None:
        res.set_static(req.path)

You can set the static file path using the BaseOptions. For example:

class Options(BaseOptions):
    STATIC_DIR = "/static"

# Now in a route handler we can access static directory the via options
class Static(AbstractRoute):
    def get(self, req: Request, res: Response) -> None:
        res.set_static(req.path)

By default, STATIC_DIR is set to /static, if your static file is nested within a Python package, for example app/static the set as STATIC_DIR = “app/static”

To render an image from within a Jinja2 template include the full path including the static directory name or path. For example:

<!-- if STATIC_DIR = "/static" -->
<body>
    <img src="/static/imgs/cat1.jpg" />
</body>

OR without the first forward slash:

<body>
    <img src="static/imgs/cat1.jpg" />
</body>
Parameters:

path

set_status(value: int) None[source]

Set the response status. You can set the status with the Response object’s set_status method. The default status is always set to 200 if there are no errors. For Example:

def get(self, req: Request, res: Response):
    res.set_headers({"Content-type": "text/plain"})
Parameters:

value – Sets the response status