Pyjo.App - Duct tape for the HTML5 web

from Pyjo.App import App

class MyApp(App):

    # All the complexities of CGI, WSGI, ASGI, HTTP and WebSockets get
    # reduced to a single method call!
    def handler(self, tx):

        # Resume transaction at the end of the block
        with tx:

            # Request
            method = tx.req.method
            path = tx.req.url.path

            # Response
            tx.res.code = 200
            tx.res.headers.content_type = 'text/plain'
            tx.res.body = f'{method} request for {path}!'.encode()

# Or import the module, as in Google Python Style Guide

import Pyjo.App

class MyApp(Pyjo.App.object):

    # All the complexities of CGI, WSGI, ASGI, HTTP and WebSockets get
    # reduced to a single method call!
    def handler(self, tx):

        # Resume transaction at the end of the block
        with tx:

            # Request
            method = tx.req.method
            path = tx.req.url.path

            # Response
            tx.res.code = 200
            tx.res.headers.content_type = 'text/plain'
            tx.res.body = f'{method} request for {path}!'.encode()

Pyjo.App is the base class of applications of Pyjo, a flexible runtime environment for real-time web frameworks, with all the basic tools and helpers needed to write simple web applications and higher level web frameworks, such as Pyjoyment.

The end of a with block closes the connections and the servers of its ua.

See also

Pyjo.App.WSGI, Pyjo.HelloWorld, Pyjoyment, https://mojolicious.org.

Inspired by Mojo of Mojolicious 6.0, the base class of Mojolicious until Mojolicious 7.

class Pyjo.App.Pyjo_App(attrs: dict[str, Any] | None = None, /, **kwargs: Any)[source]

Pyjo.App inherits all methods from Pyjo.Base and implements the following new ones.

build_tx() → Pyjo_Transaction_HTTP[source]
tx = app.build_tx()

Transaction builder, defaults to building a Pyjo.Transaction.HTTP object.

close() → None[source]
app.close()

Close the connections and the servers of ua, as the end of a with block does.

config() → dict[str, Any][source]
config(name: str, /) → Any
config(values: dict[str, Any], /) → Self
config(**values: Any) → Self
hash = app.config()
foo = app.config('foo')
app = app.config({'foo': 'bar', 'baz': 23})
app = app.config(foo='bar', baz=23)

Application configuration.

# Remove value
foo = app.config().pop('foo')

# Assign multiple values at once
app.config(foo='test', bar=23)
handler(tx: Any) → None[source]
app.handler(Pyjo.Transaction.HTTP.new())

The handler is the main entry point to your application or framework and will be called for each new transaction, which will usually be a Pyjo.Transaction.HTTP or Pyjo.Transaction.WebSocket object. A route of Pyjoyment that embeds the application, as Pyjoyment.Plugin.Mount does, calls it with its controller instead, whose tx is the transaction and whose stash has the rest of the path in path. Meant to be overloaded in a subclass.

def handler(self, tx):
    ...
property home: Pyjo_Home[source]
home = app.home
app.home = Pyjo.Home.new()

The home directory of your application, defaults to a Pyjo.Home object which stringifies to the actual path.

# Portably generate path relative to home directory
path = app.home.child('data', 'important.txt')
property log: Pyjo_Log[source]
log = app.log
app.log = Pyjo.Log.new()

The logging layer of your application, defaults to a Pyjo.Log object. The level will default to the PYJO_LOG_LEVEL environment variable, or trace, and the messages will be written to sys.stderr.

# Log debug message
app.log.debug('It works')
property moniker: str[source]
moniker = app.moniker
app.moniker = 'foo_bar'

Moniker of this application, often used as default filename for configuration files and the like, defaults to decamelizing the application class with Pyjo.Util.decamelize(): the module of a class that the module names object, as pyjo-hello_world of Pyjo.HelloWorld, or the class in its module.

server(server: Pyjo_Server) → None[source]
app.server(Pyjo.Server.new())

Called by the server before it starts. Meant to be overloaded in a subclass.

server_stopped(server: Pyjo_Server) → Pyjo_Promise | None[source]
promise = app.server_stopped(Pyjo.Server.new())

Called by the server once it has stopped gracefully, in each process and thread that has run the application, with the event loop no longer running. A promise that it returns is waited for, so that the application can clean up with the event loop. Meant to be overloaded in a subclass.

start(*args: str) → Self[source]
app = app.start()

Start the application. When the environment variable PYJO_APP_LOADER is set, as Pyjo.Server.Pyjo_Server.load_app() does, it is kept in Pyjo.Server.loaded_app for the server.

property ua: Pyjo_UserAgent[source]
ua = app.ua
app.ua = Pyjo.UserAgent.new()

A full featured HTTP user agent for use in your applications, defaults to a Pyjo.UserAgent object.

# Perform blocking request
print(app.ua.get('example.com').result.body)