PWeb Controller Manual

PWeb Controller Functionalities


PWeb Controller Blueprint

A Blueprint define a controller in PWeb

from pweb import Blueprint

# Controller for REST API
person_api_url_prefix = "/api/v1/person"
person_api_controller = Blueprint(
    "person_api_controller",
    __name__,
    url_prefix=person_api_url_prefix
)

# Controller for Static Resource and Templating Mapping
boot_static_controller = Blueprint(
    "boot-static",
    __name__,
    url_prefix="/",
    template_folder="../template-assets/templates",
    static_folder="../template-assets/assets",
    static_url_path="boot-assets"
)

# Controller with all parameter
student_blueprint = Blueprint(
    name='student',
    import_name=__name__,
    url_prefix='/student',
    static_folder="student-static",
    static_url_path="student-static",
    template_folder="student-template"
)
  • Here

    • name : The name of the controller. Will be prepended to each endpoint name.

    • import_name : The name of the blueprint package, usually __name__. This helps locate the root_path for the blueprint.

    • url_prefix : A path to prepend to all the controller URLs, to make them distinct from the rest of the app’s routes.

    • static_folder : A folder with static files that should be served by the controller static route.

    • static_url_path : The url to serve static files from. Defaults to static_folder.

    • template_folder : A directory with templates that should be added to the app’s template search path. The path is relative to the controller root path.


PWeb Routing

In PWeb application Routing is a process which convert user or browser requested url to the application function/method/action which are going to response & pass the requested data into the action. Modern web applications use meaningful URLs to help users. Users are more likely to like a page and come back if the page uses a meaningful URL they can remember and use to directly visit a page.

Use the route() decorator to bind a function to a URL. Example codes:

@person_controller.route("/", methods=['GET'])
def index():
    return 'Home Page'


@person_controller.route("/other", methods=['GET'])
def other():
    return 'Other Page'


Pass data in url (URL variables)

We can specify variable into the URL using <variable_name> syntax, such as /operator/edit/<operator_id>. Optionally, we can use a converter to specify the type of the argument like <converter:variable_name>, such as /operator/edit/<int:operator_id>. The variable value will pass to the method using same name. Example codes

@person_controller.route('/operator/edit/<operator_id>')
def edit_operator(operator_id):
    return "Operator id: " + operator_id


@person_controller.route('/operator/edit-with-type/<int:operator_id>')
def edit_operator_with_data_type(operator_id: int):
    return "Operator id with data type: " + str(operator_id)


@person_controller.route('/path/<path:sub_path>')
def show_sub_path(sub_path):
    return "Sub-path : " + sub_path


URL variable types

Name Description

string

accepts any text without a slash

int

accepts positive integers

float

accepts positive floating point values

path

like string but also accepts slashes

uuid

accepts UUID strings


Routing HTTP Methods

GET, POST, PUT, DELETE, HEAD etc. methods are available in HTTP, If we want to allow route for only specific method, then we have to specify the HTTP method type into the route. By default, a route only answers to GET requests. Example codes

@person_controller.route('/method/post-only', methods=['POST'])
def post_only():
    return "Yeh the request method is POST"


@person_controller.route('/method/post-or-get', methods=['POST', 'GET'])
def post_or_get():
    if request.method == 'POST':
        return "Yeh the request method is POST"
    else:
        return "Yeh the request method is GET"