Skip to content

Minimus Routing

jefmud edited this page Nov 3, 2022 · 1 revision

Routing

Next, we need to add a Python "method" or function to display something on a route. Minimus supports route decorators similar to Bottle and Flask. But even more at its core, the Minimus app object supports Minimus.add_route( ... )

app.add_route( ... )

from minimus import Minimus
app = Minimus(__name__)

def hello(environ):
    return "Minimus says hello."

app.add_route('/hello', hello)

if __name__ == '__main__':
    app.run()

You must use the environ variable on the method otherwise you will be greeted by a nasty error message. We're not making use of the environ variable yet, but it needs to have a placeholder for now. We simply return any string, we'll choose, "Minimus says hello". And a little business logic where we need to inform the app of the route or URL we are going to use... '/hello' and then the name of the function hello that will serve.

And finally, we run our app.

$ python app.py

We will get a "404" error on the root, but if we use the http://localhost:5000/hello

We get the following output on the browser--

Minimus says hello.

Routing parameters

add_route(self, route, handler, methods=None, route_name=None)

simple route addition to Mimimus application object

route (string) - supports simple static routes (must begin with a slash) as well as named variables. It also supports a special PATH catchment variable e.g. "/blog/mypath:path".

handler (function) - a callback function that handles the route. By default, the callback's first parameter is an environment variable. The callback can also have OTHER parameters that match the variables.

methods (list) - the optional HTTP Methods supported, by default it supports ["GET"] but can be ["POST", "GET", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH"]

route_name (string) - the optional name of the route used by the app.url_for(name) routing. If this is omitted, the route takes on the name of its base path. The route_name is handy for renaming the path and is also used in the url_for( string ) which we will visit a little later.

Route decorator

Minimus also supports a route decorator style, similar to Bottle or Flask. Someone who is new to the framework, but a Flask veteran will recognize that the decorator's behavior is very similar.

from minimus import Minimus
app = Minimus(__name__)

@app.route('/hello')
def hello(environ):
    return "Minimus says hello."

if __name__ == '__main__':
    app.run()

Clone this wiki locally