Routes

Setup

from fasthtml.common import *
from fasthtml.jupyter import *

from fh_commons.core import *
from fh_commons.static import *

import json
import pandas as pd
from math import ceil
from ngrok_token import *
url = start_ngrok(token)

app,rt = fast_app(pico=False,hdrs=hdrs)
server = JupyUvi(app)

Routes

@rt

Both get and post method, function name is the route’s name

@rt
def test():
    return P('hi')

@rt(‘/sth’)

If function name is get or post, then would be either of the method.

If function name is something else, would not change the route, but specify the route name

@rt('/something')
def get(): return P('hi')

With params:

@rt('/something/{data}')
def get(data:str): return P(data)
htmx(url,'/something/sdf')

Use function to indicate route’s name

@rt('/something')
def sdfsdf(req): 
    return P(req.url_for('sdfsdf'))

With route’s name

@rt('/something',name='aaa')
def get(req): 
    return P(req.url_for('aaa'))

@app.get/post

Similar to @rt, the function name indicates route

@app.get
def asdf():
    return P('hi')

@app.get(‘/something’)

@app.get('/user/{nm}')
def get_nm(nm:str): return f"Good day to you, {nm}!"

End

server.stop()
kill_ngrok()