How to use the klein.Plating function in klein

To help you get started, we’ve selected a few klein examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github twisted / klein / loginspike.py View on Github external
if TYPE_CHECKING:
    from hyperlink import DecodedURL
    # silence flake8 for type-checking
    (ISessionProcurer, Deferred, IRequest, Dict, Tag, Any, RenderableForm,
     ISessionStore, DecodedURL)

app = Klein()

def bootstrap(x):
    # type: (str) -> str
    return "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/" + x

requirer = Requirer()

style = Plating(
    tags=tags.html(
        tags.head(
            tags.link(rel="stylesheet",
                      href=bootstrap("css/bootstrap.min.css"),
                      crossorigin="anonymous"),
            # tags.script(
            #     src=bootstrap("js/bootstrap.min.js"),
            #     crossorigin="anonymous"
            # ),
            tags.title("hooray")
        ),
        tags.body(
            tags.nav(Class="navbar navbar-expand-lg navbar-light bg-light")(
                tags.a("Navbar", Class="navbar-brand",
                       href="/"),
                tags.ul(Class="nav navbar-nav")(
github twisted / klein / loginspike.py View on Github external
)
@inlineCallbacks
def logOtherOut(sessionID, binding, url):
    # type: (str, ISimpleAccountBinding, DecodedURL) -> Deferred
    from attr import assoc
    # TODO: public API for getting to this?
    session = yield binding._session._sessionStore.loadSession(
        sessionID, url.scheme == u'https', SessionMechanism.Header
    )
    other_binding = assoc(binding, _session=session)
    yield other_binding.unbindThisSession()
    returnValue(Redirect(b"/sessions"))



widget = Plating(
    tags=tags.tr(style=slot("highlight"))(
        tags.td(slot("id")), tags.td(slot("ip")),
        tags.td(slot("when")),
        tags.td(tags.form(action="/sessions/logout",
                          method="POST")
                (tags.button("logout"),
                 tags.input(type="hidden", value=[slot("id")],
                            name="sessionID"),
                 slot("glue")))
    ),
    presentation_slots=["glue", "highlight"]
)

@widget.widgeted
def oneSession(session_info, form, current):
    # type: (Any, RenderableForm, bool) -> Dict
github twisted / klein / docs / introduction / codeexamples / forms.py View on Github external
from klein import Klein, Plating, Form, Field, Requirer, SessionProcurer
from klein.interfaces import ISession
from klein.storage.memory import MemorySessionStore

app = Klein()

sessions = MemorySessionStore()

requirer = Requirer()

@requirer.prerequisite([ISession])
def procurer(request):
    return SessionProcurer(sessions).procureSession(request)


style = Plating(tags=tags.html(
    tags.head(tags.title("yay")),
    tags.body(tags.div(slot(Plating.CONTENT))))
)

@style.routed(
    requirer.require(
        app.route("/", methods=["POST"]),
        foo=Field.integer(minimum=3, maximum=10), bar=Field.text(),
    ),
    tags.h1('u did it: ', slot("an-form-arg"))
)
def postHandler(foo, bar):
    return {"an-form-arg": foo}

@requirer.require(
    style.routed(
github twisted / klein / docs / introduction / codeexamples / template.py View on Github external
from __future__ import print_function, unicode_literals
# Cobble together a deterministic random function using a string as a seed.
from random import Random
from hashlib import sha256
from struct import unpack

def random_from_string(string):
    return Random(
        unpack("!I", sha256(string.encode("utf-8")).digest()[:4])[0]
    )

from twisted.web.template import tags, slot
from klein import Klein, Plating
app = Klein()

myStyle = Plating(
    tags=tags.html(
        tags.head(tags.title(slot("pageTitle"))),
        tags.body(tags.h1(slot("pageTitle"), Class="titleHeading"),
                  tags.div(slot(Plating.CONTENT)))
    ),
    defaults={"pageTitle": "Places & Foods"}
)

@myStyle.routed(
    app.route("/"),
    tags.div(
        tags.h2("Sample Places:"),
        tags.ul([tags.li(tags.a(href=["/places/", place])(place))
                 for place in ["new york", "san francisco", "shanghai"]]),
        tags.h2("Sample Foods:"),
        tags.ul([tags.li(tags.a(href=["/foods/", food])(food))