API Reference
Handler
class handler.Handler(setting=None)
This is the central object of the whole web server, responsible for storing, adding, and finding back the views/pages of the website, and storing the settings of how the responses should be made, etc.
You can simply write these lines at the top of your main script to set up a proper handler:
from kripweb.handler import Handler
handler = Handler()
function page(url="", method="GET", take_request=False, name="")The decorator to add a view to your website. If there's no such page(Node
), the function will automatically create one for convenience.Arguments:
- url: the url path that will lead to this view
- method: the HTTP request method that this view will handle, usually "GET" or "POST"
- take_request: to select whether a
Request
object about the HTTP request should be called with. Select this to True and put arequest
parameter in the view function to use it.- name: a name to identify the current
Node
(or simply, the GET view of the page), used forRedirect.to_view()
.function error_page(err_code="404", take_request=False)The decorator to add an error page view to your website.Arguments:
- err_code: the type of error that this view will handle, represented as an error code. The current error codes are:
- "404": The typical 404 Not Found error. Called when a client tried to access a page that does not exist.
- "bad_host": Called when a client tries to access any webpage without using an allowed hostname. Ignored if no allowed hostnames are set. It's recommended to simply
Redirect
the client to an allowed host.- take_request: same as
handler.page
.function get_application()
Returns anASGIApplication
instance which set up an interface for other ASGI-supported modules to work with.
Currently, thiskripweb
module cannot work by itself as the server management part is not implemented yet. That's why in all examples,uvicorn
is playing a role to manage the server.function get_page(url)
Pathfind the matchingNode
object with the url.Arguments:
- url: the url path of the
Node
required.Returns:
Node
that includes the views and pages information corresponding to the url given
orDNENode
if the required url page cannot be found.function ingest_handler(subhandler)A way to include the views from other scripts, as a so-called "subpages".Argument:
- subhandler: A subhandler for handling other pages.
PagesHandler
is the only subhandler for now.function name_to_url(page_name, from_subpages="")Convert an internal name to the url path of the view. Usually used byRedirect.to_view()
.Arguments:
- page_name: The internal name of the
Node
, the same one as whenhandler.page()
is called.- from_subpages: The subpages' name that the
Node
is from, the same one as whenPagesHandler()
is called in any other scripts. An empty string indicates that the view is in the main script.Return: The url path of the wanted view.
function static_url_for(filename)Convert a filename(filepath) to a static url path of the website. Usually used byStaticResponse()
and sometimes by you in Jinja2 HTML files with{{ static() }}
method.Arguments:
- filename: the filename to locate the static file. The static path will be added thus there's no need to manually add it.
Return: The static url path.
Setting
class setting.Setting()
This is responsible for holding any freely-changable variables that affect how the handler should behave when finding things or working with codes.
There are no functions other than the setters of the variables, so below is the list of the variables and their functions.
You can change these settings anytime during the program (not quite recommended tho), and everything should follow the changes immediately without any restart.
This also means that the variables can be set directly when you initiate the Setting class, or by calling the setters at anytime.template_path :str = "template/"Change withset_template_path()
To locate the folder of the template files, relative to the main programstatic_path :str = "static/"Change withset_static_path()
To locate the folder of the static files, relative to the main programawait_send_mode :bool = FalseToggle withtoggle_await_send_mode()
To show how your functions respond to web requests.False: the functions return the Response object directly
True: the functions take asend
function as a parameter, and respond by callingawait send(resp)
hosts_allowed :list = []Append withallow_host()
To make a semi firewall the blocks requests that did not use the ips or hostnames to access the pages.
You can set how the program should behave after meeting this program with@handler.error_page(err_code="bad_host")
or leave it as default.static_url :str = "/static/"Change withset_static_url()
To locate the url branch for static files' locations.
This does not affect where you put the static files, but only where you find them by directly putting in the url.
Although you should not be using /static/ as an url branch for normal webpages, you can change this variable if it collides with your code.print_connecton_information :bool = TrueToggle withtoggle_print_conn_info()
To toggle whether you want the information about visitors accessing your pages to be printed.
This is helpful if you want to debug your code by printing something else out and not have conn info mixed with your own debug messages.
You should leave it True most of the time because it helps identify unexpected traffics.app_logging_msg :callable = constant.app_logging_message()Change withset_app_logging_msg()
To customize the conn info printed.
As there are quite a lot of information that you may or may not want to see, you have the full access to the request and response objects to pull out whatever you need and arrange them however you want.
The parameter function should accept arequest
and aresponse
, then return a string. The returning string is what you see after the INFO: keyword.