-
Notifications
You must be signed in to change notification settings - Fork 0
faq
- The
site_rootis the directory in which krait is run. - The
site_rootshould contain a directory named.configand, for MVC sites, additionally, one usually named.viewand directories named.ctrland / or.py(I recommend no.ctrl, only.py) - The
routable pages/filesare the routes' targets (the files that the URL requested by the client resolve to). These are different from both Python scripts inside.pyor views, or, in the context of a specific request, source files other than the original page that is run.
-
How to create a simple non-MVC page or css/js/png/etc file?
You just drop any file insidesite_rootor any directory and it will be served, as long as there is no directory (or file) in its path with a leading dot (these pages will just 404 - use this to hide pages, such as scripts, from clients). -
How can I access the request from Python / the controller / etc.
- Starting from v0.2, Krait exposes all these variables (with the exception of
ctrl) in thekraitmodule. Previously, you had to be in a routable page to access them, but now your code can be much cleaner: justimport kraitand use them. - Krait exposes the following variables in the
kraitmodule:-
krait.requestcontains information about the client's request. Commonly used attributes / methods:headers,body,query,get_post_form()(but others exist) -
ctrlis the controller assigned in MVC pages. This is available only in MVC views., and is a global variable (not in in thekraitmodule). -
krait.site_rootis the path to the site root. It may be absolute or relative, depending on how krait was run. -
krait.response: set it to something other thanNoneto override the server's response from Python code. -
krait.extra_headers: set it to something other thanNoneto add response headers from Python code (for example, cookies, without overriding the entire response) -
krait.content_type: set withkrait.set_content_type(raw='...')orkrait.set_content_type(ext='...')to change theContent-Typeheader from Python code (raw, or from file extension, respectively). - modules
kraitandmvcare already imported (but only in routable pages, not in your modules).
-
- Starting from v0.2, Krait exposes all these variables (with the exception of
-
How to create a simple MVC page?
Let's say you're trying to create an MVC page at/folder/page. Obviously, a folder is not necessary, or there cand be more (like/parent/child/page)- First, create directories
site_root/folder,site_root/.py/ctrl/folderandsite_root/.view/folderif they don't exist. - Then, let's create the controller. Create a file named
site_root/.py/ctrl/folder/page.pythat contains the following code:Makeimport mvc class PageController(mvc.CtrlBase): def __init__(self, arg1, arg2): # args: whatever your controller needs to know to show on the page self.attr1 = arg1 # set controller attributes to your arguments self.attr2 = arg2 self.attr3 = arg1.attr3 # obviously not only arguments # you can also query the DB here, anything that the controller needs to expose to the view def get_view(self): return ".view/folder/page.html"get_viewreturn whatever view you want your controller to use. - Then let's create the routable page. Create a file named
site_root/folder/page.pythat contains the following code:from ctrl.folder import page import mvc mvc.set_init_ctrl(page.PageController(arg1, arg2)) # arguments are whaterver the controller expects (for example, the request) - Then, let's create the view. Create a file named
site_root/.view/folder/page.html(or whatever your controller'sget_view()returns). This is an usual HTML page that can also reference variables in the controller. See the pyml syntax available here for details. The variablectrlreferences the object given as an argument tomvc.set_init_ctrl()in the routable page (use like@ctrl.attr1).
IMPORTANT: The view must set its content-type. This is achieved with the following code:Add this, for example, in the@{krait.set_content_type(ext="html")}<head>...</head>part.
- First, create directories
-
How to create a page/script accessible via POST?
Let's say the page you are trying to access via POST is/folder/page. Add a route to.config/routes.jsonlike this:[ ... other routes ... { "verb": "POST", "url": "/folder/page" }, ... other routes ... ]If the last route (entry in the JSON array) is not
{}or{"verb": "GET"}, you might want to add it. This will make all your normal pages accessible (this is a default route, because it has nourlorregex, it automatically matches all GET requests.) -
How to create a simple Python script (no client output or redirect)?
Let's say you are trying to create a Python script at/folder/script. Just drop a file namedscript.pyinsite_root/folderand it will run anytime a client requests/folder/script. The client will receive a empty response (after HTTP headers), so this may only be useful in the context of AJAX requests. -
How to create a simple Python script (redirect after execution)?
Let's say you are trying to create a Python script at/folder/script. In a file namedscript.pyplaced in the appropriate directory, runkrait.response = krait.ResponseRedirect('your_destination_url')along with any scripts you'd like to run. -
How to create a Python script with output?
Just create a.pymlfile with a large@{ ... }block and an@expressionstatement at the end. If you can't change the extension, don't forget to callset_content_type(). Alternatively, instead of the@{ ... }block you can@importa Python script from a hidden folder, like.py(like this:@import ".py/subdirectory/script.py"). -
How to use form data?
- You use the
krait.requestvariable set by the server. - For GET forms, the form is in
krait.request.query. This is a Pythondict, so usekrait.request.query["name"]if you're sure it exists orkrait.request.query.get("name")if it might not exist. - For POST forms, the form data is saved in the request body and must be parsed. For normal forms, use
krait.request.get_post_form()(this is adicttoo). For multipart forms, usekrait.request.get_multipart_form()(this is a list of objects with the following fields:name,filename,content_typeanddata)
- You use the