Closed chrisjsimpson closed 2 months ago
Yes good point -- if you (or anyone else!) have time to create a PR for this, that would be appreciated!
can you assign it to me I want to work on this
Thank you
How you getting along @rajbhoyar729? Lemme know if you need any help.
@jph00 thank if I need any help i will ask for sure
@jph00 I think i will need a till explanation on the task can you please help me a little bro
@jph00 I think i will need a till explanation on the task can you please help me a little bro
I'll take a look at it, no worries.
I want to work on it can you please guide me a little as its my first time so please give me the opportunity to learn and solve the issue i was just asking for a little guidence
Understood -- keep an eye out for "Google first issue" labels for stuff that's good for a first time. You picked an extremely technical and complex issue in this case unfortuately! :D
Hey @chrisjsimpson, the url_for
functionality is built into the request
object that FastHTML inherits from Starlette. Here's a simple application that serves as proof:
from fasthtml.common import *
from random import randint
app, rt = fast_app()
@rt('/', name='index')
def get(req):
url = req.url_for('toggle', tid=randint(1,100))
return Titled("Index",
P(A(href=req.url_for('Thing'))(req.url_for('Thing'))),
P(A(href=url)(url))
)
@rt('/thing1', name='Thing')
def get(req):
return Titled("Thing", P(A(href=req.url_for('index'))(req.url_for('index'))))
@rt("/toggle/{tid}", name="toggle")
def get(tid: int, req):
return Titled("Toggle",
P(f"Toggle ID {tid}"),
P(A(href=req.url_for('index'))(req.url_for('index')))
)
serve()
We'll document it shortly and share in this ticket. Then close it. However, if it turns out that your request isn't accounted for, let me know and I'll reopen the ticket and discuss it together. 😄
You can also do app.url_path_for(...)
with the app object I find this useful if I am making a component which needs to reverse a URL and I don't want to pass the request into the function which makes the component
Small description
Starlette supports reverse url lookups via
url_for
, to use it requires endpoints must provide aname
.By default,
fasthtml.core.RouteX
routes are called_endp
FastHTML routes can be named, and the request context passed in e.g.
Results in:
However
url_for
can't be called on them in the same wayProposed solution
Consider exposing the
url_for
api so that named route may easily be back referenced (useful for things like redirecting, DRY)