Routing

Routing in Pythonfrost allows you to connect URL paths to Python functions easily.

Basic Route

from pythonfrost import Server, route

route("/")
def home():
    return "Hello, World!"

Server()

Dynamic Routes

You can pass parameters directly in the URL:

route("/sayhello/<username>")
def greet(username):
    return f"Hello, {username}"

Multiple Routes

Define as many routes as you need:

route("/sayhello/<username>"")
def greet(username):
    return f"Hello, {username}"

route("/")
def home():
    return f"Home Page"

Routes make it simple to build web applications, from small projects to more complex sites.

Read Template

Read an HTML page template using read_template() function

from pythonfrost import read_template, route, Server

@route("/")
def home():
    return read_template("index.html")

Server()

you have to create a folder called "templates" and put your html files there to read the template.

Static Files

to add your static files (CSS, JS) you have to create a folder in your project folder called: "static" and put your static files in static folder.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <script src="script.js"></script>
</body>
</html>

Template Interpolation

to replace a varriable in html directly in python here's an easy way:

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ title }}</title>
</head>
<body>
    <p>{{ name }}</p>
    <li>
        <ul>{{ numbers }}</ul>
    </li>
</body>
</html>

Python:

from pythonfrost import read_template, route, Server


@route("/")
def home():
    context = {
        "title":"test",
        "name":"jack",
        "numbers":[1,2,3]
    }
    return read_template("test.html", context=context)

Server()