Pyjo - Web development toolkit¶
# HTTP/WebSocket user agent
import Pyjo.UserAgent
ua = Pyjo.UserAgent.new()
print(ua.get('www.mojolicious.org').result.headers.server)
# HTML/XML DOM parser with CSS selectors
import Pyjo.DOM
dom = Pyjo.DOM.new('<div><b>Hello Pyjo!</b></div>')
print(dom.at('div > b').text)
# Python-ish templates
import Pyjo.Template
mt = Pyjo.Template.new(vars=True)
print(mt.render('Hello <%= what %>!', {'what': 'Pyjo'}))
# HTTP/WebSocket server
import Pyjo.Server.Daemon
daemon = Pyjo.Server.Daemon.new(listen=['http://*:8080'])
@daemon.unsubscribe('request').on('request')
def request(daemon, tx):
tx.res.code = 200
tx.res.body = b'Hello Pyjo!'
tx.resume()
daemon.run()
# Event loop
import Pyjo.IOLoop
for seconds in range(1, 6):
Pyjo.IOLoop.timer(seconds, lambda loop, seconds=seconds: print(seconds))
Pyjo.IOLoop.start()
A powerful web development toolkit, with all the basic tools and helpers
needed to write simple web applications and higher level web frameworks, such
as Pyjoyment. Some of the most commonly used tools are
Pyjo.UserAgent, Pyjo.DOM, Pyjo.JSON,
Pyjo.Server.Daemon, Pyjo.Server.Prefork, Pyjo.IOLoop and
Pyjo.Template.
One-liners¶
$ python -m Pyjo 'g("mojolicious.org").dom().at("title").text'
A collection of functions for fun Python one-liners, which
from Pyjo import * imports, and with which python -m Pyjo runs its
code: the value of an expression is printed, and the arguments after the code
are in sys.argv, whose module sys the code has without an
import.
$ python -m Pyjo 'g(sys.argv[1]).code' example.com
Ten redirects will be followed by default, you can change this behavior with
the PYJO_MAX_REDIRECTS environment variable.
$ PYJO_MAX_REDIRECTS=0 python -m Pyjo 'g("example.com").code'
Proxy detection is enabled by default, but you can disable it with the
PYJO_PROXY environment variable.
$ PYJO_PROXY=0 python -m Pyjo 'g("example.com").body'
TLS certificate verification can be disabled with the PYJO_INSECURE
environment variable.
$ PYJO_INSECURE=1 python -m Pyjo 'g("https://127.0.0.1:3000").body'
If it is not already defined, python -m Pyjo will set the
PYJO_LOG_LEVEL environment variable to fatal.
Pyjo is a namespace package that may be split across multiple
distributions, as another reactor in Pyjo.Reactor comes from one of its
own.
See also¶
Functions¶
- Pyjo.a(*args: Any, **constraints: Any) Any[source]¶
app = a('/hello', lambda c: c.render(json={'hello': 'world'}))
Create a route with
anyofPyjoyment.Liteand return the currentPyjoyment.Liteobject, the application of the module that calls it.$ python -m Pyjo 'a("/hello", {"text": "Hello Pyjo!"}).start()' daemon
- Pyjo.b(*parts: object) Pyjo.String.Bytes.object[source]¶
stream = b(b'lalala')
Turn bytes into a
Pyjo.String.Bytesobject.$ python -m Pyjo 'b(g("mojolicious.org").body).html_unescape()'
- Pyjo.c(*elements: T) Pyjo.Collection.object[T][source]¶
collection = c(1, 2, 3)
Turn list into a
Pyjo.Collectionobject.
- Pyjo.d(*args: Any, **kwargs: Any) Pyjo.Message.Response.object[source]¶
res = d('example.com') res = d('http://example.com', {'Accept': '*/*'}, b'Hi!') res = d('http://example.com', {'Accept': '*/*'}, form={'a': 'b'}) res = d('http://example.com', {'Accept': '*/*'}, json={'a': 'b'})
Perform
DELETErequest withPyjo.UserAgent.delete()and return resultingPyjo.Message.Responseobject.
- Pyjo.f(*parts: object) Pyjo.File.object[source]¶
path = f('/home/sri/foo.txt')
Turn string into a
Pyjo.Fileobject.$ python -m Pyjo 'r(j(f("hello.json").slurp()))'
- Pyjo.g(*args: Any, **kwargs: Any) Pyjo.Message.Response.object[source]¶
res = g('example.com') res = g('http://example.com', {'Accept': '*/*'}, b'Hi!') res = g('http://example.com', {'Accept': '*/*'}, form={'a': 'b'}) res = g('http://example.com', {'Accept': '*/*'}, json={'a': 'b'})
Perform
GETrequest withPyjo.UserAgent.get()and return resultingPyjo.Message.Responseobject.$ python -m Pyjo 'g("mojolicious.org").dom("h1").map("text").join("\n")'
- Pyjo.h(*args: Any, **kwargs: Any) Pyjo.Message.Response.object[source]¶
res = h('example.com') res = h('http://example.com', {'Accept': '*/*'}, b'Hi!') res = h('http://example.com', {'Accept': '*/*'}, form={'a': 'b'}) res = h('http://example.com', {'Accept': '*/*'}, json={'a': 'b'})
Perform
HEADrequest withPyjo.UserAgent.head()and return resultingPyjo.Message.Responseobject.
- Pyjo.i(iterable: Iterable | AsyncIterable = ()) Pyjo.Iterator.object[T][source]¶
iterator = i(range(1, 4))
Turn iterable or asynchronous iterable into a
Pyjo.Iteratorobject.# "2, 4, 6" i(range(1, 4)).map(lambda n: n * 2).join(', ')
- Pyjo.j(value: Any) Any[source]¶
data = j([1, 2, 3]) data = j({'foo': 'bar'}) value = j(data)
Encode Python data structure or decode JSON with
Pyjo.JSON.j().$ python -m Pyjo 'f("hello.json").spew(j({"hello": "world!"}))'
- Pyjo.l(*args: Any) Pyjo.URL.object[source]¶
url = l('https://mojolicious.org')
Turn a string into a
Pyjo.URLobject.$ python -m Pyjo 'l("/perldoc").to_abs(l("https://mojolicious.org"))'
- Pyjo.n(cb: Callable[[], object], count: int = 1) None[source]¶
n(lambda: ...) n(lambda: ..., 100)
Benchmark callback and print the results to
sys.stderr, with an optional number of iterations, which defaults to1.$ python -m Pyjo 'n(lambda: print(g("mojolicious.org").code))'
- Pyjo.o(*args: Any, **kwargs: Any) Pyjo.Message.Response.object[source]¶
res = o('example.com') res = o('http://example.com', {'Accept': '*/*'}, b'Hi!') res = o('http://example.com', {'Accept': '*/*'}, form={'a': 'b'}) res = o('http://example.com', {'Accept': '*/*'}, json={'a': 'b'})
Perform
OPTIONSrequest withPyjo.UserAgent.options()and return resultingPyjo.Message.Responseobject.
- Pyjo.p(*args: Any, **kwargs: Any) Pyjo.Message.Response.object[source]¶
res = p('example.com') res = p('http://example.com', {'Accept': '*/*'}, b'Hi!') res = p('http://example.com', {'Accept': '*/*'}, form={'a': 'b'}) res = p('http://example.com', {'Accept': '*/*'}, json={'a': 'b'})
Perform
POSTrequest withPyjo.UserAgent.post()and return resultingPyjo.Message.Responseobject.
- Pyjo.q(pattern: str, flags: int = 0) re.Pattern[str][source]¶
regex = q(r'/phpinfo\.php') regex = q(r'/php(?P<path>/.*)?')
Compile a regular expression with
re.compile(), asqr//does in Perl, as the whole path thatPyjoyment.Plugin.Mountmounts an application under.$ python -m Pyjo 'bool(q(r"\d+").fullmatch("42"))'
- Pyjo.r(*values: object) str[source]¶
python = r({'data': 'structure'})
Dump a Python data structure with
Pyjo.Util.dumper().$ python -m Pyjo 'r(g("example.com").headers.to_dict())'
- Pyjo.s(*, fd: int = 0) Pyjo.String.Bytes.object[source]¶
- Pyjo.s(part: object, /, *parts: object, fd: int = 1) Pyjo.String.Bytes.object
data = s() data = s(fd=3) data = s(data) data = s(chars, data) data = s(data, fd=2)
Read the whole standard input, or the file descriptor
fd, once, as aPyjo.String.Bytesobject, which another call returns again, or write the parts to the standard output, or tofd, as they are, without a line break, bytes as bytes and anything else asUTF-8characters, asb()joins them, and return what it wrote, so that the calls nest and write to several streams, asteedoes.python -m Pyjodoes not print what was written to the standard output already. A terminal is read until its end, ascatreads it. The bytes keep their encoding forj()andy(), which detect it, with a byte order mark too, and the output its bytes, as of an image.$ kubectl get pod myapp -o json | python -m Pyjo 'j(s())["status"]["phase"]' $ python -m Pyjo 's(g("https://mojolicious.org/mojo/logo-white.png").body)' > logo-white.png $ kubectl get pod myapp -o yaml | python -m Pyjo 's(s(s(), fd=2))' > pod.yaml
- Pyjo.t(*args: Any, **kwargs: Any) Pyjo.Message.Response.object[source]¶
res = t('example.com') res = t('http://example.com', {'Accept': '*/*'}, b'Hi!') res = t('http://example.com', {'Accept': '*/*'}, form={'a': 'b'}) res = t('http://example.com', {'Accept': '*/*'}, json={'a': 'b'})
Perform
PATCHrequest withPyjo.UserAgent.patch()and return resultingPyjo.Message.Responseobject.
- Pyjo.u(*parts: object) Pyjo.String.Unicode.object[source]¶
stream = u('lalala')
Turn string into a
Pyjo.String.Unicodeobject.$ python -m Pyjo 'u(g("mojolicious.org").text).html_unescape()'
- Pyjo.w(*args: Any, **kwargs: Any) Pyjo.Message.Response.object[source]¶
res = w('example.com') res = w('http://example.com', {'Accept': '*/*'}, b'Hi!') res = w('http://example.com', {'Accept': '*/*'}, form={'a': 'b'}) res = w('http://example.com', {'Accept': '*/*'}, json={'a': 'b'})
Perform
PUTrequest withPyjo.UserAgent.put()and return resultingPyjo.Message.Responseobject.
- Pyjo.x(*args: Any) Pyjo.DOM.object[source]¶
dom = x('<div>Hello!</div>')
Turn HTML/XML input into
Pyjo.DOMobject.$ python -m Pyjo 'x(f("test.html").slurp()).at("title").text'
- Pyjo.y(*values: Any) Any[source]¶
documents = y(string) string = y({'foo': 'bar'}) string = y({'foo': 'bar'}, {'baz': 1})
Load YAML into a
Pyjo.Collectionof its documents, or dump values to YAML, one document for each, withPyjo.YAML.y().$ python -m Pyjo 'y(j(f("hello.json").slurp()))' $ python -m Pyjo 'r(y(f("hello.yaml").slurp()).first())'
- Pyjo.z = Pyjo.JSON.Schema.z¶
schema = z.object(name=z.string(), stars=z.int().min(0))
The builder of
Pyjo.JSON.Schema, its classz, afterzof zod, whose schemas validate a value, as the JSON of a response.$ python -m Pyjo 'z.object(port=z.int()).validate(j(f("config.json").slurp()))' $ python -m Pyjo 'z.object(id=z.int()).validate(g("api.github.com/repos/pyjoyment/Pyjoyment").json()).is_valid'